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 { | ||||
|     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, | ||||
|     }); | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     return classes; | ||||
| } | ||||
| 
 | ||||
| export async function getStudentClasses(username: string): Promise<ClassDTO[]> { | ||||
|     const classes = await fetchStudentClasses(username); | ||||
|     if (full) { | ||||
|         return classes.map(mapToClassDTO); | ||||
|     } | ||||
| 
 | ||||
| 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
 | ||||
|     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 []; | ||||
|     } | ||||
| 
 | ||||
|     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