feat: PUT request geabstraheerd naar service-helper.ts

This commit is contained in:
Adriaan Jacquet 2025-04-06 22:19:15 +02:00
parent 800d52257c
commit 541e8ab2d5
4 changed files with 57 additions and 17 deletions

View file

@ -43,6 +43,20 @@ export async function getAssignmentHandler(req: Request, res: Response): Promise
res.json({ assignment });
}
export async function putAssignmentHandler(req: Request, res: Response): Promise<void> {
const id = Number(req.params.id);
const classid = req.params.classid;
requireFields({ id, classid });
if (isNaN(id)) {
throw new BadRequestException("Assignment id should be a number")
}
const assignment = await putAssignment(classid, id);
res.json({ assignment });
}
export async function deleteAssignmentHandler(req: Request, res: Response): Promise<void> {
const id = Number(req.params.id);
const classid = req.params.classid;

View file

@ -9,6 +9,9 @@ import { fetchClass } from './classes.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
import { getLogger } from '../logging/initalize.js';
import { EntityData, EntityDTO, FromEntityType } from '@mikro-orm/core';
import { DwengoEntityRepository } from '../data/dwengo-entity-repository.js';
import { putObject } from './service-helper.js';
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
const classRepository = getClassRepository();
@ -58,6 +61,14 @@ export async function getAssignment(classid: string, id: number): Promise<Assign
return mapToAssignmentDTO(assignment);
}
export async function putAssignment(classid: string, id: number, assignmentData: Partial<EntityDTO<Assignment>>): Promise<AssignmentDTO> {
const assignment = await fetchAssignment(classid, id);
await putObject<Assignment, AssignmentDTO>(assignment, assignmentData, getAssignmentRepository());
return mapToAssignmentDTO(assignment);
}
export async function deleteAssignment(classid: string, id: number): Promise<AssignmentDTO> {
const assignment = await fetchAssignment(classid, id);
const cls = await fetchClass(classid);

View file

@ -13,6 +13,7 @@ import {fetchStudent, getStudent} from "./students";
import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher";
import {mapToTeacherDTO} from "../interfaces/teacher";
import { EntityDTO } from '@mikro-orm/core';
import { putObject } from './service-helper.js';
export async function fetchClass(classid: string): Promise<Class> {
const classRepository = getClassRepository();
@ -58,12 +59,10 @@ export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
return mapToClassDTO(newClass);
}
export async function putClass(classId: string, classData: Partial<ClassDTO>): Promise<ClassDTO> {
export async function putClass(classId: string, classData: Partial<EntityDTO<Class>>): Promise<ClassDTO> {
const cls = await fetchClass(classId);
const classRepository = getClassRepository();
classRepository.assign(cls, classData as Partial<EntityDTO<Class>>);
await classRepository.getEntityManager().flush();
await putObject<Class, ClassDTO>(cls, classData, getClassRepository());
return mapToClassDTO(cls);
}
@ -116,9 +115,8 @@ export async function getClassTeacherInvitations(classId: string, full: boolean)
export async function deleteClassStudent(classId: string, username: string): Promise<ClassDTO> {
const cls = await fetchClass(classId);
const classRepository = getClassRepository();
classRepository.assign(cls, { students: cls.students.filter((student) => student.username !== username) });
await classRepository.getEntityManager().flush();
const newStudents = { students: cls.students.filter((student) => student.username !== username) };
await putObject<Class, ClassDTO>(cls, newStudents, getClassRepository());
return mapToClassDTO(cls);
}
@ -126,9 +124,8 @@ export async function deleteClassStudent(classId: string, username: string): Pro
export async function deleteClassTeacher(classId: string, username: string): Promise<ClassDTO> {
const cls = await fetchClass(classId);
const classRepository = getClassRepository();
classRepository.assign(cls, { teachers: cls.teachers.filter((teacher) => teacher.username !== username) });
await classRepository.getEntityManager().flush();
const newTeachers = { teachers: cls.teachers.filter((teacher) => teacher.username !== username) };
await putObject<Class, ClassDTO>(cls, newTeachers, getClassRepository());
return mapToClassDTO(cls);
}
@ -137,10 +134,9 @@ export async function addClassStudent(classId: string, username: string): Promis
const cls = await fetchClass(classId);
const newStudent = await fetchStudent(username);
const classRepository = getClassRepository();
classRepository.assign(cls, { students: [...cls.students, newStudent] });
await classRepository.getEntityManager().flush();
const newStudents = { students: [...cls.students, newStudent] }
await putObject<Class, ClassDTO>(cls, newStudents, getClassRepository());
return mapToClassDTO(cls);
}
@ -148,9 +144,8 @@ export async function addClassTeacher(classId: string, username: string): Promis
const cls = await fetchClass(classId);
const newTeacher = await fetchTeacher(username);
const classRepository = getClassRepository();
classRepository.assign(cls, { teachers: [...cls.teachers, newTeacher] });
await classRepository.getEntityManager().flush();
const newTeachers = { teachers: [...cls.teachers, newTeacher] };
await putObject<Class, ClassDTO>(cls, newTeachers, getClassRepository());
return mapToClassDTO(cls);
}

View file

@ -0,0 +1,20 @@
import { EntityDTO, FromEntityType } from "@mikro-orm/core";
import { DwengoEntityRepository } from "../data/dwengo-entity-repository";
/**
* Utility function to perform an PUT on an object.
*
* @param object The object that needs to be changed
* @param data The datafields and their values that will be updated
* @param repo The repository on which this action needs to be performed
*
* @returns Nothing.
*/
export async function putObject<T extends Object, DTO>(
object: T,
data: Partial<EntityDTO<FromEntityType<T>>>,
repo: DwengoEntityRepository<T>
): Promise<void> {
repo.assign(object, data);
await repo.getEntityManager().flush();
}