fix(backend): Probleem opgelost dat meerdere studenten en leerkrachten met dezelfde PK opgeslagen konden worden.
create() en insert() i.p.v. entity constructoren en persist() gebruikt.
This commit is contained in:
		
							parent
							
								
									295fe23e99
								
							
						
					
					
						commit
						bc94b25a6a
					
				
					 8 changed files with 18 additions and 36 deletions
				
			
		|  | @ -2,9 +2,7 @@ import { EntityRepository, FilterQuery } from '@mikro-orm/core'; | ||||||
| 
 | 
 | ||||||
| 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) { | ||||||
|         const em = this.getEntityManager(); |         await this.getEntityManager().insert(entity); | ||||||
|         em.persist(entity); |  | ||||||
|         await em.flush(); |  | ||||||
|     } |     } | ||||||
|     public async deleteWhere(query: FilterQuery<T>) { |     public async deleteWhere(query: FilterQuery<T>) { | ||||||
|         const toDelete = await this.findOne(query); |         const toDelete = await this.findOne(query); | ||||||
|  |  | ||||||
|  | @ -13,12 +13,4 @@ export class Student extends User { | ||||||
| 
 | 
 | ||||||
|     @ManyToMany(() => Group) |     @ManyToMany(() => Group) | ||||||
|     groups!: Collection<Group>; |     groups!: Collection<Group>; | ||||||
| 
 |  | ||||||
|     constructor( |  | ||||||
|         public username: string, |  | ||||||
|         public firstName: string, |  | ||||||
|         public lastName: string |  | ||||||
|     ) { |  | ||||||
|         super(); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -7,12 +7,4 @@ import { TeacherRepository } from '../../data/users/teacher-repository.js'; | ||||||
| export class Teacher extends User { | export class Teacher extends User { | ||||||
|     @ManyToMany(() => Class) |     @ManyToMany(() => Class) | ||||||
|     classes!: Collection<Class>; |     classes!: Collection<Class>; | ||||||
| 
 |  | ||||||
|     constructor( |  | ||||||
|         public username: string, |  | ||||||
|         public firstName: string, |  | ||||||
|         public lastName: string |  | ||||||
|     ) { |  | ||||||
|         super(); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| import { Student } from '../entities/users/student.entity.js'; | import { Student } from '../entities/users/student.entity.js'; | ||||||
|  | import {getStudentRepository} from "../data/repositories"; | ||||||
| 
 | 
 | ||||||
| export interface StudentDTO { | export interface StudentDTO { | ||||||
|     id: string; |     id: string; | ||||||
|  | @ -23,7 +24,9 @@ export function mapToStudentDTO(student: Student): StudentDTO { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function mapToStudent(studentData: StudentDTO): Student { | export function mapToStudent(studentData: StudentDTO): Student { | ||||||
|     const student = new Student(studentData.username, studentData.firstName, studentData.lastName); |     return getStudentRepository().create({ | ||||||
| 
 |         username: studentData.username, | ||||||
|     return student; |         firstName: studentData.firstName, | ||||||
|  |         lastName: studentData.lastName | ||||||
|  |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| import { Teacher } from '../entities/users/teacher.entity.js'; | import { Teacher } from '../entities/users/teacher.entity.js'; | ||||||
|  | import {getTeacherRepository} from "../data/repositories"; | ||||||
| 
 | 
 | ||||||
| export interface TeacherDTO { | export interface TeacherDTO { | ||||||
|     id: string; |     id: string; | ||||||
|  | @ -22,8 +23,10 @@ export function mapToTeacherDTO(teacher: Teacher): TeacherDTO { | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function mapToTeacher(TeacherData: TeacherDTO): Teacher { | export function mapToTeacher(teacherData: TeacherDTO): Teacher { | ||||||
|     const teacher = new Teacher(TeacherData.username, TeacherData.firstName, TeacherData.lastName); |     return getTeacherRepository().create({ | ||||||
| 
 |         username: teacherData.username, | ||||||
|     return teacher; |         firstName: teacherData.firstName, | ||||||
|  |         lastName: teacherData.lastName | ||||||
|  |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,6 +1,4 @@ | ||||||
| import { getClassRepository, getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | import { getClassRepository, getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | ||||||
| import { Class } from '../entities/classes/class.entity.js'; |  | ||||||
| import { Student } from '../entities/users/student.entity.js'; |  | ||||||
| import { AssignmentDTO } from '../interfaces/assignment.js'; | import { AssignmentDTO } from '../interfaces/assignment.js'; | ||||||
| import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | ||||||
| import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | ||||||
|  | @ -29,7 +27,7 @@ export async function createStudent(userData: StudentDTO): Promise<StudentDTO | | ||||||
|     const studentRepository = getStudentRepository(); |     const studentRepository = getStudentRepository(); | ||||||
| 
 | 
 | ||||||
|     try { |     try { | ||||||
|         const newStudent = studentRepository.create(mapToStudent(userData)); |         const newStudent = mapToStudent(userData); | ||||||
|         await studentRepository.save(newStudent); |         await studentRepository.save(newStudent); | ||||||
| 
 | 
 | ||||||
|         return mapToStudentDTO(newStudent); |         return mapToStudentDTO(newStudent); | ||||||
|  | @ -87,9 +85,7 @@ export async function getStudentAssignments(username: string, full: boolean): Pr | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     const classes = await classRepository.findByStudent(student); |     const classes = await classRepository.findByStudent(student); | ||||||
| 
 | 
 | ||||||
|     const assignments = (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); |     return (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); | ||||||
| 
 |  | ||||||
|     return assignments; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> { | export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> { | ||||||
|  |  | ||||||
|  | @ -1,5 +1,4 @@ | ||||||
| import { setupTestApp } from '../../setup-tests.js'; | import { setupTestApp } from '../../setup-tests.js'; | ||||||
| import { Student } from '../../../src/entities/users/student.entity.js'; |  | ||||||
| import { describe, it, expect, beforeAll } from 'vitest'; | import { describe, it, expect, beforeAll } from 'vitest'; | ||||||
| import { StudentRepository } from '../../../src/data/users/student-repository.js'; | import { StudentRepository } from '../../../src/data/users/student-repository.js'; | ||||||
| import { getStudentRepository } from '../../../src/data/repositories.js'; | import { getStudentRepository } from '../../../src/data/repositories.js'; | ||||||
|  | @ -30,7 +29,7 @@ describe('StudentRepository', () => { | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('should return the queried student after he was added', async () => { |     it('should return the queried student after he was added', async () => { | ||||||
|         await studentRepository.insert(new Student(username, firstName, lastName)); |         await studentRepository.insert(studentRepository.create({username, firstName, lastName})); | ||||||
| 
 | 
 | ||||||
|         const retrievedStudent = await studentRepository.findByUsername(username); |         const retrievedStudent = await studentRepository.findByUsername(username); | ||||||
|         expect(retrievedStudent).toBeTruthy(); |         expect(retrievedStudent).toBeTruthy(); | ||||||
|  |  | ||||||
|  | @ -2,7 +2,6 @@ import { describe, it, expect, beforeAll } from 'vitest'; | ||||||
| import { TeacherRepository } from '../../../src/data/users/teacher-repository'; | import { TeacherRepository } from '../../../src/data/users/teacher-repository'; | ||||||
| import { setupTestApp } from '../../setup-tests'; | import { setupTestApp } from '../../setup-tests'; | ||||||
| import { getTeacherRepository } from '../../../src/data/repositories'; | import { getTeacherRepository } from '../../../src/data/repositories'; | ||||||
| import { Teacher } from '../../../src/entities/users/teacher.entity'; |  | ||||||
| 
 | 
 | ||||||
| const username = 'testteacher'; | const username = 'testteacher'; | ||||||
| const firstName = 'John'; | const firstName = 'John'; | ||||||
|  | @ -30,7 +29,7 @@ describe('TeacherRepository', () => { | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('should return the queried teacher after he was added', async () => { |     it('should return the queried teacher after he was added', async () => { | ||||||
|         await teacherRepository.insert(new Teacher(username, firstName, lastName)); |         await teacherRepository.insert(teacherRepository.create({username, firstName, lastName})); | ||||||
| 
 | 
 | ||||||
|         const retrievedTeacher = await teacherRepository.findByUsername(username); |         const retrievedTeacher = await teacherRepository.findByUsername(username); | ||||||
|         expect(retrievedTeacher).toBeTruthy(); |         expect(retrievedTeacher).toBeTruthy(); | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger