diff --git a/db/40-scores.sql b/db/40-scores.sql index 74f7e3f..95a4c1d 100644 --- a/db/40-scores.sql +++ b/db/40-scores.sql @@ -1,7 +1,7 @@ CREATE TABLE scores ( url varchar, - baseScore int NOT NULL DEFAULT 0, score int NOT NULL DEFAULT 0, + scoreModifier int NOT NULL DEFAULT 0, PRIMARY KEY (url), FOREIGN KEY (url) REFERENCES requests(url) ); diff --git a/db/90-views.sql b/db/90-views.sql index 5512b20..e012690 100644 --- a/db/90-views.sql +++ b/db/90-views.sql @@ -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. */ -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) SELECT requests.url AS url, COUNT(votes.requesturl) AS count, diff --git a/db/95-procedures.sql b/db/95-procedures.sql index 17a3f6b..84effd3 100644 --- a/db/95-procedures.sql +++ b/db/95-procedures.sql @@ -1,9 +1,9 @@ CREATE OR REPLACE PROCEDURE update_scores() LANGUAGE SQL AS $$ - UPDATE scores SET score = basescore + votescore - FROM vote_score_all_vw - WHERE scores.url = vote_score_all_vw.url; + 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) @@ -28,3 +28,10 @@ CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int) 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(); + $$; diff --git a/src/app.ts b/src/app.ts index 7ce03de..242f4de 100644 --- a/src/app.ts +++ b/src/app.ts @@ -128,7 +128,7 @@ app.post("/api/updateRequestState", async (request, response) => { .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 || !request.session.user) { response.status(401); @@ -154,7 +154,7 @@ app.post("/api/updateRequestScore", async (request, response) => { var url = request.body.url as string; var scoreDiff = parseInt(request.body.scoreDiff as string, 10); 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.send(val[1]); }) diff --git a/src/queries.ts b/src/queries.ts index 423d460..2e288a7 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -82,9 +82,9 @@ export const updateRequestState = { text: "UPDATE requests SET state = $2 WHERE url = $1" } -export const updateRequestScore = { - name: "updateRequestScore", - text: "UPDATE requests SET score = score + $2 WHERE url = $1" +export const updateRequestScoreModifier = { + name: "updateRequestScoreModifier", + text: "CALL update_request_score_modifier($1,$2)" } export const deleteRequest = { diff --git a/src/requests.ts b/src/requests.ts index e04ba64..ff3d982 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -61,8 +61,8 @@ export async function updateRequestState(url: string, state: string): Promise<[n .then(() => [200,"Song request state updated."] as [number,string]); }; -export async function updateRequestScore(url: string, scoreDiff: number): Promise<[number,string]> { - var query = Object.assign(queries.updateRequestScore, { values: [url,scoreDiff] }); +export async function updateRequestScoreModifier(url: string, scoreDiff: number): Promise<[number,string]> { + var query = Object.assign(queries.updateRequestScoreModifier, { values: [url,scoreDiff] }); return db.query(query) .then(() => [200,"Song request score updated."] as [number,string]); };