From 0c65aec96cea81e19a800650655da4af46e9d19c Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 21:28:47 +0200 Subject: [PATCH] Add controller and routes for groups --- src/Controllers/group.controllers.ts | 92 ++++++++++++++++++++++++++++ src/Routes/group.routes.ts | 12 ++++ src/app.ts | 3 + 3 files changed, 107 insertions(+) create mode 100644 src/Controllers/group.controllers.ts create mode 100644 src/Routes/group.routes.ts diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts new file mode 100644 index 0000000..b9ba189 --- /dev/null +++ b/src/Controllers/group.controllers.ts @@ -0,0 +1,92 @@ +import { PrismaClientUnknownRequestError } from "@prisma/client/runtime"; +import { Request, Response } from "express"; +import prisma from "../lib/prisma"; +import { generateError, genericError } from "./common"; + +const basicGroup = { + pid: true, + name: true, + oragnisation: { select: { pid: true, name: true } }, +} as const; + +export const _getAllGroups = async (res: Response, organisationId: string | undefined) => { + const groups = await prisma.group.findMany({ + where: { oragnisation: { pid: organisationId } }, + select: basicGroup, + }); + + return res.status(200).json({ + type: "success", + payload: { + groups, + }, + }); +}; + +interface GetAllGroupsSearchParams { + organisationPid?: string; +} + +export const getAllGroups = async (req: Request<{}, {}, {}, GetAllGroupsSearchParams>, res: Response) => { + return _getAllGroups(res, req.query.organisationPid); +}; + +interface getAllGroupsWithParamQueryParams { + organisationPid: string; +} + +export const getAllGroupsWithParam = async (req: Request, res: Response) => { + return _getAllGroups(res, req.params.organisationPid); +}; + +interface GetGroupQueryParams { + pid: string; +} + +export const getGroup = async (req: Request, res: Response) => { + const { pid } = req.params; + + try { + const group = await prisma.group.findUnique({ + where: { pid }, + select: { + pid: true, + name: true, + oragnisation: { select: { pid: true, name: true } }, + admins: { select: { pid: true, name: true } }, + participants: { select: { pid: true } }, + }, + }); + + if (!group) { + return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`)); + } + + return res.status(200).json({ + type: "success", + payload: { + group: { + ...group, + organisation: { + ...group.oragnisation, + _links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.oragnisation.pid}` }], + }, + admins: group.admins.map((admin) => ({ + ...admin, + _links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }], + })), + participants: group.participants.map((participant) => ({ + ...participant, + _links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }], + })), + }, + }, + }); + } catch (e) { + if (e instanceof PrismaClientUnknownRequestError) { + return res.status(400).json(generateError("Unknown error occured. This could be due to malformed IDs")); + } + } + + return res.status(500).json(genericError); +}; diff --git a/src/Routes/group.routes.ts b/src/Routes/group.routes.ts new file mode 100644 index 0000000..54e43a0 --- /dev/null +++ b/src/Routes/group.routes.ts @@ -0,0 +1,12 @@ +import express from "express"; +import { getAllGroups, getAllGroupsWithParam, getGroup } from "../Controllers/group.controllers"; +import organisationRouter from "./organisation.routes"; + +const router = express.Router(); + +router.get("/", getAllGroups); +router.get("/:pid", getGroup); + +organisationRouter.get("/:organisationPid/groups", getAllGroupsWithParam); + +export default router; diff --git a/src/app.ts b/src/app.ts index 0c5c408..bb3b56b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,6 +5,7 @@ import adminAuthRouter from "./Routes/admin_auth.routes"; import argon2 from "argon2"; import adminRouter from "./Routes/admin.routes"; import organisationRouter from "./Routes/organisation.routes"; +import groupRouter from "./Routes/group.routes"; require("dotenv").config(); // Load dotenv config @@ -49,6 +50,8 @@ async function main() { app.use("/api/organisations", organisationRouter); + app.use("/api/groups", groupRouter); + app.listen(process.env.PORT, () => { console.log(`Listening on Port: ${process.env.PORT}`); });