feat: controller en service laag toegevoegd voor student/:id/classes

This commit is contained in:
Adriaan Jacquet 2025-03-04 15:50:20 +01:00
parent f5b6a5a604
commit ceef74f1af
6 changed files with 127 additions and 23 deletions

View file

@ -1,7 +1,14 @@
import { getStudentRepository } from "../data/repositories";
import { getClassRepository, getStudentRepository } from "../data/repositories";
import { Class } from "../entities/classes/class.entity";
import { ClassDTO } from "../interfaces/classes";
import { StudentDTO } from "../interfaces/students";
export async function getStudentById(username: string): Promise<StudentDTO | null> {
export async function getAllStudents(): Promise<StudentDTO[]> {
// TODO
return [];
}
export async function getStudent(username: string): Promise<StudentDTO | null> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
@ -16,3 +23,43 @@ export async function getStudentById(username: string): Promise<StudentDTO | nul
}
}
async function fetchStudentClasses(username: string): Promise<Class[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
if (!student) return [];
const classRepository = getClassRepository();
// a weird error when running npm run dev occurs when using .findByStudent
// the error says that the function could not be found which is weird
// because typescript does not throw any errors
const classes = await classRepository.find(
{ students: student },
{ populate: ["students", "teachers"] } // voegt student en teacher objecten toe
)
// const classes = await classRepository.findByStudent(student);
if (!classes) return [];
return classes;
}
export async function getStudentClasses(username: string): Promise<ClassDTO[]> {
const classes = await fetchStudentClasses(username);
return classes.map((cls: Class): ClassDTO => {
return {
id: cls.classId,
displayName: cls.displayName,
teachers: cls.teachers.map(teacher => teacher.username),
students: cls.students.map(student => student.username),
joinRequests: [], // TODO
}
});
}
export async function getStudentClassIds(username: string): Promise<string[]> {
const classes = await fetchStudentClasses(username);
return classes.map(cls => cls.classId); // class is a native keyword
}