Created a recent endpoint for matches.

Added a field to record creation time of a match.

Additionally performed some minor refactoring.
This commit is contained in:
2022-04-01 11:16:24 -05:00
parent 19bbca36ca
commit 8a3de628a6
5 changed files with 37 additions and 13 deletions

View File

@@ -10,11 +10,12 @@ MatchController.get("/search/:sport", needDatabase, async (req, res) => {
try {
let sport = sportModel.findByName(req.params.sport);
let query = matchModel.find({ sport: sport._id });
query.where("dateTime").gte(Date.now); // We don't want to return any results of matches that have already occurred.
query.where("when").gte(Date.now); // We don't want to return any results of matches that have already occurred.
if (req.session.userId) query.where("publicity").gte(1).where("friends").in(req.session.userId);
if (req.query.within) query.where("location").within({ center: req.query.location.split(","), radius: req.query.within });
if (req.query.minDifficulty) query.where("difficulty").gte(req.query.minDifficulty);
if (req.query.maxDifficulty) query.where("difficulty").lte(req.query.maxDifficulty);
if (req.query.beforeDate) query.where("dateTime").lte(req.query.beforeDate);
if (req.query.beforeDate) query.where("when").lte(req.query.beforeDate);
let queryResults = await query;
res.send({ queryResults });
@@ -24,6 +25,27 @@ MatchController.get("/search/:sport", needDatabase, async (req, res) => {
}
});
MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
let limit = req.params.limit;
if (!req.params.limit) limit = 10;
if (isNaN(limit)) {
res.status(400).send("Limit parameter not a number.");
return;
}
if (limit > 50) {
res.status(400).send("Limit greater than maximum limit of 50.");
return;
}
try {
const recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 });
res.status(200).send({ recent: recent });
} catch (err) {
console.error(err);
res.status(500).send("Internal server error.");
// TODO: Check and improve error handling.
}
});
// TODO: delete, update match.
MatchController.post("/", needDatabase, authenticationGuard, async (req, res) => {
try {
@@ -31,7 +53,7 @@ MatchController.post("/", needDatabase, authenticationGuard, async (req, res) =>
const user = await userModel.findById(userId);
const match = new matchModel({
title: req.body.title,
dateTime: req.body.dateTime,
when: req.body.when,
public: req.body.public,
location: req.body.location,
creator: userId,