diff --git a/backend/src/controllers/groups.ts b/backend/src/controllers/groups.ts index c5087d02..38d5d5d0 100644 --- a/backend/src/controllers/groups.ts +++ b/backend/src/controllers/groups.ts @@ -28,6 +28,11 @@ export async function getGroupHandler(req: Request, res: Response): const group = await getGroup(classId, assignmentId, groupId, full); + if (!group) { + res.status(404).json({ error: 'Group not found' }); + return; + } + res.json(group); } diff --git a/backend/src/controllers/questions.ts b/backend/src/controllers/questions.ts index 604954b2..00a51329 100644 --- a/backend/src/controllers/questions.ts +++ b/backend/src/controllers/questions.ts @@ -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); } diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index e96deaad..adc88ca2 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -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); } diff --git a/backend/src/controllers/teachers.ts b/backend/src/controllers/teachers.ts index 559e0d3d..726b76c0 100644 --- a/backend/src/controllers/teachers.ts +++ b/backend/src/controllers/teachers.ts @@ -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 }); } diff --git a/backend/src/services/assignments.ts b/backend/src/services/assignments.ts index 688f9a79..075f01d3 100644 --- a/backend/src/services/assignments.ts +++ b/backend/src/services/assignments.ts @@ -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 { +export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise { 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; } diff --git a/backend/src/services/classes.ts b/backend/src/services/classes.ts index 9f6e1efe..8d69b1e3 100644 --- a/backend/src/services/classes.ts +++ b/backend/src/services/classes.ts @@ -21,7 +21,7 @@ export async function getAllClasses(full: boolean): Promise cls.classId!); } -export async function createClass(classData: ClassDTO): Promise { +export async function createClass(classData: ClassDTO): Promise { 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 { }); await classRepository.save(newClass); - return newClass; + return mapToClassDTO(newClass); } catch (e) { logger.error(e); return null; diff --git a/backend/src/services/questions.ts b/backend/src/services/questions.ts index ee003bcd..0e52440f 100644 --- a/backend/src/services/questions.ts +++ b/backend/src/services/questions.ts @@ -103,5 +103,5 @@ export async function deleteQuestion(questionId: QuestionId) { return null; } - return question; + return mapToQuestionDTO(question); } diff --git a/backend/src/services/submissions.ts b/backend/src/services/submissions.ts index a8fa96c7..0e1ad9ac 100644 --- a/backend/src/services/submissions.ts +++ b/backend/src/services/submissions.ts @@ -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) { diff --git a/backend/src/services/teachers.ts b/backend/src/services/teachers.ts index 464a809f..77cea501 100644 --- a/backend/src/services/teachers.ts +++ b/backend/src/services/teachers.ts @@ -64,11 +64,11 @@ export async function deleteTeacher(username: string): Promise { +export async function fetchClassesByTeacher(username: string): Promise { 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 { +export async function getClassesByTeacher(username: string, full: boolean): Promise { 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 { 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 { +export async function getStudentsByTeacher(username: string, full: boolean): Promise { 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 { +export async function fetchTeacherQuestions(username: string): Promise { 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 { +export async function getQuestionsByTeacher(username: string, full: boolean): Promise { const questions = await fetchTeacherQuestions(username); + if (!questions) { + return null; + } + if (full) { return questions; }