From 7e051d412a92bb8113d76c792f8eb984b342ad07 Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Sun, 9 Mar 2025 13:39:53 +0100 Subject: [PATCH] feat: endpoint voor alle groepen van een assignment geimplementeerd --- backend/src/controllers/groups.ts | 27 ++++++++++++++-- .../src/data/assignments/group-repository.ts | 6 +++- backend/src/routes/group.ts | 11 ++----- backend/src/services/groups.ts | 31 +++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/backend/src/controllers/groups.ts b/backend/src/controllers/groups.ts index 7ca6617d..1c523744 100644 --- a/backend/src/controllers/groups.ts +++ b/backend/src/controllers/groups.ts @@ -1,11 +1,11 @@ import { Request, Response } from 'express'; -import { getGroup } from '../services/groups'; +import { getAllGroups, getGroup } from '../services/groups'; // typescript is annoywith with parameter forwarding from class.ts interface GroupParams { classid: string; assignmentid: string; - groupid: string; + groupid?: string; } export async function getGroupHandler( @@ -21,7 +21,7 @@ export async function getGroupHandler( return; } - const groupId = +req.params.groupid; + const groupId = +req.params.groupid!; // can't be undefined if (isNaN(groupId)) { res.status(400).json({ error: "Group id must be a number" }); @@ -31,4 +31,25 @@ export async function getGroupHandler( const group = await getGroup(classId, assignmentId, groupId, full); res.json(group); +} + +export async function getAllGroupsHandler( + req: Request, + res: Response, +): Promise { + const classId = req.params.classid; + const full = req.query.full === "true"; + + const assignmentId = +req.params.assignmentid; + + if (isNaN(assignmentId)) { + res.status(400).json({ error: "Assignment id must be a number" }); + return; + } + + const groups = await getAllGroups(classId, assignmentId, full); + + res.json({ + groups: groups, + }); } \ No newline at end of file diff --git a/backend/src/data/assignments/group-repository.ts b/backend/src/data/assignments/group-repository.ts index 3992e113..c8770eb5 100644 --- a/backend/src/data/assignments/group-repository.ts +++ b/backend/src/data/assignments/group-repository.ts @@ -18,7 +18,11 @@ export class GroupRepository extends DwengoEntityRepository { public findAllGroupsForAssignment( assignment: Assignment ): Promise { - return this.findAll({ where: { assignment: assignment } }); + return this.findAll({ + where: { assignment: assignment }, + populate: ["members"] + }, + ); } public deleteByAssignmentAndGroupNumber( assignment: Assignment, diff --git a/backend/src/routes/group.ts b/backend/src/routes/group.ts index 436e228a..a8d4dbe5 100644 --- a/backend/src/routes/group.ts +++ b/backend/src/routes/group.ts @@ -1,16 +1,9 @@ import express from 'express' -import { getGroupHandler } from '../controllers/groups'; +import { getAllGroupsHandler, getGroupHandler } from '../controllers/groups'; const router = express.Router({ mergeParams: true }); // root endpoint used to search objects -router.get('/', (req, res) => { - res.json({ - groups: [ - '0', - '1', - ] - }); -}); +router.get('/', getAllGroupsHandler); // information about a group (members, ... [TODO DOC]) router.get('/:groupid', getGroupHandler); diff --git a/backend/src/services/groups.ts b/backend/src/services/groups.ts index c1b6cf33..c9a4024e 100644 --- a/backend/src/services/groups.ts +++ b/backend/src/services/groups.ts @@ -33,4 +33,35 @@ export async function getGroup( } return mapToGroupDTOId(group); +} + +export async function getAllGroups( + classId: string, + assignmentNumber: number, + full: boolean, +): Promise { + const classRepository = getClassRepository(); + const cls = await classRepository.findById(classId); + + if (!cls) { + return []; + } + + const assignmentRepository = getAssignmentRepository(); + const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); + + if (!assignment) { + return []; + } + + const groupRepository = getGroupRepository(); + const groups = await groupRepository.findAllGroupsForAssignment(assignment); + + if (full) { + console.log('full'); + console.log(groups); + return groups.map(mapToGroupDTO); + } + + return groups.map(mapToGroupDTOId); } \ No newline at end of file