Welcome page now shows current matches.

This commit is contained in:
2022-04-05 03:28:12 -05:00
parent f8abf7cd48
commit c1589b9758
8 changed files with 68 additions and 40 deletions

View File

@@ -27,13 +27,18 @@ MatchController.get("/search/:sport", needDatabase, async (req, res) => {
MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
const user = req.user;
let limit = req.params.limit;
if (limit && typeof (limit) !== "number") {
res.status(400).send("Limit parameter is not a number.");
}
let limit = parseInt(req.params.limit);
if (!req.params.limit) limit = 10;
if (isNaN(limit)) {
console.log(typeof (limit));
res.status(400).send("Limit parameter is not a number.");
return;
}
if (user) {
res.status(200).send(user.participatingMatches.slice(limit));
await user.populate("participatingMatches");
await user.populate("participatingMatches.participants");
await user.populate("participatingMatches.sport");
res.status(200).send();
return;
}
if (isNaN(limit)) {
@@ -45,7 +50,7 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
return;
}
try {
const recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 });
const recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 }).populate("participants").populate("sport");
res.status(200).send({ recent: recent });
} catch (err) {
console.error(err);
@@ -121,17 +126,17 @@ MatchController.delete("/:id", needDatabase, authenticationGuard, async (req, re
await match.deleteOne();
});
MatchController.get("/:matchId", needDatabase, async (req, res) => {
if (!req.params.matchId) {
MatchController.get("/:id", needDatabase, async (req, res) => {
if (!req.params.id) {
res.status(404).send("Id must be provided to retrieve match");
return;
}
try {
const match = await matchModel.findById(req.params.matchId);
const match = await matchModel.findById(req.params.id).populate("sport");
if (match) {
res.status(200).send(match);
} else {
res.status(404).send("Could not find match with ID: " + req.params.matchId);
res.status(404).send("Could not find match with ID: " + req.params.id);
}
} catch (error) {
res.status(500).send("Internal server error.");