feat(backend): opvragen van leerobjecten van een leerkracht

This commit is contained in:
Gerald Schmittinger 2025-05-11 15:46:53 +02:00
parent 78353d6b65
commit 6600441b08
11 changed files with 152 additions and 38 deletions

View file

@ -8,6 +8,7 @@ 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";
import {AuthenticatedRequest} from "../middleware/auth/authenticated-request";
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifierDTO {
if (!req.params.hruid) {
@ -31,17 +32,24 @@ function getLearningPathIdentifierFromRequest(req: Request): LearningPathIdentif
}
export async function getAllLearningObjects(req: Request, res: Response): Promise<void> {
const learningPathId = getLearningPathIdentifierFromRequest(req);
const full = req.query.full;
if (req.query.admin) { // If the admin query parameter is present, the user wants to have all learning objects with this admin.
const learningObjects =
await learningObjectService.getLearningObjectsAdministratedBy(req.query.admin as string);
let learningObjects: FilteredLearningObject[] | string[];
if (full) {
learningObjects = await learningObjectService.getLearningObjectsFromPath(learningPathId);
} else {
learningObjects = await learningObjectService.getLearningObjectIdsFromPath(learningPathId);
res.json(learningObjects);
} else { // Else he/she wants all learning objects on the path specified by the request parameters.
const learningPathId = getLearningPathIdentifierFromRequest(req);
const full = req.query.full;
let learningObjects: FilteredLearningObject[] | string[];
if (full) {
learningObjects = await learningObjectService.getLearningObjectsFromPath(learningPathId);
} else {
learningObjects = await learningObjectService.getLearningObjectIdsFromPath(learningPathId);
}
res.json({ learningObjects: learningObjects });
}
res.json({ learningObjects: learningObjects });
}
export async function getLearningObject(req: Request, res: Response): Promise<void> {
@ -74,9 +82,13 @@ 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]) {
export async function handlePostLearningObject(req: AuthenticatedRequest, res: Response): Promise<void> {
if (!req.files || !req.files.learningObject) {
throw new BadRequestException('No file uploaded');
}
await learningObjectService.storeLearningObject((req.files[0] as UploadedFile).tempFilePath);
const learningObject = await learningObjectService.storeLearningObject(
(req.files.learningObject as UploadedFile).tempFilePath,
[req.auth!.username]
);
res.json(learningObject);
}