48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
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";
|
|
|
|
const SportController = express.Router();
|
|
|
|
SportController.post("/", needDatabase, authenticationGuard, async (req, res) => {
|
|
const user = await User.findById(req.session.userId);
|
|
try {
|
|
if (user.accessLevel <= 2) {
|
|
res.status(403).send("Insufficient privileges.");
|
|
return;
|
|
}
|
|
const sport = new Sport({
|
|
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 Sport.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 Sport.find());
|
|
} catch (error) {
|
|
res.status(500).send("Internal server error.");
|
|
// TODO: Add proper error checking here.
|
|
}
|
|
});
|
|
|
|
export default SportController; |