feat: questions via student
This commit is contained in:
		
							parent
							
								
									30ec73a88d
								
							
						
					
					
						commit
						c31b471371
					
				
					 7 changed files with 153 additions and 171 deletions
				
			
		| 
						 | 
				
			
			@ -4,49 +4,40 @@ import {
 | 
			
		|||
    deleteTeacher,
 | 
			
		||||
    getAllTeachers,
 | 
			
		||||
    getClassesByTeacher,
 | 
			
		||||
    getClassIdsByTeacher,
 | 
			
		||||
    getQuestionIdsByTeacher,
 | 
			
		||||
    getQuestionsByTeacher,
 | 
			
		||||
    getStudentIdsByTeacher,
 | 
			
		||||
    getStudentsByTeacher,
 | 
			
		||||
    getTeacher,
 | 
			
		||||
} from '../services/teachers.js';
 | 
			
		||||
import { ClassDTO } from '../interfaces/class.js';
 | 
			
		||||
import { StudentDTO } from '../interfaces/student.js';
 | 
			
		||||
import { QuestionDTO, QuestionId } from '../interfaces/question.js';
 | 
			
		||||
import { Teacher } from '../entities/users/teacher.entity.js';
 | 
			
		||||
import { TeacherDTO } from '../interfaces/teacher.js';
 | 
			
		||||
import { getTeacherRepository } from '../data/repositories.js';
 | 
			
		||||
import {MISSING_FIELDS_ERROR, MISSING_USERNAME_ERROR, NAME_NOT_FOUND_ERROR} from "./users";
 | 
			
		||||
 | 
			
		||||
export async function getAllTeachersHandler(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    const full = req.query.full === 'true';
 | 
			
		||||
 | 
			
		||||
    const teacherRepository = getTeacherRepository();
 | 
			
		||||
 | 
			
		||||
    const teachers: TeacherDTO[] | string[] = full ? await getAllTeachers() : await getAllTeachers();
 | 
			
		||||
    const teachers: TeacherDTO[] | string[] = await getAllTeachers(full);
 | 
			
		||||
 | 
			
		||||
    if (!teachers) {
 | 
			
		||||
        res.status(404).json({ error: `Teacher not found.` });
 | 
			
		||||
        res.status(404).json({ error: `Teachers not found.` });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res.status(201).json(teachers);
 | 
			
		||||
    res.json({teachers});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function getTeacherHandler(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    const username = req.params.username;
 | 
			
		||||
 | 
			
		||||
    if (!username) {
 | 
			
		||||
        res.status(400).json({ error: 'Missing required field: username' });
 | 
			
		||||
        res.status(400).json(MISSING_USERNAME_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const user = await getTeacher(username);
 | 
			
		||||
 | 
			
		||||
    if (!user) {
 | 
			
		||||
        res.status(404).json({
 | 
			
		||||
            error: `User with username '${username}' not found.`,
 | 
			
		||||
        });
 | 
			
		||||
        res.status(404).json(NAME_NOT_FOUND_ERROR(username));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -57,9 +48,7 @@ export async function createTeacherHandler(req: Request, res: Response) {
 | 
			
		|||
    const userData = req.body as TeacherDTO;
 | 
			
		||||
 | 
			
		||||
    if (!userData.username || !userData.firstName || !userData.lastName) {
 | 
			
		||||
        res.status(400).json({
 | 
			
		||||
            error: 'Missing required fields: username, firstName, lastName',
 | 
			
		||||
        });
 | 
			
		||||
        res.status(400).json(MISSING_FIELDS_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -71,15 +60,13 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
 | 
			
		|||
    const username = req.params.username;
 | 
			
		||||
 | 
			
		||||
    if (!username) {
 | 
			
		||||
        res.status(400).json({ error: 'Missing required field: username' });
 | 
			
		||||
        res.status(400).json(MISSING_USERNAME_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const deletedUser = await deleteTeacher(username);
 | 
			
		||||
    if (!deletedUser) {
 | 
			
		||||
        res.status(404).json({
 | 
			
		||||
            error: `User with username '${username}' not found.`,
 | 
			
		||||
        });
 | 
			
		||||
        res.status(404).json(NAME_NOT_FOUND_ERROR(username));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -87,58 +74,43 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
        const username = req.params.username as string;
 | 
			
		||||
        const full = req.query.full === 'true';
 | 
			
		||||
    const username = req.params.username as string;
 | 
			
		||||
    const full = req.query.full === 'true';
 | 
			
		||||
 | 
			
		||||
        if (!username) {
 | 
			
		||||
            res.status(400).json({ error: 'Missing required field: username' });
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const classes: ClassDTO[] | string[] = full ? await getClassesByTeacher(username) : await getClassIdsByTeacher(username);
 | 
			
		||||
 | 
			
		||||
        res.status(201).json(classes);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.error('Error fetching classes by teacher:', error);
 | 
			
		||||
        res.status(500).json({ error: 'Internal server error' });
 | 
			
		||||
    if (!username) {
 | 
			
		||||
        res.status(400).json(MISSING_USERNAME_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const classes: ClassDTO[] | string[] = await getClassesByTeacher(username, full);
 | 
			
		||||
 | 
			
		||||
    res.status(201).json(classes);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
        const username = req.params.username as string;
 | 
			
		||||
        const full = req.query.full === 'true';
 | 
			
		||||
    const username = req.params.username as string;
 | 
			
		||||
    const full = req.query.full === 'true';
 | 
			
		||||
 | 
			
		||||
        if (!username) {
 | 
			
		||||
            res.status(400).json({ error: 'Missing required field: username' });
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const students: StudentDTO[] | string[] = full ? await getStudentsByTeacher(username) : await getStudentIdsByTeacher(username);
 | 
			
		||||
 | 
			
		||||
        res.status(201).json(students);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.error('Error fetching students by teacher:', error);
 | 
			
		||||
        res.status(500).json({ error: 'Internal server error' });
 | 
			
		||||
    if (!username) {
 | 
			
		||||
        res.status(400).json(MISSING_USERNAME_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const students: StudentDTO[] | string[] = await getStudentsByTeacher(username, full);
 | 
			
		||||
 | 
			
		||||
    res.json({students});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
        const username = req.params.username as string;
 | 
			
		||||
        const full = req.query.full === 'true';
 | 
			
		||||
    const username = req.params.username as string;
 | 
			
		||||
    const full = req.query.full === 'true';
 | 
			
		||||
 | 
			
		||||
        if (!username) {
 | 
			
		||||
            res.status(400).json({ error: 'Missing required field: username' });
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const questions: QuestionDTO[] | QuestionId[] = full ? await getQuestionsByTeacher(username) : await getQuestionIdsByTeacher(username);
 | 
			
		||||
 | 
			
		||||
        res.status(201).json(questions);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.error('Error fetching questions by teacher:', error);
 | 
			
		||||
        res.status(500).json({ error: 'Internal server error' });
 | 
			
		||||
    if (!username) {
 | 
			
		||||
        res.status(400).json(MISSING_USERNAME_ERROR);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const questions: QuestionDTO[] | QuestionId[] = await getQuestionsByTeacher(username, full);
 | 
			
		||||
 | 
			
		||||
    res.json({questions});
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue