Skip to content

Commit ccde64e

Browse files
committed
Change casual submission to allow submitting multiple categories
1 parent 4abf57b commit ccde64e

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

src/routes/postCasual.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ interface ExistingVote {
2525
}
2626

2727
export async function postCasual(req: Request, res: Response) {
28-
const { videoID, userID, downvote, category } = req.body as CasualVoteSubmission;
28+
const { videoID, userID, downvote, categories } = req.body as CasualVoteSubmission;
2929
const service = getService(req.body.service);
3030

31-
if (!videoID || !userID || userID.length < 30 || !service || !category) {
31+
if (!videoID || !userID || userID.length < 30 || !service || !categories || !Array.isArray(categories)) {
3232
return res.status(400).send("Bad Request");
3333
}
34-
if (!config.casualCategoryList.includes(category)) {
34+
if (!categories.every((c) => config.casualCategoryList.includes(c))) {
3535
return res.status(400).send("Invalid category");
3636
}
3737

@@ -54,28 +54,25 @@ export async function postCasual(req: Request, res: Response) {
5454
const now = Date.now();
5555
const voteType: CasualVoteType = downvote ? CasualVoteType.Downvote : CasualVoteType.Upvote;
5656

57-
const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID;
58-
const UUID = existingUUID || crypto.randomUUID();
59-
60-
const alreadyVotedTheSame = await handleExistingVotes(videoID, service, UUID, hashedUserID, hashedIP, category, voteType, now);
61-
if (existingUUID) {
62-
if (!alreadyVotedTheSame) {
63-
if (downvote) {
64-
await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" + 1 WHERE "UUID" = ?`, [UUID]);
65-
} else {
66-
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" + 1 WHERE "UUID" = ?`, [UUID]);
57+
for (const category of categories) {
58+
const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID;
59+
const UUID = existingUUID || crypto.randomUUID();
60+
61+
const alreadyVotedTheSame = await handleExistingVotes(videoID, service, UUID, hashedUserID, hashedIP, category, voteType, now);
62+
if (existingUUID) {
63+
if (!alreadyVotedTheSame) {
64+
if (downvote) {
65+
await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" + 1 WHERE "UUID" = ?`, [UUID]);
66+
} else {
67+
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" + 1 WHERE "UUID" = ?`, [UUID]);
68+
}
6769
}
70+
} else {
71+
await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes", "downvotes") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
72+
[videoID, service, hashedVideoID, now, UUID, category, downvote ? 0 : 1, downvote ? 1 : 0]);
6873
}
69-
} else {
70-
if (downvote) {
71-
throw new Error("Title submission doesn't exist");
72-
}
73-
74-
await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes", "downvotes") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
75-
[videoID, service, hashedVideoID, now, UUID, category, downvote ? 0 : 1, downvote ? 1 : 0]);
7674
}
7775

78-
//todo: cache clearing
7976
QueryCacher.clearBrandingCache({ videoID, hashedVideoID, service });
8077

8178
res.status(200).send("OK");

src/types/branding.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export interface CasualVoteSubmission {
106106
userID: UserID;
107107
service: Service;
108108
downvote: boolean | undefined;
109-
category: CasualCategory;
109+
categories: CasualCategory[];
110110
}
111111

112112
export interface BrandingSegmentDBResult {

test/cases/postCasual.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("postCasual", () => {
2222
const videoID = "postCasual1";
2323

2424
const res = await postCasual({
25-
category: "clever",
25+
categories: ["clever"],
2626
userID: userID1,
2727
service: Service.YouTube,
2828
videoID
@@ -40,7 +40,7 @@ describe("postCasual", () => {
4040
const videoID = "postCasual1";
4141

4242
const res = await postCasual({
43-
category: "clever",
43+
categories: ["clever"],
4444
userID: userID1,
4545
service: Service.YouTube,
4646
videoID
@@ -58,7 +58,7 @@ describe("postCasual", () => {
5858
const videoID = "postCasual1";
5959

6060
const res = await postCasual({
61-
category: "clever",
61+
categories: ["clever"],
6262
userID: userID2,
6363
service: Service.YouTube,
6464
videoID
@@ -76,7 +76,7 @@ describe("postCasual", () => {
7676
const videoID = "postCasual1";
7777

7878
const res = await postCasual({
79-
category: "clever",
79+
categories: ["clever"],
8080
downvote: true,
8181
userID: userID1,
8282
service: Service.YouTube,
@@ -95,7 +95,7 @@ describe("postCasual", () => {
9595
const videoID = "postCasual1";
9696

9797
const res = await postCasual({
98-
category: "clever",
98+
categories: ["clever"],
9999
downvote: true,
100100
userID: userID3,
101101
service: Service.YouTube,
@@ -114,7 +114,7 @@ describe("postCasual", () => {
114114
const videoID = "postCasual1";
115115

116116
const res = await postCasual({
117-
category: "clever",
117+
categories: ["clever"],
118118
downvote: false,
119119
userID: userID3,
120120
service: Service.YouTube,
@@ -129,4 +129,49 @@ describe("postCasual", () => {
129129
assert.strictEqual(dbVotes.downvotes, 1);
130130
});
131131

132+
it("submit multiple casual votes", async () => {
133+
const videoID = "postCasual2";
134+
135+
const res = await postCasual({
136+
categories: ["clever", "other"],
137+
userID: userID1,
138+
service: Service.YouTube,
139+
videoID
140+
});
141+
142+
assert.strictEqual(res.status, 200);
143+
const dbVotes = await queryCasualVotesByVideo(videoID, true);
144+
145+
assert.strictEqual(dbVotes[0].category, "clever");
146+
assert.strictEqual(dbVotes[0].upvotes, 1);
147+
assert.strictEqual(dbVotes[0].downvotes, 0);
148+
149+
assert.strictEqual(dbVotes[1].category, "other");
150+
assert.strictEqual(dbVotes[1].upvotes, 1);
151+
assert.strictEqual(dbVotes[1].downvotes, 0);
152+
});
153+
154+
it("submit multiple casual downvotes", async () => {
155+
const videoID = "postCasual3";
156+
157+
const res = await postCasual({
158+
categories: ["clever", "other"],
159+
userID: userID1,
160+
service: Service.YouTube,
161+
videoID,
162+
downvote: true
163+
});
164+
165+
assert.strictEqual(res.status, 200);
166+
const dbVotes = await queryCasualVotesByVideo(videoID, true);
167+
168+
assert.strictEqual(dbVotes[0].category, "clever");
169+
assert.strictEqual(dbVotes[0].upvotes, 0);
170+
assert.strictEqual(dbVotes[0].downvotes, 1);
171+
172+
assert.strictEqual(dbVotes[1].category, "other");
173+
assert.strictEqual(dbVotes[1].upvotes, 0);
174+
assert.strictEqual(dbVotes[1].downvotes, 1);
175+
});
176+
132177
});

0 commit comments

Comments
 (0)