feat: group verbinding tussen databank en api aangemaakt
This commit is contained in:
		
							parent
							
								
									baf43e91de
								
							
						
					
					
						commit
						3f62ab70e1
					
				
					 6 changed files with 104 additions and 24 deletions
				
			
		
							
								
								
									
										34
									
								
								backend/src/controllers/groups.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								backend/src/controllers/groups.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | ||||||
|  | import { Request, Response } from 'express'; | ||||||
|  | import { getGroup } from '../services/groups'; | ||||||
|  | 
 | ||||||
|  | // typescript is annoywith with parameter forwarding from class.ts
 | ||||||
|  | interface GroupParams { | ||||||
|  |     classid: string; | ||||||
|  |     assignmentid: string; | ||||||
|  |     groupid: string; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function getGroupHandler( | ||||||
|  |     req: Request<GroupParams>, | ||||||
|  |     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; | ||||||
|  | 
 | ||||||
|  |     if (isNaN(groupId)) { | ||||||
|  |         res.status(400).json({ error: "Group id must be a number" }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const group = await getGroup(classId, assignmentId, groupId, full); | ||||||
|  | 
 | ||||||
|  |     res.json(group); | ||||||
|  | } | ||||||
|  | @ -7,10 +7,13 @@ export class GroupRepository extends DwengoEntityRepository<Group> { | ||||||
|         assignment: Assignment, |         assignment: Assignment, | ||||||
|         groupNumber: number |         groupNumber: number | ||||||
|     ): Promise<Group | null> { |     ): Promise<Group | null> { | ||||||
|         return this.findOne({ |         return this.findOne( | ||||||
|  |         { | ||||||
|             assignment: assignment, |             assignment: assignment, | ||||||
|             groupNumber: groupNumber, |             groupNumber: groupNumber, | ||||||
|         }); |         }, | ||||||
|  |         { populate: ["members"] }, | ||||||
|  |     ); | ||||||
|     } |     } | ||||||
|     public findAllGroupsForAssignment( |     public findAllGroupsForAssignment( | ||||||
|         assignment: Assignment |         assignment: Assignment | ||||||
|  |  | ||||||
|  | @ -1,3 +1,25 @@ | ||||||
|  | import { Group } from "../entities/assignments/group.entity"; | ||||||
|  | import { AssignmentDTO, mapToAssignmentDTO } from "./assignments"; | ||||||
|  | import { mapToStudentDTO, StudentDTO } from "./students"; | ||||||
|  | 
 | ||||||
| export interface GroupDTO { | export interface GroupDTO { | ||||||
|  |     assignment: number | AssignmentDTO, | ||||||
|     groupNumber: number, |     groupNumber: number, | ||||||
|  |     members: string[] | StudentDTO[], | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export function mapToGroupDTO(group: Group): GroupDTO { | ||||||
|  |     return { | ||||||
|  |         assignment: mapToAssignmentDTO(group.assignment, group.assignment.within), | ||||||
|  |         groupNumber: group.groupNumber, | ||||||
|  |         members: group.members.map(mapToStudentDTO), | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function mapToGroupDTOId(group: Group): GroupDTO { | ||||||
|  |     return { | ||||||
|  |         assignment: group.assignment.id, | ||||||
|  |         groupNumber: group.groupNumber, | ||||||
|  |         members: group.members.map(member => member.username), | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | @ -1,9 +1,9 @@ | ||||||
| import express from 'express' | import express from 'express' | ||||||
| import { getAssignmentHandler } from '../controllers/assignments'; | import { getAssignmentHandler } from '../controllers/assignments'; | ||||||
|  | import groupRouter from './group.js'; | ||||||
|  | 
 | ||||||
| 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('/', (req, res) => { | ||||||
|     res.json({ |     res.json({ | ||||||
|  | @ -25,14 +25,6 @@ router.get('/:id/submissions', (req, res) => { | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| router.get('/:id/groups', (req, res) => { |  | ||||||
|     res.json({ |  | ||||||
|         groups: [ |  | ||||||
|             '0' |  | ||||||
|         ], |  | ||||||
|     }); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.get('/:id/questions', (req, res) => { | router.get('/:id/questions', (req, res) => { | ||||||
|     res.json({ |     res.json({ | ||||||
|         questions: [ |         questions: [ | ||||||
|  | @ -41,4 +33,6 @@ router.get('/:id/questions', (req, res) => { | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | router.use('/:assignmentid/groups', groupRouter); | ||||||
|  | 
 | ||||||
| export default router | export default router | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| import express from 'express' | import express from 'express' | ||||||
| const router = express.Router(); | import { getGroupHandler } from '../controllers/groups'; | ||||||
|  | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
| // root endpoint used to search objects
 | // root endpoint used to search objects
 | ||||||
| router.get('/', (req, res) => { | router.get('/', (req, res) => { | ||||||
|  | @ -12,17 +13,7 @@ router.get('/', (req, res) => { | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| // information about a group (members, ... [TODO DOC])
 | // information about a group (members, ... [TODO DOC])
 | ||||||
| router.get('/:id', (req, res) => { | router.get('/:groupid', getGroupHandler); | ||||||
|     res.json({ |  | ||||||
|         id: req.params.id, |  | ||||||
|         assignment: '0', |  | ||||||
|         students: [ '0' ], |  | ||||||
|         submissions: [ '0' ], |  | ||||||
|         // reference to other endpoint
 |  | ||||||
|         // should be less hardcoded
 |  | ||||||
|         questions: `/group/${req.params.id}/question`,  |  | ||||||
|     }); |  | ||||||
| }) |  | ||||||
| 
 | 
 | ||||||
| // 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) => { | ||||||
|  |  | ||||||
							
								
								
									
										36
									
								
								backend/src/services/groups.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								backend/src/services/groups.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,36 @@ | ||||||
|  | import { getAssignmentRepository, getClassRepository, getGroupRepository } from "../data/repositories"; | ||||||
|  | import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from "../interfaces/groups"; | ||||||
|  | 
 | ||||||
|  | export async function getGroup( | ||||||
|  |     classId: string,  | ||||||
|  |     assignmentNumber: number,  | ||||||
|  |     groupNumber: number, | ||||||
|  |     full: boolean, | ||||||
|  | ): Promise<GroupDTO | null> { | ||||||
|  |     const classRepository = getClassRepository(); | ||||||
|  |     const cls = await classRepository.findById(classId); | ||||||
|  | 
 | ||||||
|  |     if (!cls) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const assignmentRepository = getAssignmentRepository(); | ||||||
|  |     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||||
|  | 
 | ||||||
|  |     if (!assignment) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const groupRepository = getGroupRepository(); | ||||||
|  |     const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber); | ||||||
|  | 
 | ||||||
|  |     if (!group) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (full) { | ||||||
|  |         return mapToGroupDTO(group); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return mapToGroupDTOId(group); | ||||||
|  | } | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet