feat(backend): Controller en route voor het aanmaken van leerobjecten aangemaakt.
This commit is contained in:
		
							parent
							
								
									86ba4ea11e
								
							
						
					
					
						commit
						78353d6b65
					
				
					 6 changed files with 81 additions and 6 deletions
				
			
		|  | @ -7,6 +7,7 @@ import { BadRequestException } from '../exceptions/bad-request-exception.js'; | |||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { envVars, getEnvVar } from '../util/envVars.js'; | ||||
| import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content'; | ||||
| import {UploadedFile} from "express-fileupload"; | ||||
| 
 | ||||
| function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifierDTO { | ||||
|     if (!req.params.hruid) { | ||||
|  | @ -72,3 +73,10 @@ export async function getAttachment(req: Request, res: Response): Promise<void> | |||
|     } | ||||
|     res.setHeader('Content-Type', attachment.mimeType).send(attachment.content); | ||||
| } | ||||
| 
 | ||||
| export async function handlePostLearningObject(req: Request, res: Response): Promise<void> { | ||||
|     if (!req.files || !req.files[0]) { | ||||
|         throw new BadRequestException('No file uploaded'); | ||||
|     } | ||||
|     await learningObjectService.storeLearningObject((req.files[0] as UploadedFile).tempFilePath); | ||||
| } | ||||
|  |  | |||
|  | @ -1,8 +1,15 @@ | |||
| import express from 'express'; | ||||
| import { getAllLearningObjects, getAttachment, getLearningObject, getLearningObjectHTML } from '../controllers/learning-objects.js'; | ||||
| import { | ||||
|     getAllLearningObjects, | ||||
|     getAttachment, | ||||
|     getLearningObject, | ||||
|     getLearningObjectHTML, | ||||
|     handlePostLearningObject | ||||
| } from '../controllers/learning-objects.js'; | ||||
| 
 | ||||
| import submissionRoutes from './submissions.js'; | ||||
| import questionRoutes from './questions.js'; | ||||
| import fileUpload from "express-fileupload"; | ||||
| 
 | ||||
| const router = express.Router(); | ||||
| 
 | ||||
|  | @ -18,6 +25,8 @@ const router = express.Router(); | |||
| // Example 2: http://localhost:3000/learningObject?full=true&hruid=un_artificiele_intelligentie
 | ||||
| router.get('/', getAllLearningObjects); | ||||
| 
 | ||||
| router.post('/', fileUpload({useTempFiles: true}), handlePostLearningObject) | ||||
| 
 | ||||
| // Parameter: hruid of learning object
 | ||||
| // Query: language
 | ||||
| // Route to fetch data of one learning object based on its hruid
 | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ import { | |||
| } from '@dwengo-1/common/interfaces/learning-content'; | ||||
| import {getLearningObjectRepository} from "../../data/repositories"; | ||||
| import {processLearningObjectZip} from "./learning-object-zip-processing-service"; | ||||
| import {BadRequestException} from "../../exceptions/bad-request-exception"; | ||||
| 
 | ||||
| function getProvider(id: LearningObjectIdentifierDTO): LearningObjectProvider { | ||||
|     if (id.hruid.startsWith(getEnvVar(envVars.UserContentPrefix))) { | ||||
|  | @ -58,7 +59,7 @@ const learningObjectService = { | |||
|         const learningObject = await processLearningObjectZip(learningObjectPath); | ||||
| 
 | ||||
|         if (!learningObject.hruid.startsWith(getEnvVar(envVars.UserContentPrefix))) { | ||||
|             throw Error("Learning object name must start with the user content prefix!"); | ||||
|             throw new BadRequestException("Learning object name must start with the user content prefix!"); | ||||
|         } | ||||
| 
 | ||||
|         await learningObjectRepository.save(learningObject, {preventOverwrite: true}); | ||||
|  |  | |||
|  | @ -1,8 +1,9 @@ | |||
| import unzipper from 'unzipper'; | ||||
| import mime from 'mime-types'; | ||||
| import {LearningObjectMetadata} from "@dwengo-1/common/dist/interfaces/learning-content"; | ||||
| import {LearningObject} from "../../entities/content/learning-object.entity"; | ||||
| import {getAttachmentRepository, getLearningObjectRepository} from "../../data/repositories"; | ||||
| import {BadRequestException} from "../../exceptions/bad-request-exception"; | ||||
| import {LearningObjectMetadata} from "@dwengo-1/common/dist/interfaces/learning-content"; | ||||
| 
 | ||||
| /** | ||||
|  * Process an uploaded zip file and construct a LearningObject from its contents. | ||||
|  | @ -20,7 +21,7 @@ export async function processLearningObjectZip(filePath: string): Promise<Learni | |||
| 
 | ||||
|     for (const file of zip.files) { | ||||
|         if (file.type === "Directory") { | ||||
|             throw Error("The learning object zip file should not contain directories."); | ||||
|             throw new BadRequestException("The learning object zip file should not contain directories."); | ||||
|         } else if (file.path === "metadata.json") { | ||||
|             metadata = await processMetadataJson(file); | ||||
|         } else if (file.path.startsWith("index.")) { | ||||
|  | @ -34,10 +35,10 @@ export async function processLearningObjectZip(filePath: string): Promise<Learni | |||
|     } | ||||
| 
 | ||||
|     if (!metadata) { | ||||
|         throw Error("Missing metadata.json file"); | ||||
|         throw new BadRequestException("Missing metadata.json file"); | ||||
|     } | ||||
|     if (!content) { | ||||
|         throw Error("Missing index file"); | ||||
|         throw new BadRequestException("Missing index file"); | ||||
|     } | ||||
| 
 | ||||
|     const learningObject = learningObjectRepo.create(metadata); | ||||
|  |  | |||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger