feat: POST method voor assignment geimplementeerd

This commit is contained in:
Adriaan Jacquet 2025-03-13 14:39:12 +01:00
parent 3e2c73320b
commit a1aaf342d7
5 changed files with 81 additions and 5 deletions

View file

@ -1,5 +1,7 @@
import { Request, Response } from 'express';
import { getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js';
import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js';
import { AssignmentDTO, mapToAssignment, mapToAssignmentDTO } from '../interfaces/assignment.js';
import { getAssignmentRepository, getClassRepository } from '../data/repositories.js';
// Typescript is annoy with with parameter forwarding from class.ts
interface AssignmentParams {
@ -21,6 +23,34 @@ export async function getAllAssignmentsHandler(
});
}
export async function createAssignmentHandler(
req: Request<AssignmentParams>,
res: Response,
): Promise<void> {
const classid = req.params.classid;
const assignmentData = req.body as AssignmentDTO;
if (!assignmentData.description
|| !assignmentData.language
|| !assignmentData.learningPath
|| !assignmentData.title
) {
res.status(400).json({
error: 'Missing one or more required fields: title, description, learningPath, title',
});
return;
}
const assignment = createAssignment(classid, assignmentData);
if (!assignment) {
res.status(500).json({ error: "Could not create assignment "});
return;
}
res.status(201).json({ assignment: assignment });
}
export async function getAssignmentHandler(
req: Request<AssignmentParams>,
res: Response