feat: endpoints voor /, /:id en /:id/students in routes/class.ts zijn geimplementeerd

This commit is contained in:
Adriaan Jacquet 2025-03-05 16:31:27 +01:00
parent 123fdf0fa1
commit 241fe0103f
7 changed files with 95 additions and 34 deletions

View file

@ -1,5 +1,18 @@
import { Request, Response } from 'express'; 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<void> {
const full = req.query.full === "true";
const classes = await getAllClasses(full);
res.json({
classes: classes
});
}
export async function getClassHandler( export async function getClassHandler(
req: Request, req: Request,
@ -26,4 +39,18 @@ export async function getClassHandler(
console.error('Error fetching learning objects:', error); console.error('Error fetching learning objects:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
}
export async function getClassStudentsHandler(
req: Request,
res: Response,
): Promise<void> {
const classId = req.params.id;
const full = req.query.full === "true";
const students = await getClassStudents(classId, full);
res.json({
students: students,
});
} }

View file

@ -1,17 +1,18 @@
import { Request, Response } from 'express'; 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'; import { ClassDTO } from '../interfaces/classes';
// TODO: accept arguments (full, ...)
// TODO: endpoints
export async function getAllStudentsHandler ( export async function getAllStudentsHandler (
req: Request, req: Request,
res: Response, res: Response,
): Promise<void> { ): Promise<void> {
try { try {
const students = await getAllStudents();
res.json({ res.json({
students: [ students: students
'0',
'1',
]
}); });
} catch (error) { } catch (error) {
console.error('Error fetching learning objects:', error); console.error('Error fetching learning objects:', error);

View file

@ -4,7 +4,10 @@ import { Student } from '../../entities/users/student.entity.js';
export class ClassRepository extends DwengoEntityRepository<Class> { export class ClassRepository extends DwengoEntityRepository<Class> {
public findById(id: string): Promise<Class | null> { public findById(id: string): Promise<Class | null> {
return this.findOne({ classId: id }); return this.findOne(
{ classId: id },
{ populate: ["students", "teachers"] },
);
} }
public deleteById(id: string): Promise<void> { public deleteById(id: string): Promise<void> {
return this.deleteWhere({ classId: id }); return this.deleteWhere({ classId: id });

View file

@ -1,3 +1,5 @@
import { Student } from "../entities/users/student.entity";
export interface StudentDTO { export interface StudentDTO {
id: string; id: string;
username: string; username: string;
@ -10,3 +12,12 @@ export interface StudentDTO {
groups: string; groups: string;
}; };
} }
export function mapToStudentDTO(student: Student): StudentDTO {
return {
id: student.username,
username: student.username,
firstName: student.firstName,
lastName: student.lastName,
};
}

View file

@ -1,16 +1,9 @@
import express from 'express' import express from 'express'
import { getClassHandler } from '../controllers/classes'; import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes';
const router = express.Router(); const router = express.Router();
// root endpoint used to search objects // root endpoint used to search objects
router.get('/', (req, res) => { router.get('/', getAllClassesHandler);
res.json({
classes: [
'0',
'1',
]
});
});
// information about an class with id 'id' // information about an class with id 'id'
router.get('/:id', getClassHandler); router.get('/:id', getClassHandler);
@ -31,12 +24,6 @@ router.get('/:id/assignments', (req, res) => {
}); });
}) })
router.get('/:id/students', (req, res) => { router.get('/:id/students', getClassStudentsHandler);
res.json({
students: [
'0'
],
});
})
export default router export default router

View file

@ -1,5 +1,22 @@
import { getClassRepository } from "../data/repositories"; import { getClassRepository } from "../data/repositories";
import { Class } from "../entities/classes/class.entity";
import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
import { mapToStudentDTO, StudentDTO } from "../interfaces/students";
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
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<ClassDTO | null> { export async function getClass(classId: string): Promise<ClassDTO | null> {
const classRepository = getClassRepository(); const classRepository = getClassRepository();
@ -9,4 +26,19 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
else { else {
return mapToClassDTO(cls); return mapToClassDTO(cls);
} }
}
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
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);
}
} }

View file

@ -1,26 +1,26 @@
import { getClassRepository, getStudentRepository } from "../data/repositories"; import { getClassRepository, getStudentRepository } from "../data/repositories";
import { Class } from "../entities/classes/class.entity"; import { Class } from "../entities/classes/class.entity";
import { Student } from "../entities/users/student.entity";
import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
import { StudentDTO } from "../interfaces/students"; import { StudentDTO, mapToStudentDTO } from "../interfaces/students";
export async function getAllStudents(): Promise<StudentDTO[]> { export async function getAllStudents(): Promise<StudentDTO[]> {
// TODO const studentRepository = getStudentRepository();
return []; const students = await studentRepository.find({});
return students.map(mapToStudentDTO);
} }
export async function getStudent(username: string): Promise<StudentDTO | null> { export async function getStudent(username: string): Promise<StudentDTO | null> {
const studentRepository = getStudentRepository(); const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username); const student = await studentRepository.findByUsername(username);
if (!student) return null; if (!student) {
else { return null;
return {
id: student.username,
username: student.username,
firstName: student.firstName,
lastName: student.lastName,
}
} }
return mapToStudentDTO(student);
} }
async function fetchStudentClasses(username: string): Promise<Class[]> { async function fetchStudentClasses(username: string): Promise<Class[]> {