fix: juiste responsecodes en messages toegevoegd
This commit is contained in:
		
							parent
							
								
									59e8f2fcf2
								
							
						
					
					
						commit
						1664642940
					
				
					 9 changed files with 61 additions and 17 deletions
				
			
		|  | @ -28,6 +28,11 @@ export async function getGroupHandler(req: Request<GroupParams>, res: Response): | ||||||
| 
 | 
 | ||||||
|     const group = await getGroup(classId, assignmentId, groupId, full); |     const group = await getGroup(classId, assignmentId, groupId, full); | ||||||
| 
 | 
 | ||||||
|  |     if (!group) { | ||||||
|  |         res.status(404).json({ error: 'Group not found' }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     res.json(group); |     res.json(group); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -76,10 +76,10 @@ export async function getQuestionAnswersHandler(req: Request, res: Response): Pr | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const answers = getAnswersByQuestion(questionId, full); |     const answers = await getAnswersByQuestion(questionId, full); | ||||||
| 
 | 
 | ||||||
|     if (!answers) { |     if (!answers) { | ||||||
|         res.status(404).json({ error: `Questions not found.` }); |         res.status(404).json({ error: `Questions not found` }); | ||||||
|     } else { |     } else { | ||||||
|         res.json({ answers: answers }); |         res.json({ answers: answers }); | ||||||
|     } |     } | ||||||
|  | @ -96,7 +96,7 @@ export async function createQuestionHandler(req: Request, res: Response): Promis | ||||||
|     const question = await createQuestion(questionDTO); |     const question = await createQuestion(questionDTO); | ||||||
| 
 | 
 | ||||||
|     if (!question) { |     if (!question) { | ||||||
|         res.status(400).json({ error: 'Could not add question' }); |         res.status(400).json({ error: 'Could not create question' }); | ||||||
|     } else { |     } else { | ||||||
|         res.json(question); |         res.json(question); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -58,6 +58,14 @@ export async function createStudentHandler(req: Request, res: Response) { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const newUser = await createStudent(userData); |     const newUser = await createStudent(userData); | ||||||
|  | 
 | ||||||
|  |     if (!newUser) { | ||||||
|  |         res.status(500).json({ | ||||||
|  |             error: 'Something went wrong while creating student' | ||||||
|  |         }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     res.status(201).json(newUser); |     res.status(201).json(newUser); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -94,6 +94,11 @@ export async function getTeacherClassHandler(req: Request, res: Response): Promi | ||||||
| 
 | 
 | ||||||
|     const classes = await getClassesByTeacher(username, full); |     const classes = await getClassesByTeacher(username, full); | ||||||
| 
 | 
 | ||||||
|  |     if (!classes) { | ||||||
|  |         res.status(404).json({ error: 'Teacher not found' }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     res.json({ classes: classes }); |     res.json({ classes: classes }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -108,6 +113,11 @@ export async function getTeacherStudentHandler(req: Request, res: Response): Pro | ||||||
| 
 | 
 | ||||||
|     const students = await getStudentsByTeacher(username, full); |     const students = await getStudentsByTeacher(username, full); | ||||||
| 
 | 
 | ||||||
|  |     if (!students) { | ||||||
|  |         res.status(404).json({ error: 'Teacher not found' }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     res.json({ students: students }); |     res.json({ students: students }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -122,5 +132,10 @@ export async function getTeacherQuestionHandler(req: Request, res: Response): Pr | ||||||
| 
 | 
 | ||||||
|     const questions = await getQuestionsByTeacher(username, full); |     const questions = await getQuestionsByTeacher(username, full); | ||||||
| 
 | 
 | ||||||
|  |     if (!questions) { | ||||||
|  |         res.status(404).json({ error: 'Teacher not found' }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     res.json({ questions: questions }); |     res.json({ questions: questions }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -21,7 +21,7 @@ export async function getAllAssignments(classid: string, full: boolean): Promise | ||||||
|     return assignments.map(mapToAssignmentDTOId); |     return assignments.map(mapToAssignmentDTOId); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<Assignment | null> { | export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO | null> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     const cls = await classRepository.findById(classid); |     const cls = await classRepository.findById(classid); | ||||||
| 
 | 
 | ||||||
|  | @ -36,7 +36,7 @@ export async function createAssignment(classid: string, assignmentData: Assignme | ||||||
|         const newAssignment = assignmentRepository.create(assignment); |         const newAssignment = assignmentRepository.create(assignment); | ||||||
|         await assignmentRepository.save(newAssignment); |         await assignmentRepository.save(newAssignment); | ||||||
| 
 | 
 | ||||||
|         return newAssignment; |         return mapToAssignmentDTO(newAssignment); | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -21,7 +21,7 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[ | ||||||
|     return classes.map((cls) => cls.classId!); |     return classes.map((cls) => cls.classId!); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createClass(classData: ClassDTO): Promise<Class | null> { | export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> { | ||||||
|     const teacherRepository = getTeacherRepository(); |     const teacherRepository = getTeacherRepository(); | ||||||
|     const teacherUsernames = classData.teachers || []; |     const teacherUsernames = classData.teachers || []; | ||||||
|     const teachers = (await Promise.all(teacherUsernames.map((id) => teacherRepository.findByUsername(id)))).filter((teacher) => teacher != null); |     const teachers = (await Promise.all(teacherUsernames.map((id) => teacherRepository.findByUsername(id)))).filter((teacher) => teacher != null); | ||||||
|  | @ -42,7 +42,7 @@ export async function createClass(classData: ClassDTO): Promise<Class | null> { | ||||||
|         }); |         }); | ||||||
|         await classRepository.save(newClass); |         await classRepository.save(newClass); | ||||||
| 
 | 
 | ||||||
|         return newClass; |         return mapToClassDTO(newClass); | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|         logger.error(e); |         logger.error(e); | ||||||
|         return null; |         return null; | ||||||
|  |  | ||||||
|  | @ -103,5 +103,5 @@ export async function deleteQuestion(questionId: QuestionId) { | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return question; |     return mapToQuestionDTO(question); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,7 +32,7 @@ export async function createSubmission(submissionDTO: SubmissionDTO) { | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return submission; |     return mapToSubmissionDTO(submission); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteSubmission(learningObjectHruid: string, language: Language, version: number, submissionNumber: number) { | export async function deleteSubmission(learningObjectHruid: string, language: Language, version: number, submissionNumber: number) { | ||||||
|  |  | ||||||
|  | @ -64,11 +64,11 @@ export async function deleteTeacher(username: string): Promise<TeacherDTO | null | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { | export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[] | null> { | ||||||
|     const teacherRepository = getTeacherRepository(); |     const teacherRepository = getTeacherRepository(); | ||||||
|     const teacher = await teacherRepository.findByUsername(username); |     const teacher = await teacherRepository.findByUsername(username); | ||||||
|     if (!teacher) { |     if (!teacher) { | ||||||
|         return []; |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -76,9 +76,13 @@ export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[ | ||||||
|     return classes.map(mapToClassDTO); |     return classes.map(mapToClassDTO); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> { | export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[] | null> { | ||||||
|     const classes = await fetchClassesByTeacher(username); |     const classes = await fetchClassesByTeacher(username); | ||||||
| 
 | 
 | ||||||
|  |     if (!classes) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (full) { |     if (full) { | ||||||
|         return classes; |         return classes; | ||||||
|     } |     } | ||||||
|  | @ -86,15 +90,23 @@ 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) { | export async function fetchStudentsByTeacher(username: string): Promise<StudentDTO[] | null> { | ||||||
|     const classes = (await getClassesByTeacher(username, false)) as string[]; |     const classes = (await getClassesByTeacher(username, false)) as string[]; | ||||||
| 
 | 
 | ||||||
|  |     if (!classes) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat(); |     return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[]> { | export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> { | ||||||
|     const students = await fetchStudentsByTeacher(username); |     const students = await fetchStudentsByTeacher(username); | ||||||
| 
 | 
 | ||||||
|  |     if (!students) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (full) { |     if (full) { | ||||||
|         return students; |         return students; | ||||||
|     } |     } | ||||||
|  | @ -102,11 +114,11 @@ export async function getStudentsByTeacher(username: string, full: boolean): Pro | ||||||
|     return students.map((student) => student.username); |     return students.map((student) => student.username); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> { | export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[] | null> { | ||||||
|     const teacherRepository = getTeacherRepository(); |     const teacherRepository = getTeacherRepository(); | ||||||
|     const teacher = await teacherRepository.findByUsername(username); |     const teacher = await teacherRepository.findByUsername(username); | ||||||
|     if (!teacher) { |     if (!teacher) { | ||||||
|         throw new Error(`Teacher with username '${username}' not found.`); |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Find all learning objects that this teacher manages
 |     // Find all learning objects that this teacher manages
 | ||||||
|  | @ -120,9 +132,13 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD | ||||||
|     return questions.map(mapToQuestionDTO); |     return questions.map(mapToQuestionDTO); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getQuestionsByTeacher(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | export async function getQuestionsByTeacher(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[] | null> { | ||||||
|     const questions = await fetchTeacherQuestions(username); |     const questions = await fetchTeacherQuestions(username); | ||||||
| 
 | 
 | ||||||
|  |     if (!questions) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (full) { |     if (full) { | ||||||
|         return questions; |         return questions; | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet