diff --git a/src/routes/ratings/postRating.ts b/src/routes/ratings/postRating.ts index fbe10a8..866fe57 100644 --- a/src/routes/ratings/postRating.ts +++ b/src/routes/ratings/postRating.ts @@ -34,8 +34,8 @@ export async function postRating(req: Request, res: Response): Promise const existingVote = await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]); if (existingVote.count > 0 && !enabled) { // Undo the vote - await db.prepare("run", `UPDATE "ratings" SET "count" = "count" - 1 WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]); await privateDB.prepare("run", `DELETE FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]); + await db.prepare("run", `UPDATE "ratings" SET "count" = "count" - 1 WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]); } else if (existingVote.count === 0 && enabled) { // Make sure there hasn't been another vote from this IP const existingIPVote = (await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "hashedIP" = ?`, [videoID, service, type, hashedIP])) @@ -43,6 +43,10 @@ export async function postRating(req: Request, res: Response): Promise if (existingIPVote) { // if exisiting vote, exit early instead return res.sendStatus(200); } + + // Create entry in privateDB + await privateDB.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "userID", "timeSubmitted", "hashedIP") VALUES (?, ?, ?, ?, ?, ?)`, [videoID, service, type, hashedUserID, Date.now(), hashedIP]); + // Check if general rating already exists, if so increase it const rating = await db.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]); if (rating.count > 0) { @@ -50,9 +54,6 @@ export async function postRating(req: Request, res: Response): Promise } else { await db.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, 1, ?)`, [videoID, service, type, hashedVideoID]); } - - // Create entry in privateDB - await privateDB.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "userID", "timeSubmitted", "hashedIP") VALUES (?, ?, ?, ?, ?, ?)`, [videoID, service, type, hashedUserID, Date.now(), hashedIP]); } // clear rating cache QueryCacher.clearRatingCache({ hashedVideoID, service });