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:
		
							parent
							
								
									148de14b78
								
							
						
					
					
						commit
						e8add72de4
					
				
					 5 changed files with 1281 additions and 515 deletions
				
			
		|  | @ -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> { | export abstract class DwengoEntityRepository<T extends object> extends EntityRepository<T> { | ||||||
|     public async save(entity: T) { |     public async save(entity: T, options?: {preventOverwrite?: Boolean}): Promise<void> { | ||||||
|         await this.getEntityManager().insert(entity); |         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>) { |     public async deleteWhere(query: FilterQuery<T>) { | ||||||
|         const toDelete = await this.findOne(query); |         const toDelete = await this.findOne(query); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,7 @@ | ||||||
|  | import {ConflictException} from "./conflict-exception"; | ||||||
|  | 
 | ||||||
|  | export class EntityAlreadyExistsException extends ConflictException { | ||||||
|  |     constructor(message: string) { | ||||||
|  |         super(message); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -5,9 +5,6 @@ import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js | ||||||
| import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; | import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; | ||||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; | import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; | ||||||
| import { getAllAssignments } from './assignments.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[]> { | export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> { | ||||||
|     const studentRepository = getStudentRepository(); |     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> { | export async function createStudent(userData: StudentDTO): Promise<StudentDTO | null> { | ||||||
|     const studentRepository = getStudentRepository(); |     const studentRepository = getStudentRepository(); | ||||||
| 
 | 
 | ||||||
|     try { |     const newStudent = mapToStudent(userData); | ||||||
|         const newStudent = mapToStudent(userData); |     await studentRepository.save(newStudent, {preventOverwrite: true}); | ||||||
|         await studentRepository.save(newStudent); |     return mapToStudentDTO(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; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteStudent(username: string): Promise<StudentDTO | null> { | export async function deleteStudent(username: string): Promise<StudentDTO | null> { | ||||||
|  |  | ||||||
|  | @ -9,9 +9,6 @@ import { getClassStudents } from './classes.js'; | ||||||
| import { StudentDTO } from '../interfaces/student.js'; | import { StudentDTO } from '../interfaces/student.js'; | ||||||
| import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js'; | import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js'; | ||||||
| import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.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[]> { | export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { | ||||||
|     const teacherRepository = getTeacherRepository(); |     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> { | export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO | null> { | ||||||
|     const teacherRepository = getTeacherRepository(); |     const teacherRepository = getTeacherRepository(); | ||||||
| 
 | 
 | ||||||
|     try { |     const newTeacher = teacherRepository.create(mapToTeacher(userData)); | ||||||
|         const newTeacher = teacherRepository.create(mapToTeacher(userData)); |     await teacherRepository.save(newTeacher); | ||||||
|         await teacherRepository.save(newTeacher); |  | ||||||
| 
 | 
 | ||||||
|         return mapToTeacherDTO(newTeacher); |     return mapToTeacherDTO(newTeacher); | ||||||
|     } catch (e) { |  | ||||||
|         if (e instanceof UniqueConstraintViolationException) { |  | ||||||
|             throw new ConflictException(`There is already a user with username '${userData.username}'.`); |  | ||||||
|         } |  | ||||||
|         throw e; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteTeacher(username: string): Promise<TeacherDTO | null> { | export async function deleteTeacher(username: string): Promise<TeacherDTO | null> { | ||||||
|  |  | ||||||
							
								
								
									
										1747
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										1747
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger