fix(backend): Insert weggewerkt

In de plaats optionele check aan DwengoEntityRepository.save(...) toegevoegd die eist dat de entity nog niet bestaat.
This commit is contained in:
Gerald Schmittinger 2025-03-30 14:19:57 +02:00
parent 148de14b78
commit e8add72de4
5 changed files with 1281 additions and 515 deletions

View file

@ -1,8 +1,12 @@
import { EntityRepository, FilterQuery } from '@mikro-orm/core';
import {EntityRepository, FilterQuery} from '@mikro-orm/core';
import {EntityAlreadyExistsException} from "../exceptions/entity-already-exists-exception";
export abstract class DwengoEntityRepository<T extends object> extends EntityRepository<T> {
public async save(entity: T) {
await this.getEntityManager().insert(entity);
public async save(entity: T, options?: {preventOverwrite?: Boolean}): Promise<void> {
if (options?.preventOverwrite && await this.findOne(entity)) {
throw new EntityAlreadyExistsException(`A ${this.getEntityName()} with this identifier already exists.`);
}
await this.getEntityManager().persistAndFlush(entity);
}
public async deleteWhere(query: FilterQuery<T>) {
const toDelete = await this.findOne(query);

View file

@ -0,0 +1,7 @@
import {ConflictException} from "./conflict-exception";
export class EntityAlreadyExistsException extends ConflictException {
constructor(message: string) {
super(message);
}
}

View file

@ -5,9 +5,6 @@ import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js
import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js';
import { getAllAssignments } from './assignments.js';
import {UniqueConstraintViolationException} from "@mikro-orm/core";
import {ConflictException} from "../exceptions/conflict-exception";
export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> {
const studentRepository = getStudentRepository();
@ -29,16 +26,9 @@ export async function getStudent(username: string): Promise<StudentDTO | null> {
export async function createStudent(userData: StudentDTO): Promise<StudentDTO | null> {
const studentRepository = getStudentRepository();
try {
const newStudent = mapToStudent(userData);
await studentRepository.save(newStudent);
return mapToStudentDTO(newStudent);
} catch (e: unknown) {
if (e instanceof UniqueConstraintViolationException) {
throw new ConflictException(`There is already a user with username '${userData.username}'.`);
}
throw e;
}
const newStudent = mapToStudent(userData);
await studentRepository.save(newStudent, {preventOverwrite: true});
return mapToStudentDTO(newStudent);
}
export async function deleteStudent(username: string): Promise<StudentDTO | null> {

View file

@ -9,9 +9,6 @@ import { getClassStudents } from './classes.js';
import { StudentDTO } from '../interfaces/student.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
import {UniqueConstraintViolationException} from "@mikro-orm/core";
import {ConflictException} from "../exceptions/conflict-exception";
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository = getTeacherRepository();
@ -33,17 +30,10 @@ export async function getTeacher(username: string): Promise<TeacherDTO | null> {
export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO | null> {
const teacherRepository = getTeacherRepository();
try {
const newTeacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher);
const newTeacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher);
return mapToTeacherDTO(newTeacher);
} catch (e) {
if (e instanceof UniqueConstraintViolationException) {
throw new ConflictException(`There is already a user with username '${userData.username}'.`);
}
throw e;
}
return mapToTeacherDTO(newTeacher);
}
export async function deleteTeacher(username: string): Promise<TeacherDTO | null> {

1747
package-lock.json generated

File diff suppressed because it is too large Load diff