From 29824c549ed90da314a8f1c29b985e3f3fd0b021 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Sun, 6 Apr 2025 16:26:28 +0200 Subject: [PATCH] fix: classes error handeling en return json --- backend/src/controllers/classes.ts | 61 +++++++++++----------- backend/src/services/classes.ts | 82 ++++++++++-------------------- 2 files changed, 59 insertions(+), 84 deletions(-) diff --git a/backend/src/controllers/classes.ts b/backend/src/controllers/classes.ts index a041bf22..8e9efdcb 100644 --- a/backend/src/controllers/classes.ts +++ b/backend/src/controllers/classes.ts @@ -1,44 +1,37 @@ import { Request, Response } from 'express'; -import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/classes.js'; +import { + createClass, + getAllClasses, + getClass, + getClassStudents, + getClassTeacherInvitations, + getClassTeachers +} from '../services/classes.js'; import { ClassDTO } from '@dwengo-1/common/interfaces/class'; +import {requireFields} from "./error-helper"; export async function getAllClassesHandler(req: Request, res: Response): Promise { const full = req.query.full === 'true'; const classes = await getAllClasses(full); - res.json({ - classes: classes, - }); + res.json({ classes }); } export async function createClassHandler(req: Request, res: Response): Promise { + const displayName= req.body.displayName; + requireFields({ displayName }); + const classData = req.body as ClassDTO; - - if (!classData.displayName) { - res.status(400).json({ - error: 'Missing one or more required fields: displayName', - }); - return; - } - const cls = await createClass(classData); - if (!cls) { - res.status(500).json({ error: 'Something went wrong while creating class' }); - return; - } - - res.status(201).json(cls); + res.json({ cls }); } export async function getClassHandler(req: Request, res: Response): Promise { const classId = req.params.id; - const cls = await getClass(classId); + requireFields({ classId }); - if (!cls) { - res.status(404).json({ error: 'Class not found' }); - return; - } + const cls = await getClass(classId); res.json(cls); } @@ -46,21 +39,29 @@ export async function getClassHandler(req: Request, res: Response): Promise { const classId = req.params.id; const full = req.query.full === 'true'; + requireFields({ classId }); - const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId); + const students = await getClassStudents(classId, full); - res.json({ - students: students, - }); + res.json({ students }); +} + +export async function getClassTeachersHandler(req: Request, res: Response): Promise { + const classId = req.params.id; + const full = req.query.full === 'true'; + requireFields({ classId }); + + const teachers = await getClassTeachers(classId, full); + + res.json({ teachers }); } export async function getTeacherInvitationsHandler(req: Request, res: Response): Promise { const classId = req.params.id; const full = req.query.full === 'true'; + requireFields({ classId }); const invitations = await getClassTeacherInvitations(classId, full); - res.json({ - invitations: invitations, - }); + res.json({ invitations }); } diff --git a/backend/src/services/classes.ts b/backend/src/services/classes.ts index 754277cf..d4e4defa 100644 --- a/backend/src/services/classes.ts +++ b/backend/src/services/classes.ts @@ -8,8 +8,10 @@ import { Class } from '../entities/classes/class.entity.js'; import { ClassDTO } from '@dwengo-1/common/interfaces/class'; import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation'; import { StudentDTO } from '@dwengo-1/common/interfaces/student'; - -const logger = getLogger(); +import {fetchTeacher} from "./teachers"; +import {fetchStudent} from "./students"; +import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher"; +import {mapToTeacherDTO} from "../interfaces/teacher"; export async function fetchClass(classId: string): Promise { const classRepository = getClassRepository(); @@ -26,84 +28,56 @@ export async function getAllClasses(full: boolean): Promise cls.classId!); } -export async function createClass(classData: ClassDTO): Promise { - const teacherRepository = getTeacherRepository(); +export async function createClass(classData: ClassDTO): Promise { const teacherUsernames = classData.teachers || []; - const teachers = (await Promise.all(teacherUsernames.map(async (id) => teacherRepository.findByUsername(id)))).filter( - (teacher) => teacher !== null - ); + const teachers = (await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id) ))); - const studentRepository = getStudentRepository(); const studentUsernames = classData.students || []; - const students = (await Promise.all(studentUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter( - (student) => student !== null - ); + const students = (await Promise.all(studentUsernames.map(async (id) => fetchStudent(id) ))); const classRepository = getClassRepository(); - try { - const newClass = classRepository.create({ - displayName: classData.displayName, - teachers: teachers, - students: students, - }); - await classRepository.save(newClass); + const newClass = classRepository.create({ + displayName: classData.displayName, + teachers: teachers, + students: students, + }); + await classRepository.save(newClass, {preventOverwrite: true}); - return mapToClassDTO(newClass); - } catch (e) { - logger.error(e); - return null; - } + return mapToClassDTO(newClass); } -export async function getClass(classId: string): Promise { - const classRepository = getClassRepository(); - const cls = await classRepository.findById(classId); - - if (!cls) { - return null; - } - +export async function getClass(classId: string): Promise { + const cls = await fetchClass(classId); return mapToClassDTO(cls); } -async function fetchClassStudents(classId: string): Promise { - const classRepository = getClassRepository(); - const cls = await classRepository.findById(classId); +export async function getClassStudents(classId: string, full: boolean): Promise { + const cls = await fetchClass(classId); - if (!cls) { - return []; + if (full){ + return cls.students.map(mapToStudentDTO); } - - return cls.students.map(mapToStudentDTO); + return cls.students.map((student) => student.username); } -export async function getClassStudents(classId: string): Promise { - return await fetchClassStudents(classId); -} +export async function getClassTeachers(classId: string, full: boolean): Promise { + const cls = await fetchClass(classId); -export async function getClassStudentsIds(classId: string): Promise { - const students: StudentDTO[] = await fetchClassStudents(classId); - return students.map((student) => student.username); + if (full){ + return cls.teachers.map(mapToTeacherDTO); + } + return cls.teachers.map((student) => student.username); } 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);