From 241fe0103f04761d436e36ba8c626611c60761b7 Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Wed, 5 Mar 2025 16:31:27 +0100 Subject: [PATCH] feat: endpoints voor /, /:id en /:id/students in routes/class.ts zijn geimplementeerd --- backend/src/controllers/classes.ts | 29 +++++++++++++++++- backend/src/controllers/students.ts | 11 ++++--- backend/src/data/classes/class-repository.ts | 5 ++- backend/src/interfaces/students.ts | 11 +++++++ backend/src/routes/class.ts | 19 ++---------- backend/src/services/class.ts | 32 ++++++++++++++++++++ backend/src/services/students.ts | 22 +++++++------- 7 files changed, 95 insertions(+), 34 deletions(-) diff --git a/backend/src/controllers/classes.ts b/backend/src/controllers/classes.ts index f4c8666d..ee932734 100644 --- a/backend/src/controllers/classes.ts +++ b/backend/src/controllers/classes.ts @@ -1,5 +1,18 @@ import { Request, Response } from 'express'; -import { getClass } from '../services/class'; +import { getAllClasses, getClass, getClassStudents } from '../services/class'; +import { ClassDTO } from '../interfaces/classes'; + +export async function getAllClassesHandler( + req: Request, + res: Response, +): Promise { + const full = req.query.full === "true"; + const classes = await getAllClasses(full); + + res.json({ + classes: classes + }); +} export async function getClassHandler( req: Request, @@ -26,4 +39,18 @@ export async function getClassHandler( console.error('Error fetching learning objects:', error); res.status(500).json({ error: 'Internal server error' }); } +} + +export async function getClassStudentsHandler( + req: Request, + res: Response, +): Promise { + const classId = req.params.id; + const full = req.query.full === "true"; + + const students = await getClassStudents(classId, full); + + res.json({ + students: students, + }); } \ No newline at end of file diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index 9b4b8f3c..c6973632 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -1,17 +1,18 @@ import { Request, Response } from 'express'; -import { getStudent, getStudentClasses, getStudentClassIds } from '../services/students'; +import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students'; import { ClassDTO } from '../interfaces/classes'; +// TODO: accept arguments (full, ...) +// TODO: endpoints export async function getAllStudentsHandler ( req: Request, res: Response, ): Promise { try { + const students = await getAllStudents(); + res.json({ - students: [ - '0', - '1', - ] + students: students }); } catch (error) { console.error('Error fetching learning objects:', error); diff --git a/backend/src/data/classes/class-repository.ts b/backend/src/data/classes/class-repository.ts index a9455323..cd0e44fc 100644 --- a/backend/src/data/classes/class-repository.ts +++ b/backend/src/data/classes/class-repository.ts @@ -4,7 +4,10 @@ import { Student } from '../../entities/users/student.entity.js'; export class ClassRepository extends DwengoEntityRepository { public findById(id: string): Promise { - return this.findOne({ classId: id }); + return this.findOne( + { classId: id }, + { populate: ["students", "teachers"] }, + ); } public deleteById(id: string): Promise { return this.deleteWhere({ classId: id }); diff --git a/backend/src/interfaces/students.ts b/backend/src/interfaces/students.ts index cb13d3c2..e87e707d 100644 --- a/backend/src/interfaces/students.ts +++ b/backend/src/interfaces/students.ts @@ -1,3 +1,5 @@ +import { Student } from "../entities/users/student.entity"; + export interface StudentDTO { id: string; username: string; @@ -10,3 +12,12 @@ export interface StudentDTO { groups: string; }; } + +export function mapToStudentDTO(student: Student): StudentDTO { + return { + id: student.username, + username: student.username, + firstName: student.firstName, + lastName: student.lastName, + }; +} \ No newline at end of file diff --git a/backend/src/routes/class.ts b/backend/src/routes/class.ts index 65ca9362..e58547b2 100644 --- a/backend/src/routes/class.ts +++ b/backend/src/routes/class.ts @@ -1,16 +1,9 @@ import express from 'express' -import { getClassHandler } from '../controllers/classes'; +import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes'; const router = express.Router(); // root endpoint used to search objects -router.get('/', (req, res) => { - res.json({ - classes: [ - '0', - '1', - ] - }); -}); +router.get('/', getAllClassesHandler); // information about an class with id 'id' router.get('/:id', getClassHandler); @@ -31,12 +24,6 @@ router.get('/:id/assignments', (req, res) => { }); }) -router.get('/:id/students', (req, res) => { - res.json({ - students: [ - '0' - ], - }); -}) +router.get('/:id/students', getClassStudentsHandler); export default router \ No newline at end of file diff --git a/backend/src/services/class.ts b/backend/src/services/class.ts index 3a3e8322..89628404 100644 --- a/backend/src/services/class.ts +++ b/backend/src/services/class.ts @@ -1,5 +1,22 @@ import { getClassRepository } from "../data/repositories"; +import { Class } from "../entities/classes/class.entity"; import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; +import { mapToStudentDTO, StudentDTO } from "../interfaces/students"; + +export async function getAllClasses(full: boolean): Promise { + const classRepository = getClassRepository(); + const classes = await classRepository.find({}, { populate: ["students", "teachers"] }); + + if (!classes) { + return []; + } + + if (full) { + return classes.map(mapToClassDTO); + } else { + return classes.map((cls) => cls.classId); + } +} export async function getClass(classId: string): Promise { const classRepository = getClassRepository(); @@ -9,4 +26,19 @@ export async function getClass(classId: string): Promise { else { return mapToClassDTO(cls); } +} + +export async function getClassStudents(classId: string, full: boolean): Promise { + const classRepository = getClassRepository(); + const cls = await classRepository.findById(classId); + + if (!cls) { + return []; + } + + if (full) { + return cls.students.map(mapToStudentDTO); + } else { + return cls.students.map((student) => student.username); + } } \ No newline at end of file diff --git a/backend/src/services/students.ts b/backend/src/services/students.ts index 3a1b601f..a69cab85 100644 --- a/backend/src/services/students.ts +++ b/backend/src/services/students.ts @@ -1,26 +1,26 @@ import { getClassRepository, getStudentRepository } from "../data/repositories"; import { Class } from "../entities/classes/class.entity"; +import { Student } from "../entities/users/student.entity"; import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; -import { StudentDTO } from "../interfaces/students"; +import { StudentDTO, mapToStudentDTO } from "../interfaces/students"; + export async function getAllStudents(): Promise { - // TODO - return []; + const studentRepository = getStudentRepository(); + const students = await studentRepository.find({}); + + return students.map(mapToStudentDTO); } export async function getStudent(username: string): Promise { const studentRepository = getStudentRepository(); const student = await studentRepository.findByUsername(username); - if (!student) return null; - else { - return { - id: student.username, - username: student.username, - firstName: student.firstName, - lastName: student.lastName, - } + if (!student) { + return null; } + + return mapToStudentDTO(student); } async function fetchStudentClasses(username: string): Promise {