diff --git a/sports-matcher/server/controllers/matchController.js b/sports-matcher/server/controllers/matchController.js index e2cf3eb..4c9f98f 100644 --- a/sports-matcher/server/controllers/matchController.js +++ b/sports-matcher/server/controllers/matchController.js @@ -34,13 +34,6 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => { res.status(400).send("Limit parameter is not a number."); 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)) { res.status(400).send("Limit parameter not a number."); return; @@ -49,8 +42,15 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => { res.status(400).send("Limit greater than maximum limit of 50."); return; } + let recent = null; 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 }); } catch (err) { console.error(err); @@ -108,7 +108,6 @@ MatchController.patch("/:id", needDatabase, authenticationGuard, async (req, res } await match.updateOne(req.body); - res.status(200).send(match); }); @@ -124,6 +123,7 @@ MatchController.delete("/:id", needDatabase, authenticationGuard, async (req, re return; } await match.deleteOne(); + res.status(200).send("Deleted."); }); MatchController.get("/:id", needDatabase, async (req, res) => {