From 831017e7c23fbd4b47af133e9f784f6a0af30c7b Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 16:13:32 +0200 Subject: [PATCH] Add routes for organisations + FIX: Change eventId type --- src/Controllers/organisation.controller.ts | 18 +++++++++++++---- src/Routes/organisation.routes.ts | 23 ++++++++++++++++++++++ src/app.ts | 3 +++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/Routes/organisation.routes.ts diff --git a/src/Controllers/organisation.controller.ts b/src/Controllers/organisation.controller.ts index 559cc8d..1987ee9 100644 --- a/src/Controllers/organisation.controller.ts +++ b/src/Controllers/organisation.controller.ts @@ -3,9 +3,9 @@ import { Request, Response } from "express"; import prisma from "../lib/prisma"; import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common"; -export const _getAllOrganisations = async (res: Response, eventId: number | undefined = undefined) => { +export const _getAllOrganisations = async (res: Response, eventId: string | undefined = undefined) => { const organisations = await prisma.organisation.findMany({ - where: { eventId }, + where: { event: { pid: eventId } }, select: { pid: true, name: true, event: { select: { pid: true, name: true } } }, }); @@ -22,9 +22,19 @@ interface GetAllOrganisationsSearchParams { } export const getAllOrganisations = async (req: Request<{}, {}, {}, GetAllOrganisationsSearchParams>, res: Response) => { - const eventId = parseInt(req.query.eventId || ""); + // TODO: Maybe rename to eventPid + return _getAllOrganisations(res, req.query.eventId); +}; - return _getAllOrganisations(res, isNaN(eventId) ? undefined : eventId); +interface GetAllOrganisationsWithParamQueryParams { + eventPid: string; +} + +export const getAllOrganisationsWithParam = async ( + req: Request, + res: Response +) => { + return _getAllOrganisations(res, req.params.eventPid); }; interface GetOrganisationQueryParams { diff --git a/src/Routes/organisation.routes.ts b/src/Routes/organisation.routes.ts new file mode 100644 index 0000000..19a25a7 --- /dev/null +++ b/src/Routes/organisation.routes.ts @@ -0,0 +1,23 @@ +import express from "express"; +import eventRouter from "./event.routes"; +import { + createOrganisation, + getAllOrganisations, + getAllOrganisationsWithParam, + getOrganisation, +} from "../Controllers/organisation.controller"; +import { requireAuthentication } from "../Middleware/auth/auth"; + +const router = express.Router(); + +router.get("/", getAllOrganisations); +router.get("/:pid", getOrganisation); + +eventRouter.get("/:eventPid/organisations", getAllOrganisationsWithParam); +eventRouter.post<"/:eventPid/organisations", { eventPid: string }>( + "/:eventPid/organisations", + requireAuthentication, + createOrganisation +); + +export default router; diff --git a/src/app.ts b/src/app.ts index 6af2048..0c5c408 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,7 @@ import eventRouter from "./Routes/event.routes"; import adminAuthRouter from "./Routes/admin_auth.routes"; import argon2 from "argon2"; import adminRouter from "./Routes/admin.routes"; +import organisationRouter from "./Routes/organisation.routes"; require("dotenv").config(); // Load dotenv config @@ -46,6 +47,8 @@ async function main() { app.use("/api/admins", adminRouter); + app.use("/api/organisations", organisationRouter); + app.listen(process.env.PORT, () => { console.log(`Listening on Port: ${process.env.PORT}`); });