From 943dd04e977714c7ef8560240d789986aff958f6 Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Sat, 29 Mar 2025 22:28:52 +0100 Subject: [PATCH] fix: gefaalde testen voor class controller gefixt --- backend/src/controllers/classes.ts | 14 ++++++++-- backend/src/data/users/student-repository.ts | 4 +++ backend/src/services/class.ts | 27 ++++++++++---------- backend/tests/controllers/classes.test.ts | 17 +++--------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/backend/src/controllers/classes.ts b/backend/src/controllers/classes.ts index ca2f5698..65548cf5 100644 --- a/backend/src/controllers/classes.ts +++ b/backend/src/controllers/classes.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express'; -import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/class.js'; +import { createClass, getAllClasses, getClass, getClassStudents, getClassTeacherInvitations } from '../services/class.js'; import { ClassDTO } from '../interfaces/class.js'; export async function getAllClassesHandler(req: Request, res: Response): Promise { @@ -58,7 +58,12 @@ export async function getClassStudentsHandler(req: Request, res: Response): Prom const classId = req.params.id; const full = req.query.full === 'true'; - const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId); + const students = await getClassStudents(classId, full); + + if (!students) { + res.status(404).json({ error: 'Class not found' }); + return; + } res.json({ students: students, @@ -71,6 +76,11 @@ export async function getTeacherInvitationsHandler(req: Request, res: Response): const invitations = await getClassTeacherInvitations(classId, full); + if (!invitations) { + res.status(404).json({ error: 'Class not found' }); + return; + } + res.json({ invitations: invitations, }); diff --git a/backend/src/data/users/student-repository.ts b/backend/src/data/users/student-repository.ts index 0792678d..f35fd153 100644 --- a/backend/src/data/users/student-repository.ts +++ b/backend/src/data/users/student-repository.ts @@ -1,3 +1,4 @@ +import { Class } from '../../entities/classes/class.entity.js'; import { Student } from '../../entities/users/student.entity.js'; import { User } from '../../entities/users/user.entity.js'; import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; @@ -9,6 +10,9 @@ export class StudentRepository extends DwengoEntityRepository { public findByUsername(username: string): Promise { return this.findOne({ username: username }); } + public findByClass(cls: Class): Promise { + return this.find({ classes: cls }); + } public deleteByUsername(username: string): Promise { return this.deleteWhere({ username: username }); } diff --git a/backend/src/services/class.ts b/backend/src/services/class.ts index 9f6e1efe..bc2984fc 100644 --- a/backend/src/services/class.ts +++ b/backend/src/services/class.ts @@ -4,6 +4,7 @@ import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js'; import { getLogger } from '../logging/initalize.js'; +import { getStudent } from './students.js'; const logger = getLogger(); @@ -60,32 +61,30 @@ export async function getClass(classId: string): Promise { return mapToClassDTO(cls); } -async function fetchClassStudents(classId: string): Promise { +export async function getClassStudents(classId: string, full: boolean): Promise { const classRepository = getClassRepository(); const cls = await classRepository.findById(classId); if (!cls) { - return []; + return null; } - return cls.students.map(mapToStudentDTO); + const studentRepository = getStudentRepository(); + const students = await studentRepository.findByClass(cls); + + if (full) { + return cls.students.map(mapToStudentDTO); + } + + return students.map((student) => student.username); } -export async function getClassStudents(classId: string): Promise { - return await fetchClassStudents(classId); -} - -export async function getClassStudentsIds(classId: string): Promise { - const students: StudentDTO[] = await fetchClassStudents(classId); - return students.map((student) => student.username); -} - -export async function getClassTeacherInvitations(classId: string, full: boolean): Promise { +export async function getClassTeacherInvitations(classId: string, full: boolean): Promise { const classRepository = getClassRepository(); const cls = await classRepository.findById(classId); if (!cls) { - return []; + return null; } const teacherInvitationRepository = getTeacherInvitationRepository(); diff --git a/backend/tests/controllers/classes.test.ts b/backend/tests/controllers/classes.test.ts index f91c5e67..a5a27a5e 100644 --- a/backend/tests/controllers/classes.test.ts +++ b/backend/tests/controllers/classes.test.ts @@ -3,6 +3,7 @@ import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; import { Request, Response } from 'express'; import { getAllClasses } from '../../src/services/class.js'; +import { checkReturnList, checkReturn404 } from './qol.js'; describe('Class controllers', () => { let req: Partial; @@ -84,15 +85,7 @@ describe('Class controllers', () => { await getClassStudentsHandler(req as Request, res as Response); - expect(jsonMock).toHaveBeenCalledWith({ students: [ - 'DireStraits', - 'Nirvana', - 'Noordkaap', - 'PinkFloyd', - 'SmashingPumpkins', - 'TheDoors', - 'Tool' - ]}); + checkReturnList(jsonMock, 'students'); }); it('should return 404 not found when calling getClassStudentsHandler on a non-existent class', async () => { @@ -102,8 +95,7 @@ describe('Class controllers', () => { }; await getClassStudentsHandler(req as Request, res as Response); - - // will fail until code is fixed + expect(statusMock).toHaveBeenCalledWith(404); expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); }); @@ -133,9 +125,8 @@ describe('Class controllers', () => { await getTeacherInvitationsHandler(req as Request, res as Response); - // will fail until code is fixed - expect(statusMock).toHaveBeenCalledWith(404); expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); + expect(statusMock).toHaveBeenCalledWith(404); }); it('should return a list of classes', async () => {