diff --git a/backend/src/services/classes.ts b/backend/src/services/classes.ts index dbf6c63d..f2cb79e8 100644 --- a/backend/src/services/classes.ts +++ b/backend/src/services/classes.ts @@ -1,5 +1,6 @@ import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.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 { mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js'; @@ -7,10 +8,14 @@ import { getLogger } from '../logging/initalize.js'; const logger = getLogger(); -async function fetchClass(classid: string): Promise { +async function fetchClass(classid: string): Promise { const classRepository = getClassRepository(); const cls = await classRepository.findById(classid); + if (!cls) { + throw new NotFoundException("Class not found"); + } + return cls; } @@ -18,16 +23,18 @@ export async function getAllClasses(full: boolean): Promise cls.classId!); } +export async function getClass(classId: string): Promise { + const cls = await fetchClass(classId); + + return mapToClassDTO(cls); +} + export async function createClass(classData: ClassDTO): Promise { const teacherRepository = getTeacherRepository(); const teacherUsernames = classData.teachers || []; @@ -54,12 +61,11 @@ export async function createClass(classData: ClassDTO): Promise } } -export async function getClass(classId: string): Promise { +export async function deleteClass(classId: string): Promise { const cls = await fetchClass(classId); - if (!cls) { - return null; - } + const classRepository = getClassRepository(); + await classRepository.deleteById(classId); return mapToClassDTO(cls); } @@ -67,10 +73,6 @@ export async function getClass(classId: string): Promise { export async function getClassStudents(classId: string, full: boolean): Promise { const cls = await fetchClass(classId); - if (!cls) { - return []; - } - if (full) { 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 { - const classRepository = getClassRepository(); - const cls = await classRepository.findById(classId); - - if (!cls) { - return []; - } + const cls = await fetchClass(classId); const teacherInvitationRepository = getTeacherInvitationRepository(); const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls); diff --git a/backend/src/services/teachers.ts b/backend/src/services/teachers.ts index fced2b61..cf2743e9 100644 --- a/backend/src/services/teachers.ts +++ b/backend/src/services/teachers.ts @@ -76,28 +76,22 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom return classes.map((cls) => cls.id); } -export async function fetchStudentsByTeacher(username: string): Promise { +export async function getStudentsByTeacher(username: string, full: boolean): Promise { const classes = (await getClassesByTeacher(username, false)) as string[]; if (!classes) { return null; } - return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat(); -} - -export async function getStudentsByTeacher(username: string, full: boolean): Promise { - const students = await fetchStudentsByTeacher(username); - - if (!students) { - return null; - } - + // workaround + let students; 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 {