From 37437152da955afe53a2ccba3e1ae6ea1ee0028e Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 14:57:18 +0200 Subject: [PATCH] Add the organisation controller + Add function to get all organisation + Add function to get a specific organisation --- src/Controllers/organisation.controller.ts | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/Controllers/organisation.controller.ts diff --git a/src/Controllers/organisation.controller.ts b/src/Controllers/organisation.controller.ts new file mode 100644 index 0000000..48c9cd9 --- /dev/null +++ b/src/Controllers/organisation.controller.ts @@ -0,0 +1,51 @@ +import { Request, Response } from "express"; +import prisma from "../lib/prisma"; +import { DataType, generateInvalidBodyError } from "./common"; + +export const getAllOrganisations = async (req: Request, res: Response) => { + const organisations = await prisma.organisation.findMany({ + select: { pid: true, name: true, event: { select: { pid: true, name: true } } }, + }); + + res.status(200).json({ + type: "success", + payload: { + organisations, + }, + }); +}; + +interface GetOrganisationQueryParams { + pid: string; +} + +export const getOrganisation = async (req: Request, res: Response) => { + const { pid } = req.params; + + const organisation = await prisma.organisation.findUnique({ + where: { pid }, + select: { pid: true, name: true, event: { select: { pid: true, date: true, name: true, description: true } } }, + }); + + if (!organisation) { + return res.status(404).json({ + type: "error", + payload: { + message: `The organisation with ID '${pid} could not be found'`, + }, + }); + } + + return res.status(200).json({ + type: "success", + payload: { + organisation: { + ...organisation, + event: { + ...organisation.event, + _links: [{ rel: "self", type: "GET", href: `/api/event/${organisation.event.pid}` }], + }, + }, + }, + }); +};