Refactored schemas, naming, and instantiation files.

This commit is contained in:
2022-04-01 10:27:58 -05:00
parent 8231f9db67
commit 19bbca36ca
12 changed files with 77 additions and 99 deletions

View File

@@ -1,15 +1,15 @@
import express from "express";
import { authenticationGuard } from "../middleware/Authority.js";
import { needDatabase } from "../middleware/Database.js";
import Match from "../schemas/Match.js";
import Sport from "../schemas/Sport.js";
import User from "../schemas/User.js";
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 = Sport.findByName(req.params.sport);
let query = Match.find({ sport: sport._id });
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.
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);
@@ -28,19 +28,20 @@ MatchController.get("/search/:sport", needDatabase, async (req, res) => {
MatchController.post("/", needDatabase, authenticationGuard, async (req, res) => {
try {
const userId = req.session.userId;
const match = new Match({
const user = await userModel.findById(userId);
const match = new matchModel({
title: req.body.title,
dateTime: req.body.dateTime,
public: req.body.public,
location: req.body.location,
creator: userId,
difficulty: req.body.difficulty,
sport: await Sport.findByName(req.body.sport)
sport: await sportModel.findByName(req.body.sport),
participants: [user._id]
});
await match.save();
const user = await User.findById(userId);
user.createdMatches.push(match._id);
user.participatingMatches.value.push(match._id);
user.participatingMatches.push(match._id);
await user.save();
res.status(201).send(match);
} catch (error) {
@@ -56,7 +57,7 @@ MatchController.get("/:matchId", needDatabase, async (req, res) => {
return;
}
try {
const match = await Match.findById(req.params.matchId);
const match = await matchModel.findById(req.params.matchId);
if (match) {
res.status(200).send(match);
} else {

View File

@@ -1,19 +1,19 @@
import express from "express";
import { authenticationGuard } from "../middleware/Authority.js";
import { needDatabase } from "../middleware/Database.js";
import Sport from "../schemas/Sport.js";
import User from "../schemas/User.js";
import { authenticationGuard } from "../middleware/authority.js";
import { needDatabase } from "../middleware/database.js";
import sportModel from "../schemas/sportModel.js";
import userModel from "../schemas/userModel.js";
const SportController = express.Router();
SportController.post("/", needDatabase, authenticationGuard, async (req, res) => {
const user = await User.findById(req.session.userId);
const user = await userModel.findById(req.session.userId);
try {
if (user.accessLevel <= 2) {
res.status(403).send("Insufficient privileges.");
return;
}
const sport = new Sport({
const sport = new sportModel({
name: req.body.name,
maxPlayers: req.body.maxPlayers,
minPlayers: req.body.minPlayers,
@@ -29,7 +29,7 @@ SportController.post("/", needDatabase, authenticationGuard, async (req, res) =>
SportController.get("/:sportId", needDatabase, async (req, res) => {
try {
res.status(200).send(await Sport.findById(req.params.sportId));
res.status(200).send(await sportModel.findById(req.params.sportId));
} catch (error) {
res.status(500).send("Internal server error.");
// TODO: Add proper error checking here.
@@ -38,7 +38,7 @@ SportController.get("/:sportId", needDatabase, async (req, res) => {
SportController.get("/", needDatabase, async (req, res) => {
try {
res.status(200).send(await Sport.find());
res.status(200).send(await sportModel.find());
} catch (error) {
res.status(500).send("Internal server error.");
// TODO: Add proper error checking here.

View File

@@ -1,7 +1,7 @@
import express from "express";
import { authenticationGuard } from "../middleware/Authority.js";
import { needDatabase } from "../middleware/Database.js";
import User from "../schemas/User.js";
import { authenticationGuard } from "../middleware/authority.js";
import { needDatabase } from "../middleware/database.js";
import User from "../schemas/userModel.js";
const UserController = express.Router();
UserController.post("/login", needDatabase, async (req, res) => {
@@ -14,7 +14,7 @@ UserController.post("/login", needDatabase, async (req, res) => {
return;
} else {
req.session.userId = user._id;
req.session.email = user.email.value;
req.session.email = user.email;
res.status(200).send("Authenticated.");
}
} catch (error) {
@@ -54,7 +54,7 @@ UserController.get("/email/:userId?", needDatabase, authenticationGuard, async (
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (selUser.email.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ email: selUser.email.value });
res.status(200).send({ email: selUser.email });
} else {
res.status(401).send("Could not authenticate request.");
}
@@ -65,7 +65,7 @@ UserController.get("/firstName/:userId?", needDatabase, authenticationGuard, asy
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (selUser.firstName.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ firstName: selUser.firstName.value });
res.status(200).send({ firstName: selUser.firstName });
} else {
res.status(401).send("Could not authenticate request.");
}
@@ -76,7 +76,7 @@ UserController.get("/lastName/:userId?", needDatabase, authenticationGuard, asyn
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (selUser.lastName.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ email: selUser.lastName.value });
res.status(200).send({ email: selUser.lastName });
} else {
res.status(401).send("Could not authenticate request.");
}
@@ -87,7 +87,7 @@ UserController.get("/phone/:userId?", needDatabase, authenticationGuard, async (
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (selUser.phone.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ phone: selUser.phone.value });
res.status(200).send({ phone: selUser.phone });
} else {
res.status(401).send("Could not authenticate request.");
}
@@ -95,10 +95,10 @@ UserController.get("/phone/:userId?", needDatabase, authenticationGuard, async (
UserController.get("/participatingMatches/:userId?", needDatabase, authenticationGuard, async (req, res) => {
if (!req.params.userId) req.params.userId = req.session.userId;
const curUser = await User.findById(req.session.userId).populate("participatingMatches.value");
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (selUser.participatingMatches.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ participatingMatches: selUser.participatingMatches.value });
res.status(200).send({ participatingMatches: selUser.participatingMatches });
} else {
res.status(401).send("Could not authenticate request.");
}
@@ -117,7 +117,7 @@ UserController.get("/joinDate/:userId?", needDatabase, authenticationGuard, asyn
UserController.get("/createdMatches/:userId?", needDatabase, authenticationGuard, async (req, res) => {
if (!req.params.userId) req.params.userId = req.session.userId;
const curUser = await User.findById(req.session.userId).populate("createdMatches");
const curUser = await User.findById(req.session.userId);
const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
if (curUser._id === selUser._id || curUser.accessLevel > 2) {
res.status(200).send({ createdMatches: selUser.createdMatches });
@@ -139,6 +139,7 @@ UserController.post("/", needDatabase, async (req, res) => {
});
await createdUser.save();
res.sendStatus(201);
return;
} catch (err) {
if (err.name === "TypeError" || err.name === "ValidationError") {
if (process.env.NODE_ENV === "development") {