feat: endpoint voor alle groepen van een assignment geimplementeerd
This commit is contained in:
		
							parent
							
								
									3f62ab70e1
								
							
						
					
					
						commit
						7e051d412a
					
				
					 4 changed files with 62 additions and 13 deletions
				
			
		|  | @ -1,11 +1,11 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { getGroup } from '../services/groups'; | import { getAllGroups, getGroup } from '../services/groups'; | ||||||
| 
 | 
 | ||||||
| // typescript is annoywith with parameter forwarding from class.ts
 | // typescript is annoywith with parameter forwarding from class.ts
 | ||||||
| interface GroupParams { | interface GroupParams { | ||||||
|     classid: string; |     classid: string; | ||||||
|     assignmentid: string; |     assignmentid: string; | ||||||
|     groupid: string; |     groupid?: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getGroupHandler( | export async function getGroupHandler( | ||||||
|  | @ -21,7 +21,7 @@ export async function getGroupHandler( | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const groupId = +req.params.groupid; |     const groupId = +req.params.groupid!; // can't be undefined
 | ||||||
| 
 | 
 | ||||||
|     if (isNaN(groupId)) { |     if (isNaN(groupId)) { | ||||||
|         res.status(400).json({ error: "Group id must be a number" }); |         res.status(400).json({ error: "Group id must be a number" }); | ||||||
|  | @ -31,4 +31,25 @@ export async function getGroupHandler( | ||||||
|     const group = await getGroup(classId, assignmentId, groupId, full); |     const group = await getGroup(classId, assignmentId, groupId, full); | ||||||
| 
 | 
 | ||||||
|     res.json(group); |     res.json(group); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function getAllGroupsHandler( | ||||||
|  |     req: Request, | ||||||
|  |     res: Response, | ||||||
|  | ): Promise<void> { | ||||||
|  |     const classId = req.params.classid; | ||||||
|  |     const full = req.query.full === "true"; | ||||||
|  | 
 | ||||||
|  |     const assignmentId = +req.params.assignmentid; | ||||||
|  | 
 | ||||||
|  |     if (isNaN(assignmentId)) { | ||||||
|  |         res.status(400).json({ error: "Assignment id must be a number" }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const groups = await getAllGroups(classId, assignmentId, full); | ||||||
|  | 
 | ||||||
|  |     res.json({ | ||||||
|  |         groups: groups, | ||||||
|  |     }); | ||||||
| } | } | ||||||
|  | @ -18,7 +18,11 @@ export class GroupRepository extends DwengoEntityRepository<Group> { | ||||||
|     public findAllGroupsForAssignment( |     public findAllGroupsForAssignment( | ||||||
|         assignment: Assignment |         assignment: Assignment | ||||||
|     ): Promise<Group[]> { |     ): Promise<Group[]> { | ||||||
|         return this.findAll({ where: { assignment: assignment } }); |         return this.findAll({  | ||||||
|  |             where: { assignment: assignment },  | ||||||
|  |             populate: ["members"]  | ||||||
|  |         }, | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
|     public deleteByAssignmentAndGroupNumber( |     public deleteByAssignmentAndGroupNumber( | ||||||
|         assignment: Assignment, |         assignment: Assignment, | ||||||
|  |  | ||||||
|  | @ -1,16 +1,9 @@ | ||||||
| import express from 'express' | import express from 'express' | ||||||
| import { getGroupHandler } from '../controllers/groups'; | import { getAllGroupsHandler, getGroupHandler } from '../controllers/groups'; | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
| // root endpoint used to search objects
 | // root endpoint used to search objects
 | ||||||
| router.get('/', (req, res) => { | router.get('/', getAllGroupsHandler); | ||||||
|     res.json({ |  | ||||||
|         groups: [ |  | ||||||
|             '0', |  | ||||||
|             '1', |  | ||||||
|         ] |  | ||||||
|     }); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| // information about a group (members, ... [TODO DOC])
 | // information about a group (members, ... [TODO DOC])
 | ||||||
| router.get('/:groupid', getGroupHandler); | router.get('/:groupid', getGroupHandler); | ||||||
|  |  | ||||||
|  | @ -33,4 +33,35 @@ export async function getGroup( | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return mapToGroupDTOId(group); |     return mapToGroupDTOId(group); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function getAllGroups( | ||||||
|  |     classId: string, | ||||||
|  |     assignmentNumber: number, | ||||||
|  |     full: boolean, | ||||||
|  | ): Promise<GroupDTO[]> { | ||||||
|  |     const classRepository = getClassRepository(); | ||||||
|  |     const cls = await classRepository.findById(classId); | ||||||
|  | 
 | ||||||
|  |     if (!cls) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const assignmentRepository = getAssignmentRepository(); | ||||||
|  |     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||||
|  | 
 | ||||||
|  |     if (!assignment) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const groupRepository = getGroupRepository(); | ||||||
|  |     const groups = await groupRepository.findAllGroupsForAssignment(assignment); | ||||||
|  | 
 | ||||||
|  |     if (full) { | ||||||
|  |         console.log('full'); | ||||||
|  |         console.log(groups); | ||||||
|  |         return groups.map(mapToGroupDTO); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return groups.map(mapToGroupDTOId); | ||||||
| } | } | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet