DROP PROCEDURE IF EXISTS `add_post`;
DELIMITER ;;
CREATE PROCEDURE `add_post` (
    IN title VARCHAR(500),
    IN thumbnail_url VARCHAR(500),
    IN post_description VARCHAR(1000),
    IN p_session_id CHAR(64)
)
proc:BEGIN
    IF (SELECT COUNT(session_id) FROM sessions WHERE session_id = LOWER(p_session_id)) = 0 OR p_session_id IS NULL THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'add_post: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    IF title = '' OR title IS NULL THEN
        SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'add_post: Title Required';
        LEAVE proc;
    END IF;
    IF post_description = '' OR post_description IS NULL THEN
        SIGNAL SQLSTATE '45002' SET MESSAGE_TEXT = 'add_post: Post Description Required';
        LEAVE proc;
    END IF;
    IF thumbnail_url = '' OR thumbnail_url IS NULL THEN
        SIGNAL SQLSTATE '45003' SET MESSAGE_TEXT = 'add_post: No Thumbnail specified';
        LEAVE proc;
    END IF;
    INSERT INTO newsfeed VALUES (post_id, @user_id, title, thumbnail_url, post_description);
    SELECT 0 AS 'status', 'add_post' AS 'proc';
END ;;
DELIMITER ;
