Merge branch 'dev' into refactor/linting

This commit is contained in:
Tibo De Peuter 2025-03-30 11:27:59 +02:00
commit f9b59af2fd
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
22 changed files with 256 additions and 344 deletions

View file

@ -1,20 +1,25 @@
import { getClassRepository, getLearningObjectRepository, getQuestionRepository, getTeacherRepository } from '../data/repositories.js';
import {
getClassRepository,
getLearningObjectRepository,
getQuestionRepository,
getTeacherRepository,
} from '../data/repositories.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { getClassStudents } from './class.js';
import { getClassStudents } from './classes.js';
import { StudentDTO } from '../interfaces/student.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
import { getLogger } from '../logging/initalize.js';
export async function getAllTeachers(): Promise<TeacherDTO[]> {
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository = getTeacherRepository();
const users = await teacherRepository.findAll();
return users.map(mapToTeacherDTO);
}
const teachers = await teacherRepository.findAll();
export async function getAllTeacherIds(): Promise<string[]> {
const users = await getAllTeachers();
return users.map((user) => user.username);
if (full) {
return teachers.map(mapToTeacherDTO);
}
return teachers.map((teacher) => teacher.username);
}
export async function getTeacher(username: string): Promise<TeacherDTO | null> {
@ -56,11 +61,11 @@ export async function deleteTeacher(username: string): Promise<TeacherDTO | null
}
}
export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[] | null> {
const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username);
if (!teacher) {
return [];
return null;
}
const classRepository = getClassRepository();
@ -68,35 +73,49 @@ export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[
return classes.map(mapToClassDTO);
}
export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> {
return await fetchClassesByTeacher(username);
}
export async function getClassIdsByTeacher(username: string): Promise<string[]> {
export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[] | null> {
const classes = await fetchClassesByTeacher(username);
if (!classes) {
return null;
}
if (full) {
return classes;
}
return classes.map((cls) => cls.id);
}
export async function fetchStudentsByTeacher(username: string): Promise<StudentDTO[]> {
const classes = await getClassIdsByTeacher(username);
export async function fetchStudentsByTeacher(username: string): Promise<StudentDTO[] | null> {
const classes = (await getClassesByTeacher(username, false)) as string[];
if (!classes) {
return null;
}
return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat();
}
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
return await fetchStudentsByTeacher(username);
}
export async function getStudentIdsByTeacher(username: string): Promise<string[]> {
export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[] | null> {
const students = await fetchStudentsByTeacher(username);
if (!students) {
return null;
}
if (full) {
return students;
}
return students.map((student) => student.username);
}
export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> {
export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[] | null> {
const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username);
if (!teacher) {
throw new Error(`Teacher with username '${username}' not found.`);
return null;
}
// Find all learning objects that this teacher manages
@ -110,12 +129,16 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD
return questions.map(mapToQuestionDTO);
}
export async function getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
return await fetchTeacherQuestions(username);
}
export async function getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
export async function getQuestionsByTeacher(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[] | null> {
const questions = await fetchTeacherQuestions(username);
if (!questions) {
return null;
}
if (full) {
return questions;
}
return questions.map(mapToQuestionId);
}