feat: DELETE voor class geimplenteerd
This commit is contained in:
		
							parent
							
								
									62711f323d
								
							
						
					
					
						commit
						7ae2f1de0c
					
				
					 2 changed files with 23 additions and 32 deletions
				
			
		|  | @ -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<Class | null> { | ||||
| async function fetchClass(classid: string): Promise<Class> { | ||||
|     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<ClassDTO[] | string[ | |||
|     const classRepository = getClassRepository(); | ||||
|     const classes = await classRepository.find({}, { populate: ['students', 'teachers'] }); | ||||
| 
 | ||||
|     if (!classes) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return classes.map(mapToClassDTO); | ||||
|     } | ||||
|     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> { | ||||
|     const teacherRepository = getTeacherRepository(); | ||||
|     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); | ||||
| 
 | ||||
|     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<ClassDTO | null> { | |||
| export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> { | ||||
|     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<TeacherInvitationDTO[]> { | ||||
|     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); | ||||
|  |  | |||
|  | @ -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<StudentDTO[] | null> { | ||||
| export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> { | ||||
|     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<StudentDTO[] | string[] | null> { | ||||
|     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<QuestionDTO[] | null> { | ||||
|  |  | |||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet