feat: gestart met implementeren van assignment service en controller laag
This commit is contained in:
		
							parent
							
								
									241fe0103f
								
							
						
					
					
						commit
						cfd0cce2df
					
				
					 8 changed files with 92 additions and 25 deletions
				
			
		|  | @ -8,7 +8,6 @@ import learningObjectRoutes from './routes/learningObjects.js'; | |||
| 
 | ||||
| import studentRouter from './routes/student.js'; | ||||
| import groupRouter from './routes/group.js'; | ||||
| import assignmentRouter from './routes/assignment.js'; | ||||
| import submissionRouter from './routes/submission.js'; | ||||
| import classRouter from './routes/class.js'; | ||||
| import questionRouter from './routes/question.js'; | ||||
|  | @ -27,7 +26,6 @@ app.get('/', (_, res: Response) => { | |||
| 
 | ||||
| app.use('/student', studentRouter); | ||||
| app.use('/group', groupRouter); | ||||
| app.use('/assignment', assignmentRouter); | ||||
| app.use('/submission', submissionRouter); | ||||
| app.use('/class', classRouter); | ||||
| app.use('/question', questionRouter); | ||||
|  |  | |||
							
								
								
									
										30
									
								
								backend/src/controllers/assignments.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								backend/src/controllers/assignments.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | |||
| import { Request, Response } from 'express' | ||||
| import { getAssignment } from '../services/assignments'; | ||||
| 
 | ||||
| // typescript is annoywith with parameter forwarding from class.ts
 | ||||
| interface AssignmentParams { | ||||
|     classid: string; | ||||
|     id: string; | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentHandler( | ||||
|     req: Request<AssignmentParams>, | ||||
|     res: Response, | ||||
| ): Promise<void> { | ||||
|     const id = +req.params.id; | ||||
|     const classid = req.params.classid; | ||||
| 
 | ||||
|     if (isNaN(id)) { | ||||
|         res.status(400).json({ error: "Assignment id must be a number" }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const assignment = await getAssignment(classid, id); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         res.status(404).json({ error: "Assignment not found" }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json(assignment); | ||||
| } | ||||
							
								
								
									
										25
									
								
								backend/src/interfaces/assignments.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								backend/src/interfaces/assignments.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| import { Assignment } from "../entities/assignments/assignment.entity"; | ||||
| import { Class } from "../entities/classes/class.entity"; | ||||
| import { GroupDTO } from "./groups"; | ||||
| 
 | ||||
| export interface AssignmentDTO { | ||||
|     id: number, | ||||
|     class: string, // id of class 'within'
 | ||||
|     title: string, | ||||
|     description: string, | ||||
|     learningPath: string, | ||||
|     language: string, | ||||
|     groups?: GroupDTO[], // TODO
 | ||||
| } | ||||
| 
 | ||||
| export function mapToAssignmentDTO(assignment: Assignment, cls: Class): AssignmentDTO { | ||||
|     return { | ||||
|         id: assignment.id, | ||||
|         class: cls.classId, | ||||
|         title: assignment.title, | ||||
|         description: assignment.description, | ||||
|         learningPath: assignment.learningPathHruid, | ||||
|         language: assignment.learningPathLanguage, | ||||
|         //groups: assignment.groups.map(mapToGroupDTO),
 | ||||
|     }; | ||||
| } | ||||
							
								
								
									
										3
									
								
								backend/src/interfaces/groups.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								backend/src/interfaces/groups.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| export interface GroupDTO { | ||||
|     groupNumber: number, | ||||
| } | ||||
							
								
								
									
										5
									
								
								backend/src/interfaces/list.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								backend/src/interfaces/list.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| // TODO: implement something like this but with named endpoints
 | ||||
| export interface List<T> { | ||||
|     items: T[], | ||||
|     endpoints?: string[], | ||||
| }; | ||||
|  | @ -1,5 +1,8 @@ | |||
| import express from 'express' | ||||
| const router = express.Router(); | ||||
| import { getAssignmentHandler } from '../controllers/assignments'; | ||||
| const router = express.Router({ mergeParams: true }); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| // root endpoint used to search objects
 | ||||
| router.get('/', (req, res) => { | ||||
|  | @ -12,20 +15,7 @@ router.get('/', (req, res) => { | |||
| }); | ||||
| 
 | ||||
| // information about an assignment with id 'id'
 | ||||
| router.get('/:id', (req, res) => { | ||||
|     res.json({ | ||||
|         id: req.params.id, | ||||
|         title: 'Dit is een test assignment', | ||||
|         description: 'Een korte beschrijving', | ||||
|         groups: [ '0' ], | ||||
|         learningPath: '0', | ||||
|         class: '0', | ||||
|         links: { | ||||
|             self: `${req.baseUrl}/${req.params.id}`, | ||||
|             submissions: `${req.baseUrl}/${req.params.id}`, | ||||
|         }, | ||||
|     }); | ||||
| }) | ||||
| router.get('/:id', getAssignmentHandler); | ||||
| 
 | ||||
| router.get('/:id/submissions', (req, res) => { | ||||
|     res.json({ | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| import express from 'express' | ||||
| import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes'; | ||||
| import assignmentRouter from './assignment.js'; | ||||
| 
 | ||||
| const router = express.Router(); | ||||
| 
 | ||||
| // root endpoint used to search objects
 | ||||
|  | @ -16,14 +18,8 @@ router.get('/:id/invitations', (req, res) => { | |||
|     }); | ||||
| }) | ||||
| 
 | ||||
| router.get('/:id/assignments', (req, res) => { | ||||
|     res.json({ | ||||
|         assignments: [  | ||||
|             '0' | ||||
|         ], | ||||
|     }); | ||||
| }) | ||||
| 
 | ||||
| router.get('/:id/students', getClassStudentsHandler); | ||||
| 
 | ||||
| router.use('/:classid/assignments', assignmentRouter); | ||||
| 
 | ||||
| export default router | ||||
							
								
								
									
										20
									
								
								backend/src/services/assignments.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								backend/src/services/assignments.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| import { getAssignmentRepository, getClassRepository } from "../data/repositories"; | ||||
| import { AssignmentDTO, mapToAssignmentDTO } from "../interfaces/assignments"; | ||||
| 
 | ||||
| export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
|      | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, id); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     return mapToAssignmentDTO(assignment, cls); | ||||
| } | ||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet