feat: get all submissions route toegevoegd
This commit is contained in:
		
							parent
							
								
									7ae2f1de0c
								
							
						
					
					
						commit
						6290d3dd9b
					
				
					 4 changed files with 59 additions and 41 deletions
				
			
		|  | @ -1,33 +1,41 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { createSubmission, deleteSubmission, getSubmission } from '../services/submissions.js'; | import { createSubmission, deleteSubmission, getAllSubmissions, getSubmission } from '../services/submissions.js'; | ||||||
| import { Language, languageMap } from '../entities/content/language.js'; | import { Language, languageMap } from '../entities/content/language.js'; | ||||||
| import { SubmissionDTO } from '../interfaces/submission'; | import { SubmissionDTO } from '../interfaces/submission'; | ||||||
|  | import { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||||
|  | import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
|  | import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||||
| 
 | 
 | ||||||
| interface SubmissionParams { |  | ||||||
|     hruid: string; |  | ||||||
|     id: number; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| export async function getSubmissionHandler(req: Request<SubmissionParams>, res: Response): Promise<void> { | 
 | ||||||
|     const lohruid = req.params.hruid; | export async function getSubmissionHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const submissionNumber = +req.params.id; |     const submissionNumber = +req.params.id; | ||||||
|      |      | ||||||
|     if (isNaN(submissionNumber)) { |     if (isNaN(submissionNumber)) { | ||||||
|         res.status(400).json({ error: 'Submission number is not a number' }); |         throw new BadRequestException('Submission number must be a number'); | ||||||
|         return; |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  |     const lohruid = req.params.hruid; | ||||||
|     const lang = languageMap[req.query.language as string] || Language.Dutch; |     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||||
|     const version = (req.query.version || 1) as number; |     const version = (req.query.version || 1) as number; | ||||||
| 
 | 
 | ||||||
|     const submission = await getSubmission(lohruid, lang, version, submissionNumber); |     const loId = new LearningObjectIdentifier(lohruid, lang, version); | ||||||
| 
 | 
 | ||||||
|     if (!submission) { |     const submission = await getSubmission(loId, submissionNumber); | ||||||
|         res.status(404).json({ error: 'Submission not found' }); |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     res.json(submission); |     res.json({ submission }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function getAllSubmissionsHandler(req: Request, res: Response): Promise<void> { | ||||||
|  |     const lohruid = req.params.hruid; | ||||||
|  |     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||||
|  |     const version = (req.query.version || 1) as number; | ||||||
|  | 
 | ||||||
|  |     const loId = new LearningObjectIdentifier(lohruid, lang, version); | ||||||
|  | 
 | ||||||
|  |     const submissions = await getAllSubmissions(loId); | ||||||
|  | 
 | ||||||
|  |     res.json({ submissions }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createSubmissionHandler(req: Request, res: Response) { | export async function createSubmissionHandler(req: Request, res: Response) { | ||||||
|  | @ -40,22 +48,23 @@ export async function createSubmissionHandler(req: Request, res: Response) { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     res.json(submission); |     res.json({ submission }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteSubmissionHandler(req: Request, res: Response) { | export async function deleteSubmissionHandler(req: Request, res: Response) { | ||||||
|     const hruid = req.params.hruid; |  | ||||||
|     const submissionNumber = +req.params.id; |     const submissionNumber = +req.params.id; | ||||||
|      |      | ||||||
|  |     const hruid = req.params.hruid; | ||||||
|     const lang = languageMap[req.query.language as string] || Language.Dutch; |     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||||
|     const version = (req.query.version || 1) as number; |     const version = (req.query.version || 1) as number; | ||||||
| 
 | 
 | ||||||
|     const submission = await deleteSubmission(hruid, lang, version, submissionNumber); |     const loId = new LearningObjectIdentifier(hruid, lang, version); | ||||||
|  | 
 | ||||||
|  |     const submission = await deleteSubmission(loId, submissionNumber); | ||||||
| 
 | 
 | ||||||
|     if (!submission) { |     if (!submission) { | ||||||
|         res.status(404).json({ error: 'Submission not found' }); |         throw new NotFoundException('Could not delete submission'); | ||||||
|         return; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     res.json(submission); |     res.json({ submission }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -14,6 +14,14 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> { | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public findByLearningObject(loId: LearningObjectIdentifier): Promise<Submission[]> { | ||||||
|  |         return this.find({ | ||||||
|  |             learningObjectHruid: loId.hruid, | ||||||
|  |             learningObjectLanguage: loId.language, | ||||||
|  |             learningObjectVersion: loId.version, | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public findMostRecentSubmissionForStudent(loId: LearningObjectIdentifier, submitter: Student): Promise<Submission | null> { |     public findMostRecentSubmissionForStudent(loId: LearningObjectIdentifier, submitter: Student): Promise<Submission | null> { | ||||||
|         return this.findOne( |         return this.findOne( | ||||||
|             { |             { | ||||||
|  |  | ||||||
|  | @ -1,13 +1,9 @@ | ||||||
| import express from 'express'; | import express from 'express'; | ||||||
| import { createSubmissionHandler, deleteSubmissionHandler, getSubmissionHandler } from '../controllers/submissions.js'; | import { createSubmissionHandler, deleteSubmissionHandler, getAllSubmissionsHandler, getSubmissionHandler } from '../controllers/submissions.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('/', getAllSubmissionsHandler); | ||||||
|     res.json({ |  | ||||||
|         submissions: ['0', '1'], |  | ||||||
|     }); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| router.post('/:id', createSubmissionHandler); | router.post('/:id', createSubmissionHandler); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,26 +1,32 @@ | ||||||
| import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js'; | import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js'; | ||||||
| import { Language } from '../entities/content/language.js'; | import { Language } from '../entities/content/language.js'; | ||||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||||
|  | import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
| import { mapToSubmission, mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; | import { mapToSubmission, mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; | ||||||
| 
 | 
 | ||||||
| export async function getSubmission( | export async function getSubmission( | ||||||
|     learningObjectHruid: string, |     loId: LearningObjectIdentifier, | ||||||
|     language: Language, |  | ||||||
|     version: number, |  | ||||||
|     submissionNumber: number |     submissionNumber: number | ||||||
| ): Promise<SubmissionDTO | null> { | ): Promise<SubmissionDTO> { | ||||||
|     const loId = new LearningObjectIdentifier(learningObjectHruid, language, version); |  | ||||||
| 
 |  | ||||||
|     const submissionRepository = getSubmissionRepository(); |     const submissionRepository = getSubmissionRepository(); | ||||||
|     const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); |     const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); | ||||||
| 
 | 
 | ||||||
|     if (!submission) { |     if (!submission) { | ||||||
|         return null; |         throw new NotFoundException('Could not find submission'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return mapToSubmissionDTO(submission); |     return mapToSubmissionDTO(submission); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export async function getAllSubmissions( | ||||||
|  |     loId: LearningObjectIdentifier, | ||||||
|  | ): Promise<SubmissionDTO[]> { | ||||||
|  |     const submissionRepository = getSubmissionRepository(); | ||||||
|  |     const submissions = await submissionRepository.findByLearningObject(loId); | ||||||
|  | 
 | ||||||
|  |     return submissions.map(mapToSubmissionDTO); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function createSubmission(submissionDTO: SubmissionDTO) { | export async function createSubmission(submissionDTO: SubmissionDTO) { | ||||||
|     const submissionRepository = getSubmissionRepository(); |     const submissionRepository = getSubmissionRepository(); | ||||||
|     const submission = mapToSubmission(submissionDTO); |     const submission = mapToSubmission(submissionDTO); | ||||||
|  | @ -35,16 +41,15 @@ export async function createSubmission(submissionDTO: SubmissionDTO) { | ||||||
|     return mapToSubmissionDTO(submission); |     return mapToSubmissionDTO(submission); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteSubmission(learningObjectHruid: string, language: Language, version: number, submissionNumber: number) { | export async function deleteSubmission(loId: LearningObjectIdentifier, submissionNumber: number) { | ||||||
|     const submissionRepository = getSubmissionRepository(); |     const submissionRepository = getSubmissionRepository(); | ||||||
| 
 | 
 | ||||||
|     const submission = getSubmission(learningObjectHruid, language, version, submissionNumber); |     const submission = getSubmission(loId, submissionNumber); | ||||||
| 
 | 
 | ||||||
|     if (!submission) { |     if (!submission) { | ||||||
|         return null; |         throw new NotFoundException('Could not delete submission because it does not exist'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const loId = new LearningObjectIdentifier(learningObjectHruid, language, version); |  | ||||||
|     await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); |     await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); | ||||||
| 
 | 
 | ||||||
|     return submission; |     return submission; | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet