Update updateRequestScore endpoint and clean up naming

master
Dessa Simpson 2020-10-18 17:24:24 -07:00
parent 54a7145630
commit 36d3871bdc
6 changed files with 19 additions and 12 deletions

View File

@ -1,7 +1,7 @@
CREATE TABLE scores ( CREATE TABLE scores (
url varchar, url varchar,
baseScore int NOT NULL DEFAULT 0,
score int NOT NULL DEFAULT 0, score int NOT NULL DEFAULT 0,
scoreModifier int NOT NULL DEFAULT 0,
PRIMARY KEY (url), PRIMARY KEY (url),
FOREIGN KEY (url) REFERENCES requests(url) FOREIGN KEY (url) REFERENCES requests(url)
); );

View File

@ -32,7 +32,7 @@ case of no votes), and users.is{follower,subscriber} must be COALESCEd to FALSE
as in the case of no votes, user.* will be NULL. as in the case of no votes, user.* will be NULL.
*/ */
CREATE OR REPLACE VIEW vote_score_all_vw AS CREATE OR REPLACE VIEW vote_score_vw AS
WITH votepoints AS (SELECT normaluservotepoints, followervotepoints, subscribervotepoints FROM config) WITH votepoints AS (SELECT normaluservotepoints, followervotepoints, subscribervotepoints FROM config)
SELECT requests.url AS url, SELECT requests.url AS url,
COUNT(votes.requesturl) AS count, COUNT(votes.requesturl) AS count,

View File

@ -1,9 +1,9 @@
CREATE OR REPLACE PROCEDURE update_scores() CREATE OR REPLACE PROCEDURE update_scores()
LANGUAGE SQL LANGUAGE SQL
AS $$ AS $$
UPDATE scores SET score = basescore + votescore UPDATE scores SET score = votescore + scoreModifier
FROM vote_score_all_vw FROM vote_score_vw
WHERE scores.url = vote_score_all_vw.url; WHERE scores.url = vote_score_vw.url;
$$; $$;
CREATE OR REPLACE PROCEDURE add_request(url varchar,requester int) CREATE OR REPLACE PROCEDURE add_request(url varchar,requester int)
@ -28,3 +28,10 @@ CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
DELETE FROM votes WHERE requesturl = url AND userid = voteuser; DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
CALL update_scores(); 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();
$$;

View File

@ -128,7 +128,7 @@ app.post("/api/updateRequestState", async (request, response) => {
.catch((e: any) => errorHandler(request,response,e)); .catch((e: any) => errorHandler(request,response,e));
}); });
app.post("/api/updateRequestScore", async (request, response) => { app.post("/api/updateRequestScoreModifier", async (request, response) => {
if (request.session) await validateApiToken(request.session); if (request.session) await validateApiToken(request.session);
if (!request.session || !request.session.user) { if (!request.session || !request.session.user) {
response.status(401); response.status(401);
@ -154,7 +154,7 @@ app.post("/api/updateRequestScore", async (request, response) => {
var url = request.body.url as string; var url = request.body.url as string;
var scoreDiff = parseInt(request.body.scoreDiff as string, 10); var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
response.type('text/plain'); response.type('text/plain');
requests.updateRequestScore(url,scoreDiff).then((val: [number,string]) => { requests.updateRequestScoreModifier(url,scoreDiff).then((val: [number,string]) => {
response.status(val[0]); response.status(val[0]);
response.send(val[1]); response.send(val[1]);
}) })

View File

@ -82,9 +82,9 @@ export const updateRequestState = {
text: "UPDATE requests SET state = $2 WHERE url = $1" text: "UPDATE requests SET state = $2 WHERE url = $1"
} }
export const updateRequestScore = { export const updateRequestScoreModifier = {
name: "updateRequestScore", name: "updateRequestScoreModifier",
text: "UPDATE requests SET score = score + $2 WHERE url = $1" text: "CALL update_request_score_modifier($1,$2)"
} }
export const deleteRequest = { export const deleteRequest = {

View File

@ -61,8 +61,8 @@ export async function updateRequestState(url: string, state: string): Promise<[n
.then(() => [200,"Song request state updated."] as [number,string]); .then(() => [200,"Song request state updated."] as [number,string]);
}; };
export async function updateRequestScore(url: string, scoreDiff: number): Promise<[number,string]> { export async function updateRequestScoreModifier(url: string, scoreDiff: number): Promise<[number,string]> {
var query = Object.assign(queries.updateRequestScore, { values: [url,scoreDiff] }); var query = Object.assign(queries.updateRequestScoreModifier, { values: [url,scoreDiff] });
return db.query(query) return db.query(query)
.then(() => [200,"Song request score updated."] as [number,string]); .then(() => [200,"Song request score updated."] as [number,string]);
}; };