From e0a5596994770305c79aa3859d696f8b7e9d4140 Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Sun, 9 Mar 2025 15:31:10 +0100 Subject: [PATCH] feat: alle assignments en student's assignments geimplementeerd --- backend/src/controllers/assignments.ts | 18 ++++++++++++++++-- backend/src/controllers/students.ts | 20 ++++++++++++++++++++ backend/src/interfaces/assignments.ts | 22 +++++++++++++++++----- backend/src/routes/assignment.ts | 11 ++--------- backend/src/routes/student.ts | 8 ++------ backend/src/services/assignments.ts | 22 ++++++++++++++++++++-- 6 files changed, 77 insertions(+), 24 deletions(-) diff --git a/backend/src/controllers/assignments.ts b/backend/src/controllers/assignments.ts index 775a60df..78d11336 100644 --- a/backend/src/controllers/assignments.ts +++ b/backend/src/controllers/assignments.ts @@ -1,12 +1,26 @@ import { Request, Response } from 'express' -import { getAssignment } from '../services/assignments'; +import { getAllAssignments, getAssignment } from '../services/assignments'; -// typescript is annoywith with parameter forwarding from class.ts +// typescript is annoy with with parameter forwarding from class.ts interface AssignmentParams { classid: string; id: string; } +export async function getAllAssignmentsHandler( + req: Request, + res: Response, +): Promise { + const classid = req.params.classid; + const full = req.query.full === 'true'; + + const assignments = await getAllAssignments(classid, full); + + res.json({ + assignments: assignments, + }); +} + export async function getAssignmentHandler( req: Request, res: Response, diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index c6973632..68a5fc76 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -1,6 +1,7 @@ import { Request, Response } from 'express'; import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students'; import { ClassDTO } from '../interfaces/classes'; +import { getAllAssignments } from '../services/assignments'; // TODO: accept arguments (full, ...) // TODO: endpoints @@ -72,4 +73,23 @@ export async function getStudentClassesHandler ( console.error('Error fetching learning objects:', error); res.status(500).json({ error: 'Internal server error' }); } +} + +// Might not be fully correct depending on if +// a class has an assignment, that all students +// have this assignment. +export async function getStudentAssignmentsHandler( + req: Request, + res: Response, +): Promise { + const full = req.query.full === 'true'; + const username = req.params.id; + + const classes = await getStudentClasses(username); + + const assignments = (await Promise.all(classes.map(async cls => await getAllAssignments(cls.id, full)))).flat(); + + res.json({ + assignments: assignments + }); } \ No newline at end of file diff --git a/backend/src/interfaces/assignments.ts b/backend/src/interfaces/assignments.ts index a3c3c4cc..7fabece5 100644 --- a/backend/src/interfaces/assignments.ts +++ b/backend/src/interfaces/assignments.ts @@ -1,6 +1,6 @@ import { Assignment } from "../entities/assignments/assignment.entity"; import { Class } from "../entities/classes/class.entity"; -import { GroupDTO } from "./groups"; +import { GroupDTO, mapToGroupDTO } from "./groups"; export interface AssignmentDTO { id: number, @@ -9,17 +9,29 @@ export interface AssignmentDTO { description: string, learningPath: string, language: string, - groups?: GroupDTO[], // TODO + groups?: GroupDTO[] | string[], // TODO } -export function mapToAssignmentDTO(assignment: Assignment, cls: Class): AssignmentDTO { +export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { return { id: assignment.id, - class: cls.classId, + class: assignment.within.classId, title: assignment.title, description: assignment.description, learningPath: assignment.learningPathHruid, language: assignment.learningPathLanguage, - //groups: assignment.groups.map(mapToGroupDTO), + // groups: assignment.groups.map(group => group.groupNumber), + } +} + +export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { + return { + id: assignment.id, + class: assignment.within.classId, + title: assignment.title, + description: assignment.description, + learningPath: assignment.learningPathHruid, + language: assignment.learningPathLanguage, + // groups: assignment.groups.map(mapToGroupDTO), }; } \ No newline at end of file diff --git a/backend/src/routes/assignment.ts b/backend/src/routes/assignment.ts index 35b4b986..d6965c49 100644 --- a/backend/src/routes/assignment.ts +++ b/backend/src/routes/assignment.ts @@ -1,18 +1,11 @@ import express from 'express' -import { getAssignmentHandler } from '../controllers/assignments'; +import { getAllAssignmentsHandler, getAssignmentHandler } from '../controllers/assignments'; import groupRouter from './group.js'; const router = express.Router({ mergeParams: true }); // root endpoint used to search objects -router.get('/', (req, res) => { - res.json({ - assignments: [ - '0', - '1', - ] - }); -}); +router.get('/', getAllAssignmentsHandler); // information about an assignment with id 'id' router.get('/:id', getAssignmentHandler); diff --git a/backend/src/routes/student.ts b/backend/src/routes/student.ts index ca74d2f7..bc806b4e 100644 --- a/backend/src/routes/student.ts +++ b/backend/src/routes/student.ts @@ -1,5 +1,5 @@ import express from 'express' -import { getAllStudentsHandler, getStudentClassesHandler, getStudentHandler } from '../controllers/students'; +import { getAllStudentsHandler, getStudentAssignmentsHandler, getStudentClassesHandler, getStudentHandler } from '../controllers/students'; const router = express.Router(); // root endpoint used to search objects @@ -20,11 +20,7 @@ router.get('/:id/submissions', (req, res) => { // the list of assignments a student has -router.get('/:id/assignments', (req, res) => { - res.json({ - assignments: [ '0' ], - }); -}) +router.get('/:id/assignments', getStudentAssignmentsHandler); // the list of groups a student is in router.get('/:id/groups', (req, res) => { diff --git a/backend/src/services/assignments.ts b/backend/src/services/assignments.ts index ab76f6a8..e0804750 100644 --- a/backend/src/services/assignments.ts +++ b/backend/src/services/assignments.ts @@ -1,5 +1,23 @@ import { getAssignmentRepository, getClassRepository } from "../data/repositories"; -import { AssignmentDTO, mapToAssignmentDTO } from "../interfaces/assignments"; +import { AssignmentDTO, mapToAssignmentDTO, mapToAssignmentDTOId } from "../interfaces/assignments"; + +export async function getAllAssignments(classid: string, full: boolean): Promise { + const classRepository = getClassRepository(); + const cls = await classRepository.findById(classid); + + if (!cls) { + return []; + } + + const assignmentRepository = getAssignmentRepository(); + const assignments = await assignmentRepository.findAllAssignmentsInClass(cls); + + if (full) { + return assignments.map(mapToAssignmentDTO); + } + + return assignments.map(mapToAssignmentDTOId); +} export async function getAssignment(classid: string, id: number): Promise { const classRepository = getClassRepository(); @@ -16,5 +34,5 @@ export async function getAssignment(classid: string, id: number): Promise