Added fetching of categories and questions
This commit is contained in:
78
src/games.js
78
src/games.js
@@ -88,6 +88,10 @@ export function initGames(app, db) {
|
||||
app.get('/walls/:gameid', fetchWalls);
|
||||
app.post('/wall', createWall);
|
||||
app.delete('/wall/:wallid', deleteWallRoute);
|
||||
|
||||
app.get('/category', fetchCategory);
|
||||
|
||||
app.get('/question', fetchQuestion);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,6 +186,80 @@ async function deleteGameRoute(req, res) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function fetchQuestion(req, res) {
|
||||
if (req.query.id === undefined || req.query.id.length <= 0) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
const id = new ObjectId(req.query.id);
|
||||
|
||||
let question = await cQuestions.findOne({
|
||||
_id: id,
|
||||
owner: req.user._id,
|
||||
});
|
||||
|
||||
if (question) {
|
||||
res.status(200).send(question);
|
||||
} else {
|
||||
res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function fetchCategory(req, res) {
|
||||
if (req.query.id === undefined || req.query.id.length <= 0) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
const id = new ObjectId(req.query.id);
|
||||
|
||||
let category = await cCategories.findOne({
|
||||
_id: id,
|
||||
owner: req.user._id,
|
||||
});
|
||||
|
||||
if (category) {
|
||||
let questions = await cQuestions
|
||||
.find(
|
||||
{
|
||||
_id: {
|
||||
$in: category.questions,
|
||||
},
|
||||
owner: req.user._id,
|
||||
},
|
||||
{
|
||||
projection: {
|
||||
_id: 1,
|
||||
points: 1,
|
||||
},
|
||||
},
|
||||
)
|
||||
.toArray();
|
||||
|
||||
if (questions.length !== 5) {
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
|
||||
category.questions = questions;
|
||||
|
||||
res.status(200).send(category);
|
||||
} else {
|
||||
res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
|
||||
Reference in New Issue
Block a user