diff --git a/backend/src/controllers/groups.ts b/backend/src/controllers/groups.ts index e484c409..8b26b810 100644 --- a/backend/src/controllers/groups.ts +++ b/backend/src/controllers/groups.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express'; -import { getAllGroups, getGroup } from '../services/groups.js'; +import { getAllGroups, getGroup, getGroupSubmissions } from '../services/groups.js'; // Typescript is annoywith with parameter forwarding from class.ts interface GroupParams { @@ -53,3 +53,31 @@ export async function getAllGroupsHandler( groups: groups, }); } + +export async function getGroupSubmissionsHandler( + 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 groupId = +req.params.groupid!; // Can't be undefined + + if (isNaN(groupId)) { + res.status(400).json({ error: 'Group id must be a number' }); + return; + } + + const submissions = await getGroupSubmissions(classId, assignmentId, groupId); + + res.json({ + submissions: submissions, + }); +} \ No newline at end of file diff --git a/backend/src/routes/groups.ts b/backend/src/routes/groups.ts index d604c088..d17a330e 100644 --- a/backend/src/routes/groups.ts +++ b/backend/src/routes/groups.ts @@ -1,5 +1,5 @@ import express from 'express'; -import { getAllGroupsHandler, getGroupHandler } from '../controllers/groups.js'; +import { getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../controllers/groups.js'; const router = express.Router({ mergeParams: true }); @@ -9,6 +9,8 @@ router.get('/', getAllGroupsHandler); // Information about a group (members, ... [TODO DOC]) router.get('/:groupid', getGroupHandler); +router.get('/:groupid', getGroupSubmissionsHandler); + // The list of questions a group has made router.get('/:id/question', (req, res) => { res.json({ diff --git a/backend/src/services/groups.ts b/backend/src/services/groups.ts index 467f90a3..a2af8e4a 100644 --- a/backend/src/services/groups.ts +++ b/backend/src/services/groups.ts @@ -2,12 +2,14 @@ import { getAssignmentRepository, getClassRepository, getGroupRepository, + getSubmissionRepository, } from '../data/repositories.js'; import { GroupDTO, mapToGroupDTO, mapToGroupDTOId, } from '../interfaces/group.js'; +import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; export async function getGroup( classId: string, @@ -82,3 +84,41 @@ export async function getAllGroups( return groups.map(mapToGroupDTOId); } + +export async function getGroupSubmissions( + classId: string, + assignmentNumber: number, + groupNumber: number, +): 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 group = await groupRepository.findByAssignmentAndGroupNumber( + assignment, + groupNumber + ); + + if (!group) { + return []; + } + + const submissionRepository = getSubmissionRepository(); + const submissions = await submissionRepository.findAllSubmissionsForGroup(group); + + return submissions.map(mapToSubmissionDTO); +}