fix: classes error handeling en return json
This commit is contained in:
parent
0387f1c699
commit
29824c549e
2 changed files with 59 additions and 84 deletions
|
@ -1,44 +1,37 @@
|
||||||
import { Request, Response } from 'express';
|
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 { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
||||||
|
import {requireFields} from "./error-helper";
|
||||||
|
|
||||||
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
|
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
|
||||||
const full = req.query.full === 'true';
|
const full = req.query.full === 'true';
|
||||||
const classes = await getAllClasses(full);
|
const classes = await getAllClasses(full);
|
||||||
|
|
||||||
res.json({
|
res.json({ classes });
|
||||||
classes: classes,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createClassHandler(req: Request, res: Response): Promise<void> {
|
export async function createClassHandler(req: Request, res: Response): Promise<void> {
|
||||||
|
const displayName= req.body.displayName;
|
||||||
|
requireFields({ displayName });
|
||||||
|
|
||||||
const classData = req.body as ClassDTO;
|
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);
|
const cls = await createClass(classData);
|
||||||
|
|
||||||
if (!cls) {
|
res.json({ cls });
|
||||||
res.status(500).json({ error: 'Something went wrong while creating class' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(201).json(cls);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getClassHandler(req: Request, res: Response): Promise<void> {
|
export async function getClassHandler(req: Request, res: Response): Promise<void> {
|
||||||
const classId = req.params.id;
|
const classId = req.params.id;
|
||||||
const cls = await getClass(classId);
|
requireFields({ classId });
|
||||||
|
|
||||||
if (!cls) {
|
const cls = await getClass(classId);
|
||||||
res.status(404).json({ error: 'Class not found' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json(cls);
|
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> {
|
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {
|
||||||
const classId = req.params.id;
|
const classId = req.params.id;
|
||||||
const full = req.query.full === 'true';
|
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({
|
res.json({ students });
|
||||||
students: 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> {
|
export async function getTeacherInvitationsHandler(req: Request, res: Response): Promise<void> {
|
||||||
const classId = req.params.id;
|
const classId = req.params.id;
|
||||||
const full = req.query.full === 'true';
|
const full = req.query.full === 'true';
|
||||||
|
requireFields({ classId });
|
||||||
|
|
||||||
const invitations = await getClassTeacherInvitations(classId, full);
|
const invitations = await getClassTeacherInvitations(classId, full);
|
||||||
|
|
||||||
res.json({
|
res.json({ invitations });
|
||||||
invitations: invitations,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,10 @@ import { Class } from '../entities/classes/class.entity.js';
|
||||||
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
||||||
import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||||
|
import {fetchTeacher} from "./teachers";
|
||||||
const logger = getLogger();
|
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> {
|
export async function fetchClass(classId: string): Promise<Class> {
|
||||||
const classRepository = getClassRepository();
|
const classRepository = getClassRepository();
|
||||||
|
@ -26,84 +28,56 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
|
||||||
const classRepository = getClassRepository();
|
const classRepository = getClassRepository();
|
||||||
const classes = await classRepository.find({}, { populate: ['students', 'teachers'] });
|
const classes = await classRepository.find({}, { populate: ['students', 'teachers'] });
|
||||||
|
|
||||||
if (!classes) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (full) {
|
if (full) {
|
||||||
return classes.map(mapToClassDTO);
|
return classes.map(mapToClassDTO);
|
||||||
}
|
}
|
||||||
return classes.map((cls) => cls.classId!);
|
return classes.map((cls) => cls.classId!);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> {
|
export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
|
||||||
const teacherRepository = getTeacherRepository();
|
|
||||||
const teacherUsernames = classData.teachers || [];
|
const teacherUsernames = classData.teachers || [];
|
||||||
const teachers = (await Promise.all(teacherUsernames.map(async (id) => teacherRepository.findByUsername(id)))).filter(
|
const teachers = (await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id) )));
|
||||||
(teacher) => teacher !== null
|
|
||||||
);
|
|
||||||
|
|
||||||
const studentRepository = getStudentRepository();
|
|
||||||
const studentUsernames = classData.students || [];
|
const studentUsernames = classData.students || [];
|
||||||
const students = (await Promise.all(studentUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter(
|
const students = (await Promise.all(studentUsernames.map(async (id) => fetchStudent(id) )));
|
||||||
(student) => student !== null
|
|
||||||
);
|
|
||||||
|
|
||||||
const classRepository = getClassRepository();
|
const classRepository = getClassRepository();
|
||||||
|
|
||||||
try {
|
|
||||||
const newClass = classRepository.create({
|
const newClass = classRepository.create({
|
||||||
displayName: classData.displayName,
|
displayName: classData.displayName,
|
||||||
teachers: teachers,
|
teachers: teachers,
|
||||||
students: students,
|
students: students,
|
||||||
});
|
});
|
||||||
await classRepository.save(newClass);
|
await classRepository.save(newClass, {preventOverwrite: true});
|
||||||
|
|
||||||
return mapToClassDTO(newClass);
|
return mapToClassDTO(newClass);
|
||||||
} catch (e) {
|
|
||||||
logger.error(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
return mapToClassDTO(cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchClassStudents(classId: string): Promise<StudentDTO[]> {
|
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
|
||||||
const classRepository = getClassRepository();
|
const cls = await fetchClass(classId);
|
||||||
const cls = await classRepository.findById(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 getClassStudentsIds(classId: string): Promise<string[]> {
|
export async function getClassTeachers(classId: string, full: boolean): Promise<TeacherDTO[] | string[]> {
|
||||||
const students: StudentDTO[] = await fetchClassStudents(classId);
|
const cls = await fetchClass(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[]> {
|
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
|
||||||
const classRepository = getClassRepository();
|
const cls = await fetchClass(classId);
|
||||||
const cls = await classRepository.findById(classId);
|
|
||||||
|
|
||||||
if (!cls) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||||
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls);
|
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue