2022-04-01 10:36:10 +00:00
|
|
|
import express from "express";
|
2022-04-01 15:27:58 +00:00
|
|
|
import { authenticationGuard } from "../middleware/authority.js";
|
|
|
|
import { needDatabase } from "../middleware/database.js";
|
|
|
|
import sportModel from "../schemas/sportModel.js";
|
|
|
|
import userModel from "../schemas/userModel.js";
|
2022-04-01 10:36:10 +00:00
|
|
|
|
|
|
|
const SportController = express.Router();
|
|
|
|
|
|
|
|
SportController.post("/", needDatabase, authenticationGuard, async (req, res) => {
|
2022-04-01 15:27:58 +00:00
|
|
|
const user = await userModel.findById(req.session.userId);
|
2022-04-01 10:36:10 +00:00
|
|
|
try {
|
|
|
|
if (user.accessLevel <= 2) {
|
|
|
|
res.status(403).send("Insufficient privileges.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-01 15:27:58 +00:00
|
|
|
const sport = new sportModel({
|
2022-04-01 10:36:10 +00:00
|
|
|
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 {
|
2022-04-01 15:27:58 +00:00
|
|
|
res.status(200).send(await sportModel.findById(req.params.sportId));
|
2022-04-01 10:36:10 +00:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).send("Internal server error.");
|
|
|
|
// TODO: Add proper error checking here.
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
SportController.get("/", needDatabase, async (req, res) => {
|
|
|
|
try {
|
2022-04-01 15:27:58 +00:00
|
|
|
res.status(200).send(await sportModel.find());
|
2022-04-01 10:36:10 +00:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).send("Internal server error.");
|
|
|
|
// TODO: Add proper error checking here.
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default SportController;
|