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:
Harrison Deng 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,

View File

@ -1,12 +1,12 @@
import mongoose from "mongoose";
import ModelNameRegister from "./ModelNameRegister.js";
import ModelNameRegister from "./modelNameRegister.js";
const Types = mongoose.Schema.Types; // Some types require defining from this object.
const matchSchema = new mongoose.Schema({
title: { type: String, required: true, trim: true },
dateTime: { type: Date, required: true },
public: { type: Boolean, required: true, default: true },
when: { type: Date, required: true },
publicity: { type: Number, required: true, default: 2 },
location: {
type: [Number],
required: true,
@ -14,13 +14,14 @@ const matchSchema = new mongoose.Schema({
validator: function (v) {
return v.length === 2;
},
message: "Invalid coordinate format (array not length of 2)"
message: "Invalid coordinate format (array not length of 2)."
}
},
creator: { type: Types.ObjectId, ref: ModelNameRegister.User },
participants: { type: [{ type: Types.ObjectId, ref: ModelNameRegister.User }], required: true, default: [] },
difficulty: { type: Number, required: true },
sport: { type: Types.ObjectId, ref: ModelNameRegister.Sport }
sport: { type: Types.ObjectId, ref: ModelNameRegister.Sport },
createDate: { type: Date, required: true, default: Date.now }
});
export default mongoose.model(ModelNameRegister.Match, matchSchema);

View File

@ -1,5 +1,5 @@
import mongoose from "mongoose";
import ModelNameRegister from "./ModelNameRegister.js";
import ModelNameRegister from "./modelNameRegister.js";
const sportSchema = new mongoose.Schema({
name: { type: String, required: true, unique: true, trim: true },

View File

@ -1,7 +1,7 @@
import mongoose from "mongoose";
import validator from "validator";
import bcrypt from "bcrypt";
import ModelNameRegister from "./ModelNameRegister.js";
import modelNameRegister from "./modelNameRegister.js";
const Types = mongoose.Schema.Types;
@ -27,12 +27,13 @@ const userSchema = new mongoose.Schema({
minlength: 8
// TODO: Custom validator for password requirements?
},
createdMatches: { type: [{ type: Types.ObjectId, ref: ModelNameRegister.Match }], required: true, default: [] },
participatingMatches: { type: [{ type: Types.ObjectId, ref: ModelNameRegister.Match }], required: true, default: [] },
createdMatches: { type: [{ type: Types.ObjectId, ref: modelNameRegister.Match }], required: true, default: [] },
participatingMatches: { type: [{ type: Types.ObjectId, ref: modelNameRegister.Match }], required: true, default: [] },
emailPublicity: { type: Number, required: true, default: 0 },
bioPublicity: { type: Boolean, required: true, default: false },
phonePublicity: { type: Boolean, required: true, default: false },
participatingMatchesPublicity: { type: Boolean, required: true, default: false },
friends: { type: Types.ObjectId, ref: modelNameRegister.User },
accessLevel: { type: Number, required: true, default: 0 },
});
@ -64,4 +65,4 @@ userSchema.pre("save", function (next) {
}
});
export default mongoose.model(ModelNameRegister.User, userSchema);
export default mongoose.model(modelNameRegister.User, userSchema);