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:
48
sports-matcher/server/controllers/sportController.js
Normal file
48
sports-matcher/server/controllers/sportController.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import express from "express";
|
||||
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 userModel.findById(req.session.userId);
|
||||
try {
|
||||
if (user.accessLevel <= 2) {
|
||||
res.status(403).send("Insufficient privileges.");
|
||||
return;
|
||||
}
|
||||
const sport = new sportModel({
|
||||
name: req.body.name,
|
||||
maxPlayers: req.body.maxPlayers,
|
||||
minPlayers: req.body.minPlayers,
|
||||
description: req.body.description
|
||||
});
|
||||
await sport.save();
|
||||
res.status(201).send("Successfully created new sport.");
|
||||
} catch (error) {
|
||||
res.status(500).send("Internal server error.");
|
||||
// TODO: Add proper error checking here.
|
||||
}
|
||||
});
|
||||
|
||||
SportController.get("/:sportId", needDatabase, async (req, res) => {
|
||||
try {
|
||||
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.
|
||||
}
|
||||
});
|
||||
|
||||
SportController.get("/", needDatabase, async (req, res) => {
|
||||
try {
|
||||
res.status(200).send(await sportModel.find());
|
||||
} catch (error) {
|
||||
res.status(500).send("Internal server error.");
|
||||
// TODO: Add proper error checking here.
|
||||
}
|
||||
});
|
||||
|
||||
export default SportController;
|
||||
Reference in New Issue
Block a user