CREATE OR REPLACE PROCEDURE update_scores()
	LANGUAGE SQL
	AS $$
		UPDATE scores SET score = votescore + scoreModifier
			FROM vote_score_vw
			WHERE scores.url = vote_score_vw.url;
	$$;

CREATE OR REPLACE PROCEDURE add_request(url varchar,requester int)
	LANGUAGE SQL
	AS $$
		INSERT INTO requests (url,requester) VALUES (url,requester);
		INSERT INTO requestMetadata (url) VALUES (url);
		INSERT INTO scores (url) VALUES (url);
		INSERT INTO votes (requesturl,userid) VALUES (url,requester);
		CALL update_scores();
	$$;

CREATE OR REPLACE PROCEDURE add_vote(url varchar,voteuser int)
	LANGUAGE SQL
	AS $$
		INSERT INTO votes (requesturl,userid) VALUES (url,voteuser);
		CALL update_scores();
	$$;

CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
	LANGUAGE SQL
	AS $$
		DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
		CALL update_scores();
	$$;

CREATE OR REPLACE PROCEDURE update_request_score_modifier(updateurl varchar, scoreDiff int)
	LANGUAGE SQL
	AS $$
		UPDATE scores SET scoreModifier = scoreModifier + scoreDiff WHERE url = updateurl;
		CALL update_scores();
	$$;

CREATE OR REPLACE PROCEDURE update_vote_points(normaluser int, follower int, subscriber int)
	LANGUAGE SQL
	AS $$
		UPDATE config SET normaluservotepoints = normaluser,
			followervotepoints = follower, subscribervotepoints = subscriber;
		CALL update_scores();
	$$;
