feat: groepen van een leerling geimplmenteerd in backend
This commit is contained in:
		
							parent
							
								
									1b096b411b
								
							
						
					
					
						commit
						22cdf58fed
					
				
					 3 changed files with 71 additions and 28 deletions
				
			
		|  | @ -1,7 +1,8 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { | import { | ||||||
|  |     getStudentAssignments, | ||||||
|     getStudentClasses, |     getStudentClasses, | ||||||
|     getStudentClassIds, |     getStudentGroups, | ||||||
|     StudentService, |     StudentService, | ||||||
| } from '../services/students.js'; | } from '../services/students.js'; | ||||||
| import { ClassDTO } from '../interfaces/class.js'; | import { ClassDTO } from '../interfaces/class.js'; | ||||||
|  | @ -52,12 +53,7 @@ export async function getStudentClassesHandler( | ||||||
|         const full = req.query.full === 'true'; |         const full = req.query.full === 'true'; | ||||||
|         const username = req.params.id; |         const username = req.params.id; | ||||||
| 
 | 
 | ||||||
|         let classes: ClassDTO[] | string[]; |         const classes = await getStudentClasses(username, full); | ||||||
|         if (full) { |  | ||||||
|             classes = await getStudentClasses(username); |  | ||||||
|         } else { |  | ||||||
|             classes = await getStudentClassIds(username); |  | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         res.json({ |         res.json({ | ||||||
|             classes: classes, |             classes: classes, | ||||||
|  | @ -85,17 +81,23 @@ export async function getStudentAssignmentsHandler( | ||||||
|     const full = req.query.full === 'true'; |     const full = req.query.full === 'true'; | ||||||
|     const username = req.params.id; |     const username = req.params.id; | ||||||
| 
 | 
 | ||||||
|     const classes = await getStudentClasses(username); |     const assignments = getStudentAssignments(username, full); | ||||||
| 
 |  | ||||||
|     const assignments = ( |  | ||||||
|         await Promise.all( |  | ||||||
|             classes.map(async (cls) => { |  | ||||||
|                 return await getAllAssignments(cls.id, full); |  | ||||||
|             }) |  | ||||||
|         ) |  | ||||||
|     ).flat(); |  | ||||||
| 
 | 
 | ||||||
|     res.json({ |     res.json({ | ||||||
|         assignments: assignments, |         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, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; | import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; | ||||||
| import { Group } from '../../entities/assignments/group.entity.js'; | import { Group } from '../../entities/assignments/group.entity.js'; | ||||||
| import { Assignment } from '../../entities/assignments/assignment.entity.js'; | import { Assignment } from '../../entities/assignments/assignment.entity.js'; | ||||||
|  | import { Student } from '../../entities/users/student.entity.js'; | ||||||
| 
 | 
 | ||||||
| export class GroupRepository extends DwengoEntityRepository<Group> { | export class GroupRepository extends DwengoEntityRepository<Group> { | ||||||
|     public findByAssignmentAndGroupNumber( |     public findByAssignmentAndGroupNumber( | ||||||
|  | @ -23,6 +24,14 @@ export class GroupRepository extends DwengoEntityRepository<Group> { | ||||||
|             populate: ['members'], |             populate: ['members'], | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |     public findAllGroupsWithStudent( | ||||||
|  |         student: Student | ||||||
|  |     ): Promise<Group[]> { | ||||||
|  |         return this.find( | ||||||
|  |             { members: student }, | ||||||
|  |             { populate: ['members'] } | ||||||
|  |         ) | ||||||
|  |     } | ||||||
|     public deleteByAssignmentAndGroupNumber( |     public deleteByAssignmentAndGroupNumber( | ||||||
|         assignment: Assignment, |         assignment: Assignment, | ||||||
|         groupNumber: number |         groupNumber: number | ||||||
|  |  | ||||||
|  | @ -1,10 +1,14 @@ | ||||||
| import { | import { | ||||||
|     getClassRepository, |     getClassRepository, | ||||||
|  |     getGroupRepository, | ||||||
|     getStudentRepository, |     getStudentRepository, | ||||||
| } from '../data/repositories.js'; | } from '../data/repositories.js'; | ||||||
| import { Class } from '../entities/classes/class.entity.js'; | import { Class } from '../entities/classes/class.entity.js'; | ||||||
| import { Student } from '../entities/users/student.entity.js'; | import { Student } from '../entities/users/student.entity.js'; | ||||||
|  | import { AssignmentDTO } from '../interfaces/assignment.js'; | ||||||
| import { ClassDTO, mapToClassDTO } from '../interfaces/class.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'; | import { UserService } from './users.js'; | ||||||
| 
 | 
 | ||||||
| export class StudentService extends UserService<Student> { | 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 studentRepository = getStudentRepository(); | ||||||
|     const student = await studentRepository.findByUsername(username); |     const student = await studentRepository.findByUsername(username); | ||||||
| 
 | 
 | ||||||
|  | @ -24,21 +28,49 @@ async function fetchStudentClasses(username: string): Promise<Class[]> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     const classes = await classRepository.findByStudent(student); |     const classes = await classRepository.findByStudent(student); | ||||||
| 
 | 
 | ||||||
|     if (!classes) { |     if (full) { | ||||||
|         return []; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return classes; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export async function getStudentClasses(username: string): Promise<ClassDTO[]> { |  | ||||||
|     const classes = await fetchStudentClasses(username); |  | ||||||
|         return classes.map(mapToClassDTO); |         return classes.map(mapToClassDTO); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| export async function getStudentClassIds(username: string): Promise<string[]> { |     return classes.map((cls) => cls.classId); | ||||||
|     const classes = await fetchStudentClasses(username); | } | ||||||
|     return classes.map((cls) => { | 
 | ||||||
|         return cls.classId; | export async function getStudentAssignments(username: string, full: boolean): Promise<AssignmentDTO[]> { | ||||||
|     }); // 'class' is a native keyword
 |     const studentRepository = getStudentRepository(); | ||||||
|  |     const student = await studentRepository.findByUsername(username); | ||||||
|  | 
 | ||||||
|  |     if (!student) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     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 getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> { | ||||||
|  |     const studentRepository = getStudentRepository(); | ||||||
|  |     const student = await studentRepository.findByUsername(username); | ||||||
|  | 
 | ||||||
|  |     if (!student) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const groupRepository = getGroupRepository(); | ||||||
|  |     const groups = await groupRepository.findAllGroupsWithStudent(student); | ||||||
|  | 
 | ||||||
|  |     if (full) { | ||||||
|  |         return groups.map(mapToGroupDTO); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return groups.map(mapToGroupDTOId); | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet