feat: DELETE voor class geimplenteerd

This commit is contained in:
Adriaan Jacquet 2025-03-31 12:39:22 +02:00
parent 62711f323d
commit 7ae2f1de0c
2 changed files with 23 additions and 32 deletions

View file

@ -1,5 +1,6 @@
import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js'; import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js';
import { Class } from '../entities/classes/class.entity.js'; import { Class } from '../entities/classes/class.entity.js';
import { NotFoundException } from '../exceptions/not-found-exception.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js'; import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
@ -7,10 +8,14 @@ import { getLogger } from '../logging/initalize.js';
const logger = getLogger(); const logger = getLogger();
async function fetchClass(classid: string): Promise<Class | null> { async function fetchClass(classid: string): Promise<Class> {
const classRepository = getClassRepository(); const classRepository = getClassRepository();
const cls = await classRepository.findById(classid); const cls = await classRepository.findById(classid);
if (!cls) {
throw new NotFoundException("Class not found");
}
return cls; return cls;
} }
@ -18,16 +23,18 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
const classRepository = getClassRepository(); const classRepository = getClassRepository();
const classes = await classRepository.find({}, { populate: ['students', 'teachers'] }); const classes = await classRepository.find({}, { populate: ['students', 'teachers'] });
if (!classes) {
return [];
}
if (full) { if (full) {
return classes.map(mapToClassDTO); return classes.map(mapToClassDTO);
} }
return classes.map((cls) => cls.classId!); return classes.map((cls) => cls.classId!);
} }
export async function getClass(classId: string): Promise<ClassDTO | null> {
const cls = await fetchClass(classId);
return mapToClassDTO(cls);
}
export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> { export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> {
const teacherRepository = getTeacherRepository(); const teacherRepository = getTeacherRepository();
const teacherUsernames = classData.teachers || []; const teacherUsernames = classData.teachers || [];
@ -54,12 +61,11 @@ export async function createClass(classData: ClassDTO): Promise<ClassDTO | null>
} }
} }
export async function getClass(classId: string): Promise<ClassDTO | null> { export async function deleteClass(classId: string): Promise<ClassDTO> {
const cls = await fetchClass(classId); const cls = await fetchClass(classId);
if (!cls) { const classRepository = getClassRepository();
return null; await classRepository.deleteById(classId);
}
return mapToClassDTO(cls); return mapToClassDTO(cls);
} }
@ -67,10 +73,6 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> { export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
const cls = await fetchClass(classId); const cls = await fetchClass(classId);
if (!cls) {
return [];
}
if (full) { if (full) {
return cls.students.map(mapToStudentDTO); return cls.students.map(mapToStudentDTO);
} }
@ -79,12 +81,7 @@ export async function getClassStudents(classId: string, full: boolean): Promise<
} }
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> { export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
const classRepository = getClassRepository(); const cls = await fetchClass(classId);
const cls = await classRepository.findById(classId);
if (!cls) {
return [];
}
const teacherInvitationRepository = getTeacherInvitationRepository(); const teacherInvitationRepository = getTeacherInvitationRepository();
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls); const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls);

View file

@ -76,28 +76,22 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom
return classes.map((cls) => cls.id); return classes.map((cls) => cls.id);
} }
export async function fetchStudentsByTeacher(username: string): Promise<StudentDTO[] | null> { export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> {
const classes = (await getClassesByTeacher(username, false)) as string[]; const classes = (await getClassesByTeacher(username, false)) as string[];
if (!classes) { if (!classes) {
return null; return null;
} }
return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat(); // workaround
} let students;
export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> {
const students = await fetchStudentsByTeacher(username);
if (!students) {
return null;
}
if (full) { if (full) {
return students; students = (await Promise.all(classes.map(async (id) => await getClassStudents(id, full) as StudentDTO[]))).flat();
} else {
students = (await Promise.all(classes.map(async (id) => await getClassStudents(id, full) as string[]))).flat();
} }
return students.map((student) => student.username); return students;
} }
export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[] | null> { export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[] | null> {