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); | ||||
| 
 | ||||
|     if (!group) { | ||||
|         res.status(404).json({ error: 'Group not found' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json(group); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -76,10 +76,10 @@ export async function getQuestionAnswersHandler(req: Request, res: Response): Pr | |||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const answers = getAnswersByQuestion(questionId, full); | ||||
|     const answers = await getAnswersByQuestion(questionId, full); | ||||
| 
 | ||||
|     if (!answers) { | ||||
|         res.status(404).json({ error: `Questions not found.` }); | ||||
|         res.status(404).json({ error: `Questions not found` }); | ||||
|     } else { | ||||
|         res.json({ answers: answers }); | ||||
|     } | ||||
|  | @ -96,7 +96,7 @@ export async function createQuestionHandler(req: Request, res: Response): Promis | |||
|     const question = await createQuestion(questionDTO); | ||||
| 
 | ||||
|     if (!question) { | ||||
|         res.status(400).json({ error: 'Could not add question' }); | ||||
|         res.status(400).json({ error: 'Could not create question' }); | ||||
|     } else { | ||||
|         res.json(question); | ||||
|     } | ||||
|  |  | |||
|  | @ -58,6 +58,14 @@ export async function createStudentHandler(req: Request, res: Response) { | |||
|     } | ||||
| 
 | ||||
|     const newUser = await createStudent(userData); | ||||
| 
 | ||||
|     if (!newUser) { | ||||
|         res.status(500).json({ | ||||
|             error: 'Something went wrong while creating student' | ||||
|         }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.status(201).json(newUser); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -94,6 +94,11 @@ export async function getTeacherClassHandler(req: Request, res: Response): Promi | |||
| 
 | ||||
|     const classes = await getClassesByTeacher(username, full); | ||||
| 
 | ||||
|     if (!classes) { | ||||
|         res.status(404).json({ error: 'Teacher not found' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json({ classes: classes }); | ||||
| } | ||||
| 
 | ||||
|  | @ -108,6 +113,11 @@ export async function getTeacherStudentHandler(req: Request, res: Response): Pro | |||
| 
 | ||||
|     const students = await getStudentsByTeacher(username, full); | ||||
| 
 | ||||
|     if (!students) { | ||||
|         res.status(404).json({ error: 'Teacher not found' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json({ students: students }); | ||||
| } | ||||
| 
 | ||||
|  | @ -122,5 +132,10 @@ export async function getTeacherQuestionHandler(req: Request, res: Response): Pr | |||
| 
 | ||||
|     const questions = await getQuestionsByTeacher(username, full); | ||||
| 
 | ||||
|     if (!questions) { | ||||
|         res.status(404).json({ error: 'Teacher not found' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json({ questions: questions }); | ||||
| } | ||||
|  |  | |||
|  | @ -21,7 +21,7 @@ export async function getAllAssignments(classid: string, full: boolean): Promise | |||
|     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 cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|  | @ -36,7 +36,7 @@ export async function createAssignment(classid: string, assignmentData: Assignme | |||
|         const newAssignment = assignmentRepository.create(assignment); | ||||
|         await assignmentRepository.save(newAssignment); | ||||
| 
 | ||||
|         return newAssignment; | ||||
|         return mapToAssignmentDTO(newAssignment); | ||||
|     } catch (e) { | ||||
|         return null; | ||||
|     } | ||||
|  |  | |||
|  | @ -21,7 +21,7 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[ | |||
|     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 teacherUsernames = classData.teachers || []; | ||||
|     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); | ||||
| 
 | ||||
|         return newClass; | ||||
|         return mapToClassDTO(newClass); | ||||
|     } catch (e) { | ||||
|         logger.error(e); | ||||
|         return null; | ||||
|  |  | |||
|  | @ -103,5 +103,5 @@ export async function deleteQuestion(questionId: QuestionId) { | |||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     return question; | ||||
|     return mapToQuestionDTO(question); | ||||
| } | ||||
|  |  | |||
|  | @ -32,7 +32,7 @@ export async function createSubmission(submissionDTO: SubmissionDTO) { | |||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     return submission; | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
| 
 | ||||
| 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 teacher = await teacherRepository.findByUsername(username); | ||||
|     if (!teacher) { | ||||
|         return []; | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     const classRepository = getClassRepository(); | ||||
|  | @ -76,9 +76,13 @@ export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[ | |||
|     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); | ||||
| 
 | ||||
|     if (!classes) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return classes; | ||||
|     } | ||||
|  | @ -86,15 +90,23 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom | |||
|     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[]; | ||||
| 
 | ||||
|     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[]> { | ||||
| export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> { | ||||
|     const students = await fetchStudentsByTeacher(username); | ||||
| 
 | ||||
|     if (!students) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return students; | ||||
|     } | ||||
|  | @ -102,11 +114,11 @@ export async function getStudentsByTeacher(username: string, full: boolean): Pro | |||
|     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 teacher = await teacherRepository.findByUsername(username); | ||||
|     if (!teacher) { | ||||
|         throw new Error(`Teacher with username '${username}' not found.`); | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     // Find all learning objects that this teacher manages
 | ||||
|  | @ -120,9 +132,13 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD | |||
|     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); | ||||
| 
 | ||||
|     if (!questions) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return questions; | ||||
|     } | ||||
|  |  | |||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet