Added Event Endpoints; Dotenv; npm Scripts

+ GET    /events/
+ POST /events/
This commit is contained in:
Stefan-5422
2021-10-17 02:30:01 +02:00
parent 4b0615ac27
commit a966bfffd9
8 changed files with 107 additions and 17 deletions
+40
View File
@@ -0,0 +1,40 @@
import { Request, Response } from "express";
import prisma from "../lib/prisma";
const getAllEvents = async (req: Request, res: Response) => {
const events = await prisma.event.findMany({
select: {
name: true,
date: true,
pid: true,
id: false,
},
});
if (events.length > 0) res.status(200).send(events);
else res.status(204).send();
};
const addEvent = async (req: Request, res: Response) => {
// TODO: Add checks if user has admin permissions
if (req.body.name == null || undefined || req.body.date == null || undefined) {
res.status(400).send("Malformed request");
return;
}
const event = await prisma.event.create({
data: {
name: req.body.name,
date: req.body.date,
},
select: {
name: true,
date: true,
pid: true,
id: false,
},
});
res.status(201).send(event);
};
export { getAllEvents, addEvent };