feat: teacher-class en teacher-students route
This commit is contained in:
parent
6b87722469
commit
9c9e7c4870
6 changed files with 146 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getAllClasses, getClass, getClassStudents } from '../services/class';
|
||||
import {getAllClasses, getClass, getClassStudents, getClassStudentsIds} from '../services/class';
|
||||
import { ClassDTO } from '../interfaces/classes';
|
||||
|
||||
export async function getAllClassesHandler(
|
||||
|
@ -48,9 +48,12 @@ export async function getClassStudentsHandler(
|
|||
const classId = req.params.id;
|
||||
const full = req.query.full === "true";
|
||||
|
||||
const students = await getClassStudents(classId, full);
|
||||
let students;
|
||||
|
||||
if (full) students = await getClassStudents(classId);
|
||||
else students = await getClassStudentsIds(classId);
|
||||
|
||||
res.json({
|
||||
students: students,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
import { Request, Response } from 'express';
|
||||
import {
|
||||
createTeacher, deleteTeacher,
|
||||
fetchAllTeachers, fetchTeacherByUsername
|
||||
createTeacher,
|
||||
deleteTeacher,
|
||||
fetchTeacherByUsername,
|
||||
getClassesByTeacher,
|
||||
getClassIdsByTeacher,
|
||||
getAllTeachers,
|
||||
getAllTeachersIds, getStudentsByTeacher, getStudentIdsByTeacher
|
||||
} from '../services/teachers.js';
|
||||
import {TeacherDTO} from "../interfaces/teacher";
|
||||
import {ClassDTO} from "../interfaces/classes";
|
||||
import {StudentDTO} from "../interfaces/students";
|
||||
|
||||
export async function getTeacherHandler(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
|
@ -20,10 +27,10 @@ export async function getTeacherHandler(req: Request, res: Response): Promise<vo
|
|||
return;
|
||||
}
|
||||
|
||||
let teachers: TeacherDTO[] | string[] = await fetchAllTeachers();
|
||||
let teachers: TeacherDTO[] | string[];
|
||||
|
||||
if (!full)
|
||||
teachers = teachers.map((teacher) => teacher.username)
|
||||
if (full) teachers = await getAllTeachers();
|
||||
else teachers = await getAllTeachersIds();
|
||||
|
||||
res.json(teachers);
|
||||
} catch (error) {
|
||||
|
@ -77,3 +84,47 @@ export async function deleteTeacherHandler(
|
|||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
let classes: ClassDTO[] | string[];
|
||||
|
||||
if (full) classes = await getClassesByTeacher(username);
|
||||
else classes = 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' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
let students: StudentDTO[] | string[];
|
||||
|
||||
if (full) students = await getStudentsByTeacher(username);
|
||||
else students = 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' });
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue