Skip to content

Commit

Permalink
fix #309: multiple stars don't crash server
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Sep 9, 2024
1 parent 9472a79 commit a7fe83b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions database/account-info/stars/star-bonus.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { bonusStars } from '../collections.js';
* @returns {Promise<boolean>} true if the bonus was not starred before
*/
async function starBonus (userId, bonusId) {
if (await bonusStars.findOne({ user_id: userId, bonus_id: bonusId })) {
return false;
}

await bonusStars.insertOne({ user_id: userId, bonus_id: bonusId });
return true;
// get whether a document was inserted
const result = await bonusStars.updateOne(
{ user_id: userId, bonus_id: bonusId },
{ $set: { user_id: userId, bonus_id: bonusId } },
{ upsert: true }
);
return result.upsertedCount > 0;
}

export default starBonus;
13 changes: 7 additions & 6 deletions database/account-info/stars/star-tossup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { tossupStars } from '../collections.js';
* @returns {Promise<boolean>} true if the tossup was not starred before
*/
async function starTossup (userId, tossupId) {
if (await tossupStars.findOne({ user_id: userId, tossup_id: tossupId })) {
return false;
}

await tossupStars.insertOne({ user_id: userId, tossup_id: tossupId });
return true;
// get whether a document was inserted
const result = await tossupStars.updateOne(
{ user_id: userId, tossup_id: tossupId },
{ $set: { user_id: userId, tossup_id: tossupId } },
{ upsert: true }
);
return result.upsertedCount > 0;
}

export default starTossup;

0 comments on commit a7fe83b

Please sign in to comment.