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,7 +48,10 @@ 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' }); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; | ||||
| import { Class } from '../../entities/classes/class.entity.js'; | ||||
| import { Student } from '../../entities/users/student.entity.js'; | ||||
| import {Teacher} from "../../entities/users/teacher.entity"; | ||||
| 
 | ||||
| export class ClassRepository extends DwengoEntityRepository<Class> { | ||||
|     public findById(id: string): Promise<Class | null> { | ||||
|  | @ -18,4 +19,11 @@ export class ClassRepository extends DwengoEntityRepository<Class> { | |||
|             { populate: ["students", "teachers"] } // voegt student en teacher objecten toe
 | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     public findByTeacher(teacher: Teacher): Promise<Class[]> { | ||||
|         return this.find( | ||||
|             { teachers: teacher }, | ||||
|             { populate: ["students", "teachers"] } | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,5 +1,10 @@ | |||
| import express from 'express' | ||||
| import {createTeacherHandler, deleteTeacherHandler, getTeacherHandler} from "../controllers/teachers.js"; | ||||
| import { | ||||
|     createTeacherHandler, | ||||
|     deleteTeacherHandler, | ||||
|     getTeacherClassHandler, | ||||
|     getTeacherHandler, getTeacherStudentHandler | ||||
| } from "../controllers/teachers.js"; | ||||
| const router = express.Router(); | ||||
| 
 | ||||
| // root endpoint used to search objects
 | ||||
|  | @ -9,6 +14,10 @@ router.post('/', createTeacherHandler); | |||
| 
 | ||||
| router.delete('/:username', deleteTeacherHandler); | ||||
| 
 | ||||
| router.get('/:username/classes', getTeacherClassHandler); | ||||
| 
 | ||||
| router.get('/:username/students', getTeacherStudentHandler); | ||||
| 
 | ||||
| // the questions students asked a teacher
 | ||||
| router.get('/:id/questions', (req, res) => { | ||||
|     res.json({ | ||||
|  |  | |||
|  | @ -28,17 +28,21 @@ export async function getClass(classId: string): Promise<ClassDTO | null> { | |||
|     } | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> { | ||||
| async function fetchClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|     if (!cls) | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return cls.students.map(mapToStudentDTO); | ||||
|     } else { | ||||
|         return cls.students.map((student) => student.username); | ||||
|     } | ||||
|     return cls.students.map(mapToStudentDTO); | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudents(classId: string): Promise<StudentDTO[]> { | ||||
|     return await fetchClassStudents(classId); | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudentsIds(classId: string): Promise<string[]> { | ||||
|     return await fetchClassStudents(classId).map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,15 +1,26 @@ | |||
| import {getTeacherRepository} from "../data/repositories.js"; | ||||
| import {getClassRepository, getTeacherRepository} from "../data/repositories.js"; | ||||
| import {mapToTeacher, mapToTeacherDTO, TeacherDTO} from "../interfaces/teacher.js"; | ||||
| import { Teacher } from "../entities/users/teacher.entity"; | ||||
| import {ClassDTO, mapToClassDTO} from "../interfaces/classes"; | ||||
| import {getClassStudents, getClassStudentsIds} from "./class"; | ||||
| import {StudentDTO} from "../interfaces/students"; | ||||
| 
 | ||||
| 
 | ||||
| export async function fetchAllTeachers(): Promise<TeacherDTO[]> { | ||||
| async function fetchAllTeachers(): Promise<TeacherDTO[]> { | ||||
|     const teacherRepository = getTeacherRepository(); | ||||
|     const teachers = await teacherRepository.find({}); | ||||
| 
 | ||||
|     return teachers.map(mapToTeacherDTO); | ||||
| } | ||||
| 
 | ||||
| export async function getAllTeachers(): Promise<TeacherDTO[]> { | ||||
|     return await fetchAllTeachers(); | ||||
| } | ||||
| 
 | ||||
| export async function getAllTeachersIds(): Promise<string[]> { | ||||
|     return await fetchAllTeachers().map((teacher) => teacher.username) | ||||
| } | ||||
| 
 | ||||
| export async function createTeacher(teacherData: TeacherDTO): Promise<Teacher> { | ||||
|     const teacherRepository = getTeacherRepository(); | ||||
|     const newTeacher = mapToTeacher(teacherData); | ||||
|  | @ -36,3 +47,42 @@ export async function deleteTeacher(username: string): Promise<TeacherDTO | null | |||
|     return teacher; | ||||
| } | ||||
| 
 | ||||
| async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { | ||||
|     const teacherRepository = getTeacherRepository(); | ||||
|     const classRepository = getClassRepository(); | ||||
| 
 | ||||
|     const teacher = await teacherRepository.findByUsername(username); | ||||
|     if (!teacher) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     const classes = await classRepository.findByTeacher(teacher); | ||||
|     return classes.map(mapToClassDTO); | ||||
| } | ||||
| 
 | ||||
| export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> { | ||||
|     return await fetchClassesByTeacher(username) | ||||
| } | ||||
| 
 | ||||
| export async function getClassIdsByTeacher(): Promise<string[]> { | ||||
|     return await fetchClassesByTeacher(username).map((cls) => cls.id); | ||||
| } | ||||
| 
 | ||||
| async function fetchStudentsByTeacher(username: string) { | ||||
|     const classes = await getClassIdsByTeacher(); | ||||
| 
 | ||||
|     return Promise.all( | ||||
|         classes.map( async (id) => getClassStudents(id)) | ||||
|     ); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> { | ||||
|     return await fetchStudentsByTeacher(username); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentIdsByTeacher(): Promise<string[]> { | ||||
|     return await fetchStudentsByTeacher(username).map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl