feat: teacher-class en teacher-students route

This commit is contained in:
Gabriellvl 2025-03-07 23:09:51 +01:00
parent 6b87722469
commit 9c9e7c4870
6 changed files with 146 additions and 21 deletions

View file

@ -10,7 +10,7 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
if (!classes) {
return [];
}
if (full) {
return classes.map(mapToClassDTO);
} else {
@ -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);
}

View file

@ -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);
}