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 { 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 // Typescript is annoy with with parameter forwarding from class.ts
interface AssignmentParams { 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( export async function getAssignmentHandler(
req: Request<AssignmentParams>, req: Request<AssignmentParams>,
res: Response res: Response

View file

@ -25,8 +25,8 @@ export class Assignment {
}) })
within!: Class; within!: Class;
@PrimaryKey({ type: 'number' }) @PrimaryKey({ type: 'number', autoincrement: true })
id!: number; id?: number;
@Property({ type: 'string' }) @Property({ type: 'string' })
title!: string; title!: string;

View file

@ -1,4 +1,7 @@
import { FALLBACK_LANG } from '../config.js';
import { Assignment } from '../entities/assignments/assignment.entity.js'; import { Assignment } from '../entities/assignments/assignment.entity.js';
import { Class } from '../entities/classes/class.entity.js';
import { languageMap } from '../entities/content/language.js';
import { GroupDTO, mapToGroupDTO } from './group.js'; import { GroupDTO, mapToGroupDTO } from './group.js';
export interface AssignmentDTO { export interface AssignmentDTO {
@ -13,7 +16,7 @@ export interface AssignmentDTO {
export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
return { return {
id: assignment.id, id: assignment.id!,
class: assignment.within.classId, class: assignment.within.classId,
title: assignment.title, title: assignment.title,
description: assignment.description, description: assignment.description,
@ -25,7 +28,7 @@ export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO {
return { return {
id: assignment.id, id: assignment.id!,
class: assignment.within.classId, class: assignment.within.classId,
title: assignment.title, title: assignment.title,
description: assignment.description, description: assignment.description,
@ -34,3 +37,16 @@ export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO {
// Groups: assignment.groups.map(mapToGroupDTO), // Groups: assignment.groups.map(mapToGroupDTO),
}; };
} }
export function mapToAssignment(assignmentData: AssignmentDTO, cls: Class): Assignment {
const assignment = new Assignment();
assignment.title = assignmentData.title;
assignment.description = assignmentData.description;
assignment.learningPathHruid = assignmentData.learningPath;
assignment.learningPathLanguage = languageMap[assignmentData.language] || FALLBACK_LANG;
assignment.within = cls;
console.log(assignment);
return assignment;
}

View file

@ -1,5 +1,6 @@
import express from 'express'; import express from 'express';
import { import {
createAssignmentHandler,
getAllAssignmentsHandler, getAllAssignmentsHandler,
getAssignmentHandler, getAssignmentHandler,
getAssignmentsSubmissionsHandler, getAssignmentsSubmissionsHandler,
@ -11,6 +12,8 @@ const router = express.Router({ mergeParams: true });
// Root endpoint used to search objects // Root endpoint used to search objects
router.get('/', getAllAssignmentsHandler); router.get('/', getAllAssignmentsHandler);
router.post('/', createAssignmentHandler);
// Information about an assignment with id 'id' // Information about an assignment with id 'id'
router.get('/:id', getAssignmentHandler); router.get('/:id', getAssignmentHandler);

View file

@ -4,8 +4,10 @@ import {
getGroupRepository, getGroupRepository,
getSubmissionRepository, getSubmissionRepository,
} from '../data/repositories.js'; } from '../data/repositories.js';
import { Assignment } from '../entities/assignments/assignment.entity.js';
import { import {
AssignmentDTO, AssignmentDTO,
mapToAssignment,
mapToAssignmentDTO, mapToAssignmentDTO,
mapToAssignmentDTOId, mapToAssignmentDTOId,
} from '../interfaces/assignment.js'; } from '../interfaces/assignment.js';
@ -33,6 +35,31 @@ export async function getAllAssignments(
return assignments.map(mapToAssignmentDTOId); return assignments.map(mapToAssignmentDTOId);
} }
export async function createAssignment(
classid: string,
assignmentData: AssignmentDTO,
): Promise<Assignment | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classid);
if (!cls) {
return null;
}
const assignment = mapToAssignment(assignmentData, cls);
const assignmentRepository = getAssignmentRepository();
try {
const newAssignment = assignmentRepository.create(assignment);
await assignmentRepository.save(newAssignment);
return newAssignment;
} catch(e) {
return null;
}
}
export async function getAssignment( export async function getAssignment(
classid: string, classid: string,
id: number id: number