Recent match endpoint now populates all references.

This commit is contained in:
Harrison Deng 2022-04-05 03:41:20 -05:00
parent d17fe1d912
commit 6cedd74473

View File

@ -34,13 +34,6 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
res.status(400).send("Limit parameter is not a number."); res.status(400).send("Limit parameter is not a number.");
return; return;
} }
if (user) {
await user.populate("participatingMatches");
await user.populate("participatingMatches.participants");
await user.populate("participatingMatches.sport");
res.status(200).send();
return;
}
if (isNaN(limit)) { if (isNaN(limit)) {
res.status(400).send("Limit parameter not a number."); res.status(400).send("Limit parameter not a number.");
return; return;
@ -49,8 +42,15 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
res.status(400).send("Limit greater than maximum limit of 50."); res.status(400).send("Limit greater than maximum limit of 50.");
return; return;
} }
let recent = null;
try { try {
const recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 }).populate("participants").populate("sport"); if (user) {
await user.populate("participatingMatches").populate("participatingMatches.participants").populate("participatingMatches.sport");
recent = user.participatingMatches;
} else {
recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 });
}
await recent.populate("members.$"); // Populates all references.
res.status(200).send({ recent: recent }); res.status(200).send({ recent: recent });
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -108,7 +108,6 @@ MatchController.patch("/:id", needDatabase, authenticationGuard, async (req, res
} }
await match.updateOne(req.body); await match.updateOne(req.body);
res.status(200).send(match); res.status(200).send(match);
}); });
@ -124,6 +123,7 @@ MatchController.delete("/:id", needDatabase, authenticationGuard, async (req, re
return; return;
} }
await match.deleteOne(); await match.deleteOne();
res.status(200).send("Deleted.");
}); });
MatchController.get("/:id", needDatabase, async (req, res) => { MatchController.get("/:id", needDatabase, async (req, res) => {