Added U and D endpoints.
This commit is contained in:
@@ -46,7 +46,6 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: delete, update match.
|
||||
MatchController.post("/", needDatabase, authenticationGuard, async (req, res) => {
|
||||
try {
|
||||
const userId = req.session.userId;
|
||||
@@ -73,6 +72,47 @@ MatchController.post("/", needDatabase, authenticationGuard, async (req, res) =>
|
||||
}
|
||||
});
|
||||
|
||||
MatchController.patch("/:id", needDatabase, authenticationGuard, async (req, res) => {
|
||||
const match = await matchModel.findById(req.params.id);
|
||||
if (!match) {
|
||||
res.status(400).send("Invalid match ID provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.user._id !== match.creator && req.user.accessLevel < 3) {
|
||||
res.status(401).send("Not authorized.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.body._id) {
|
||||
res.status(400).send("Cannot change ID of match.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.body.creator) {
|
||||
res.status(400).send("Cannot change creator of match.");
|
||||
return;
|
||||
}
|
||||
|
||||
await match.updateOne(req.body);
|
||||
|
||||
res.status(200).send(match);
|
||||
});
|
||||
|
||||
MatchController.delete("/:id", needDatabase, authenticationGuard, async (req, res) => {
|
||||
const match = await matchModel.findById(req.params.id);
|
||||
if (!match) {
|
||||
res.status(400).send("Invalid match ID provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.user._id !== match.creator && req.user.accessLevel < 3) {
|
||||
res.status(401).send("Not authorized.");
|
||||
return;
|
||||
}
|
||||
await match.deleteOne();
|
||||
});
|
||||
|
||||
MatchController.get("/:matchId", needDatabase, async (req, res) => {
|
||||
if (!req.params.matchId) {
|
||||
res.status(404).send("Id must be provided to retrieve match");
|
||||
@@ -91,4 +131,51 @@ MatchController.get("/:matchId", needDatabase, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
MatchController.get("/join/:id", needDatabase, authenticationGuard, async (req, res) => {
|
||||
const match = await matchModel.findById(req.params.id);
|
||||
const user = req.user;
|
||||
if (!match) {
|
||||
res.status(400).send("Invalid match ID provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.participatingMatches.includes(match._id)) {
|
||||
res.status(400).send("Already participating in match.");
|
||||
return;
|
||||
}
|
||||
|
||||
match.participants.push(user._id);
|
||||
user.participatingMatches.push(match._id);
|
||||
|
||||
await match.save();
|
||||
await user.save();
|
||||
|
||||
res.status(200).send("Joined.");
|
||||
});
|
||||
|
||||
MatchController.get("/leave/:id", needDatabase, authenticationGuard, async (req, res) => {
|
||||
const match = await matchModel.findById(req.params.id);
|
||||
const user = req.user;
|
||||
|
||||
if (!match) {
|
||||
res.status(400).send("Invalid match ID provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.participatingMatches.includes(match._id)) {
|
||||
res.status(400).send("Not part of match.");
|
||||
return;
|
||||
}
|
||||
|
||||
const userIndex = match.participants.indexOf(user._id);
|
||||
match.participants.splice(userIndex, 1);
|
||||
await match.save();
|
||||
|
||||
const matchIndex = user.participatingMatches.indexOf(match._id);
|
||||
user.participatingMatches.splice(matchIndex, 1);
|
||||
await user.save();
|
||||
|
||||
res.status(200).send("Left match.");
|
||||
});
|
||||
|
||||
export default MatchController;
|
||||
Reference in New Issue
Block a user