fix: classes error handeling en return json

This commit is contained in:
Gabriellvl 2025-04-06 16:26:28 +02:00
parent 0387f1c699
commit 29824c549e
2 changed files with 59 additions and 84 deletions

View file

@ -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<void> {
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<void> {
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<void> {
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<void
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {
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<void> {
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<void> {
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 });
}

View file

@ -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<Class> {
const classRepository = getClassRepository();
@ -26,84 +28,56 @@ 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 createClass(classData: ClassDTO): Promise<ClassDTO | null> {
const teacherRepository = getTeacherRepository();
export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
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<ClassDTO | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
if (!cls) {
return null;
}
export async function getClass(classId: string): Promise<ClassDTO> {
const cls = await fetchClass(classId);
return mapToClassDTO(cls);
}
async function fetchClassStudents(classId: string): Promise<StudentDTO[]> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
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);
}
return cls.students.map(mapToStudentDTO);
return cls.students.map((student) => student.username);
}
export async function getClassStudents(classId: string): Promise<StudentDTO[]> {
return await fetchClassStudents(classId);
}
export async function getClassTeachers(classId: string, full: boolean): Promise<TeacherDTO[] | string[]> {
const cls = await fetchClass(classId);
export async function getClassStudentsIds(classId: string): Promise<string[]> {
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<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);