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 { 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 });
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue