Rewrite phase 1.
Started improved client code structure. Implemented session based authentication serverside. Implemented user, match, and sport database models serverside. Implemented Controllers for variety of C and R operations of CRUD.
This commit is contained in:
94
sports-matcher/server/controllers/matchController.js
Normal file
94
sports-matcher/server/controllers/matchController.js
Normal file
@@ -0,0 +1,94 @@
|
||||
import express from "express";
|
||||
import { authenticationGuard } from "../middleware/authority.js";
|
||||
import { needDatabase } from "../middleware/database.js";
|
||||
import matchModel from "../schemas/matchModel.js";
|
||||
import sportModel from "../schemas/sportModel.js";
|
||||
import userModel from "../schemas/userModel.js";
|
||||
const MatchController = express.Router();
|
||||
|
||||
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("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("when").lte(req.query.beforeDate);
|
||||
|
||||
let queryResults = await query;
|
||||
res.send({ queryResults });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Internal server error.");
|
||||
}
|
||||
});
|
||||
|
||||
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 {
|
||||
const userId = req.session.userId;
|
||||
const user = await userModel.findById(userId);
|
||||
const match = new matchModel({
|
||||
title: req.body.title,
|
||||
when: req.body.when,
|
||||
public: req.body.public,
|
||||
location: req.body.location,
|
||||
creator: userId,
|
||||
difficulty: req.body.difficulty,
|
||||
sport: await sportModel.findByName(req.body.sport),
|
||||
participants: [user._id]
|
||||
});
|
||||
await match.save();
|
||||
user.createdMatches.push(match._id);
|
||||
user.participatingMatches.push(match._id);
|
||||
await user.save();
|
||||
res.status(201).send(match);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Internal server error.");
|
||||
// TODO: Develop the error handling.
|
||||
}
|
||||
});
|
||||
|
||||
MatchController.get("/:matchId", needDatabase, async (req, res) => {
|
||||
if (!req.params.matchId) {
|
||||
res.status(404).send("Id must be provided to retrieve match");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const match = await matchModel.findById(req.params.matchId);
|
||||
if (match) {
|
||||
res.status(200).send(match);
|
||||
} else {
|
||||
res.status(404).send("Could not find match with ID: " + req.params.matchId);
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send("Internal server error.");
|
||||
// TODO: Develop the error handling.
|
||||
}
|
||||
});
|
||||
|
||||
export default MatchController;
|
||||
Reference in New Issue
Block a user