feat: groepen van een leerling geimplmenteerd in backend

This commit is contained in:
Adriaan Jacquet 2025-03-10 11:24:37 +01:00
parent 1b096b411b
commit 22cdf58fed
3 changed files with 71 additions and 28 deletions

View file

@ -1,7 +1,8 @@
import { Request, Response } from 'express';
import {
getStudentAssignments,
getStudentClasses,
getStudentClassIds,
getStudentGroups,
StudentService,
} from '../services/students.js';
import { ClassDTO } from '../interfaces/class.js';
@ -52,12 +53,7 @@ export async function getStudentClassesHandler(
const full = req.query.full === 'true';
const username = req.params.id;
let classes: ClassDTO[] | string[];
if (full) {
classes = await getStudentClasses(username);
} else {
classes = await getStudentClassIds(username);
}
const classes = await getStudentClasses(username, full);
res.json({
classes: classes,
@ -85,17 +81,23 @@ export async function getStudentAssignmentsHandler(
const full = req.query.full === 'true';
const username = req.params.id;
const classes = await getStudentClasses(username);
const assignments = (
await Promise.all(
classes.map(async (cls) => {
return await getAllAssignments(cls.id, full);
})
)
).flat();
const assignments = getStudentAssignments(username, full);
res.json({
assignments: assignments,
});
}
export async function getStudentGroupsHandler(
req: Request,
res: Response,
): Promise<void> {
const full = req.query.full === 'true';
const username = req.params.id;
const groups = await getStudentGroups(username, full);
res.json({
groups: groups,
});
}

View file

@ -1,6 +1,7 @@
import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
import { Group } from '../../entities/assignments/group.entity.js';
import { Assignment } from '../../entities/assignments/assignment.entity.js';
import { Student } from '../../entities/users/student.entity.js';
export class GroupRepository extends DwengoEntityRepository<Group> {
public findByAssignmentAndGroupNumber(
@ -23,6 +24,14 @@ export class GroupRepository extends DwengoEntityRepository<Group> {
populate: ['members'],
});
}
public findAllGroupsWithStudent(
student: Student
): Promise<Group[]> {
return this.find(
{ members: student },
{ populate: ['members'] }
)
}
public deleteByAssignmentAndGroupNumber(
assignment: Assignment,
groupNumber: number

View file

@ -1,10 +1,14 @@
import {
getClassRepository,
getGroupRepository,
getStudentRepository,
} from '../data/repositories.js';
import { Class } from '../entities/classes/class.entity.js';
import { Student } from '../entities/users/student.entity.js';
import { AssignmentDTO } from '../interfaces/assignment.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
import { getAllAssignments } from './assignments.js';
import { UserService } from './users.js';
export class StudentService extends UserService<Student> {
@ -13,7 +17,7 @@ export class StudentService extends UserService<Student> {
}
}
async function fetchStudentClasses(username: string): Promise<Class[]> {
export async function getStudentClasses(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
@ -24,21 +28,49 @@ async function fetchStudentClasses(username: string): Promise<Class[]> {
const classRepository = getClassRepository();
const classes = await classRepository.findByStudent(student);
if (!classes) {
if (full) {
return classes.map(mapToClassDTO);
}
return classes.map((cls) => cls.classId);
}
export async function getStudentAssignments(username: string, full: boolean): Promise<AssignmentDTO[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
if (!student) {
return [];
}
return classes;
const classRepository = getClassRepository();
const classes = await classRepository.findByStudent(student);
const assignments = (
await Promise.all(
classes.map(async (cls) => {
return await getAllAssignments(cls.classId, full);
})
)
).flat();
return assignments;
}
export async function getStudentClasses(username: string): Promise<ClassDTO[]> {
const classes = await fetchStudentClasses(username);
return classes.map(mapToClassDTO);
}
export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
export async function getStudentClassIds(username: string): Promise<string[]> {
const classes = await fetchStudentClasses(username);
return classes.map((cls) => {
return cls.classId;
}); // 'class' is a native keyword
if (!student) {
return [];
}
const groupRepository = getGroupRepository();
const groups = await groupRepository.findAllGroupsWithStudent(student);
if (full) {
return groups.map(mapToGroupDTO);
}
return groups.map(mapToGroupDTOId);
}