Skip to content

Commit

Permalink
add get starred ids endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jul 14, 2024
1 parent 62b2faa commit 413f87b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 26 deletions.
12 changes: 12 additions & 0 deletions client/scripts/auth/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,16 @@ export default class star {
.then(response => response.json())
.then(bonuses => bonuses);
}

static async getStarredTossupIds () {
return await fetch('/auth/stars/tossup-ids')
.then(response => response.json())
.then(ids => ids);
}

static async getStarredBonusIds () {
return await fetch('/auth/stars/bonus-ids')
.then(response => response.json())
.then(ids => ids);
}
}
17 changes: 4 additions & 13 deletions database/account-info/stars/get-bonus-stars.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { bonusStars } from '../collections.js';

import getBonusIds from './get-ids-bonus.js';
import { bonuses } from '../../qbreader/collections.js';

/**
*
* @param {ObjectId} userId
* @returns {Promise<types.Bonus[]>}
*/
async function getBonusStars (userId) {
const aggregation = [
{ $match: { user_id: userId } },
{ $addFields: { insertTime: { $toDate: '$_id' } } },
{ $sort: { insertTime: -1 } }
];

const bonusIds = await bonusStars.aggregate(aggregation).toArray();
export default async function getBonusStars (userId) {
const bonusIds = await getBonusIds(userId);
return await bonuses.find(
{ _id: { $in: bonusIds.map(bonus => bonus.bonus_id) } },
{ _id: { $in: bonusIds } },
{
sort: {
'set.name': -1,
Expand All @@ -26,5 +19,3 @@ async function getBonusStars (userId) {
}
).toArray();
}

export default getBonusStars;
17 changes: 17 additions & 0 deletions database/account-info/stars/get-ids-bonus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { bonusStars } from '../collections.js';

/**
* Get the ids of a user's starred bonuses.
* @param {ObjectId} userId - The user ID
* @returns {Promise<ObjectId[]>} The bonus IDs
*/
export default async function getBonusIds (userId) {
const aggregation = [
{ $match: { user_id: userId } },
{ $addFields: { insertTime: { $toDate: '$_id' } } },
{ $sort: { insertTime: -1 } }
];

const bonusIds = await bonusStars.aggregate(aggregation).toArray();
return bonusIds.map(bonus => bonus.bonus_id);
}
17 changes: 17 additions & 0 deletions database/account-info/stars/get-ids-tossup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { tossupStars } from '../collections.js';

/**
* Get the ids of a user's starred tossups.
* @param {ObjectId} userId - The user ID
* @returns {Promise<ObjectId[]>} The tossup IDs
*/
export default async function getTossupIds (userId) {
const aggregation = [
{ $match: { user_id: userId } },
{ $addFields: { insertTime: { $toDate: '$_id' } } },
{ $sort: { insertTime: -1 } }
];

const tossupIds = await tossupStars.aggregate(aggregation).toArray();
return tossupIds.map(tossup => tossup.tossup_id);
}
17 changes: 4 additions & 13 deletions database/account-info/stars/get-tossup-stars.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { tossupStars } from '../collections.js';

import getTossupIds from './get-ids-tossup.js';
import { tossups } from '../../qbreader/collections.js';

/**
*
* @param {ObjectId} userId
* @returns {Promise<types.Tossup[]>}
*/
async function getTossupStars (userId) {
const aggregation = [
{ $match: { user_id: userId } },
{ $addFields: { insertTime: { $toDate: '$_id' } } },
{ $sort: { insertTime: -1 } }
];

const tossupIds = await tossupStars.aggregate(aggregation).toArray();
export default async function getTossupStars (userId) {
const tossupIds = await getTossupIds(userId);
return await tossups.find(
{ _id: { $in: tossupIds.map(tossup => tossup.tossup_id) } },
{ _id: { $in: tossupIds } },
{
sort: {
'set.name': -1,
Expand All @@ -26,5 +19,3 @@ async function getTossupStars (userId) {
}
).toArray();
}

export default getTossupStars;
15 changes: 15 additions & 0 deletions routes/auth/stars/bonus-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getUserId from '../../../database/account-info/get-user-id.js';
import getBonusIds from '../../../database/account-info/stars/get-ids-bonus.js';

import { Router } from 'express';

const router = Router();

router.get('/', async (req, res) => {
const username = req.session.username;
const userId = await getUserId(username);
const ids = await getBonusIds(userId);
res.json(ids);
});

export default router;
4 changes: 4 additions & 0 deletions routes/auth/stars/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import bonusIdsRouter from './bonus-ids.js';
import bonusesRouter from './bonuses.js';
import isStarredBonusRouter from './is-starred-bonus.js';
import isStarredTossupRouter from './is-starred-tossup.js';
import starBonusRouter from './star-bonus.js';
import starTossupRouter from './star-tossup.js';
import tossupIdsRouter from './tossup-ids.js';
import tossupsRouter from './tossups.js';
import unstarBonusRouter from './unstar-bonus.js';
import unstarTossupRouter from './unstar-tossup.js';
Expand All @@ -24,11 +26,13 @@ router.use((req, res, next) => {
next();
});

router.use('/bonus-ids', bonusIdsRouter);
router.use('/bonuses', bonusesRouter);
router.use('/is-starred-bonus', isStarredBonusRouter);
router.use('/is-starred-tossup', isStarredTossupRouter);
router.use('/star-bonus', starBonusRouter);
router.use('/star-tossup', starTossupRouter);
router.use('/tossup-ids', tossupIdsRouter);
router.use('/tossups', tossupsRouter);
router.use('/unstar-bonus', unstarBonusRouter);
router.use('/unstar-tossup', unstarTossupRouter);
Expand Down
15 changes: 15 additions & 0 deletions routes/auth/stars/tossup-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getUserId from '../../../database/account-info/get-user-id.js';
import getTossupIds from '../../../database/account-info/stars/get-ids-tossup.js';

import { Router } from 'express';

const router = Router();

router.get('/', async (req, res) => {
const username = req.session.username;
const userId = await getUserId(username);
const ids = await getTossupIds(userId);
res.json(ids);
});

export default router;

0 comments on commit 413f87b

Please sign in to comment.