feat: endpoint voor alle groepen van een assignment geimplementeerd

This commit is contained in:
Adriaan Jacquet 2025-03-09 13:39:53 +01:00
parent 3f62ab70e1
commit 7e051d412a
4 changed files with 62 additions and 13 deletions

View file

@ -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<void> {
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,
});
}

View file

@ -18,7 +18,11 @@ export class GroupRepository extends DwengoEntityRepository<Group> {
public findAllGroupsForAssignment(
assignment: Assignment
): Promise<Group[]> {
return this.findAll({ where: { assignment: assignment } });
return this.findAll({
where: { assignment: assignment },
populate: ["members"]
},
);
}
public deleteByAssignmentAndGroupNumber(
assignment: Assignment,

View file

@ -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);

View file

@ -33,4 +33,35 @@ export async function getGroup(
}
return mapToGroupDTOId(group);
}
export async function getAllGroups(
classId: string,
assignmentNumber: number,
full: boolean,
): Promise<GroupDTO[]> {
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);
}