feat: verbinding tussen groepen en hun submissions
This commit is contained in:
		
							parent
							
								
									d8b97f3aea
								
							
						
					
					
						commit
						e8975878aa
					
				
					 3 changed files with 72 additions and 2 deletions
				
			
		|  | @ -1,5 +1,5 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { getAllGroups, getGroup } from '../services/groups.js'; | import { getAllGroups, getGroup, getGroupSubmissions } from '../services/groups.js'; | ||||||
| 
 | 
 | ||||||
| // Typescript is annoywith with parameter forwarding from class.ts
 | // Typescript is annoywith with parameter forwarding from class.ts
 | ||||||
| interface GroupParams { | interface GroupParams { | ||||||
|  | @ -53,3 +53,31 @@ export async function getAllGroupsHandler( | ||||||
|         groups: groups, |         groups: groups, | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export async function getGroupSubmissionsHandler( | ||||||
|  |     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 groupId = +req.params.groupid!; // Can't be undefined
 | ||||||
|  | 
 | ||||||
|  |     if (isNaN(groupId)) { | ||||||
|  |         res.status(400).json({ error: 'Group id must be a number' }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const submissions = await getGroupSubmissions(classId, assignmentId, groupId); | ||||||
|  | 
 | ||||||
|  |     res.json({ | ||||||
|  |         submissions: submissions, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import express from 'express'; | import express from 'express'; | ||||||
| import { getAllGroupsHandler, getGroupHandler } from '../controllers/groups.js'; | import { getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../controllers/groups.js'; | ||||||
| 
 | 
 | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
|  | @ -9,6 +9,8 @@ router.get('/', getAllGroupsHandler); | ||||||
| // Information about a group (members, ... [TODO DOC])
 | // Information about a group (members, ... [TODO DOC])
 | ||||||
| router.get('/:groupid', getGroupHandler); | router.get('/:groupid', getGroupHandler); | ||||||
| 
 | 
 | ||||||
|  | router.get('/:groupid', getGroupSubmissionsHandler); | ||||||
|  | 
 | ||||||
| // The list of questions a group has made
 | // The list of questions a group has made
 | ||||||
| router.get('/:id/question', (req, res) => { | router.get('/:id/question', (req, res) => { | ||||||
|     res.json({ |     res.json({ | ||||||
|  |  | ||||||
|  | @ -2,12 +2,14 @@ import { | ||||||
|     getAssignmentRepository, |     getAssignmentRepository, | ||||||
|     getClassRepository, |     getClassRepository, | ||||||
|     getGroupRepository, |     getGroupRepository, | ||||||
|  |     getSubmissionRepository, | ||||||
| } from '../data/repositories.js'; | } from '../data/repositories.js'; | ||||||
| import { | import { | ||||||
|     GroupDTO, |     GroupDTO, | ||||||
|     mapToGroupDTO, |     mapToGroupDTO, | ||||||
|     mapToGroupDTOId, |     mapToGroupDTOId, | ||||||
| } from '../interfaces/group.js'; | } from '../interfaces/group.js'; | ||||||
|  | import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; | ||||||
| 
 | 
 | ||||||
| export async function getGroup( | export async function getGroup( | ||||||
|     classId: string, |     classId: string, | ||||||
|  | @ -82,3 +84,41 @@ export async function getAllGroups( | ||||||
| 
 | 
 | ||||||
|     return groups.map(mapToGroupDTOId); |     return groups.map(mapToGroupDTOId); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export async function getGroupSubmissions( | ||||||
|  |     classId: string, | ||||||
|  |     assignmentNumber: number, | ||||||
|  |     groupNumber: number, | ||||||
|  | ): Promise<SubmissionDTO[]> { | ||||||
|  |     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 group = await groupRepository.findByAssignmentAndGroupNumber( | ||||||
|  |         assignment, | ||||||
|  |         groupNumber | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     if (!group) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const submissionRepository = getSubmissionRepository(); | ||||||
|  |     const submissions = await submissionRepository.findAllSubmissionsForGroup(group); | ||||||
|  | 
 | ||||||
|  |     return submissions.map(mapToSubmissionDTO); | ||||||
|  | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet