diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index 2134a9f2..a104979a 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -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 { + const full = req.query.full === 'true'; + const username = req.params.id; + + const groups = await getStudentGroups(username, full); + + res.json({ + groups: groups, + }); +} diff --git a/backend/src/data/assignments/group-repository.ts b/backend/src/data/assignments/group-repository.ts index e9b668b9..56736787 100644 --- a/backend/src/data/assignments/group-repository.ts +++ b/backend/src/data/assignments/group-repository.ts @@ -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 { public findByAssignmentAndGroupNumber( @@ -23,6 +24,14 @@ export class GroupRepository extends DwengoEntityRepository { populate: ['members'], }); } + public findAllGroupsWithStudent( + student: Student + ): Promise { + return this.find( + { members: student }, + { populate: ['members'] } + ) + } public deleteByAssignmentAndGroupNumber( assignment: Assignment, groupNumber: number diff --git a/backend/src/services/students.ts b/backend/src/services/students.ts index 952c3cf1..fc0a8e1d 100644 --- a/backend/src/services/students.ts +++ b/backend/src/services/students.ts @@ -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 { @@ -13,7 +17,7 @@ export class StudentService extends UserService { } } -async function fetchStudentClasses(username: string): Promise { +export async function getStudentClasses(username: string, full: boolean): Promise { const studentRepository = getStudentRepository(); const student = await studentRepository.findByUsername(username); @@ -24,21 +28,49 @@ async function fetchStudentClasses(username: string): Promise { 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 { + 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 { - const classes = await fetchStudentClasses(username); - return classes.map(mapToClassDTO); -} +export async function getStudentGroups(username: string, full: boolean): Promise { + const studentRepository = getStudentRepository(); + const student = await studentRepository.findByUsername(username); -export async function getStudentClassIds(username: string): Promise { - 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); }