fix(backend): Modellering van ManyToMany-relationships verbeterd
This commit is contained in:
		
							parent
							
								
									f4fda7db5d
								
							
						
					
					
						commit
						451ac7546f
					
				
					 6 changed files with 13 additions and 17 deletions
				
			
		|  | @ -18,6 +18,8 @@ export class Group { | ||||||
| 
 | 
 | ||||||
|     @ManyToMany({ |     @ManyToMany({ | ||||||
|         entity: () => Student, |         entity: () => Student, | ||||||
|  |         owner: true, | ||||||
|  |         inversedBy: "groups" | ||||||
|     }) |     }) | ||||||
|     members!: Student[]; |     members: Collection<Student> = new Collection<Student>(this); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -14,9 +14,9 @@ export class Class { | ||||||
|     @Property({ type: 'string' }) |     @Property({ type: 'string' }) | ||||||
|     displayName!: string; |     displayName!: string; | ||||||
| 
 | 
 | ||||||
|     @ManyToMany(() => Teacher) |     @ManyToMany({entity: () => Teacher, owner: true, inversedBy: 'classes'}) | ||||||
|     teachers!: Collection<Teacher>; |     teachers!: Collection<Teacher>; | ||||||
| 
 | 
 | ||||||
|     @ManyToMany(() => Student) |     @ManyToMany({entity: () => Student, owner: true, inversedBy: 'classes'}) | ||||||
|     students!: Collection<Student>; |     students!: Collection<Student>; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -8,9 +8,9 @@ import { StudentRepository } from '../../data/users/student-repository.js'; | ||||||
|     repository: () => StudentRepository, |     repository: () => StudentRepository, | ||||||
| }) | }) | ||||||
| export class Student extends User { | export class Student extends User { | ||||||
|     @ManyToMany(() => Class) |     @ManyToMany({entity: () => Class, mappedBy: 'students'}) | ||||||
|     classes!: Collection<Class>; |     classes!: Collection<Class>; | ||||||
| 
 | 
 | ||||||
|     @ManyToMany(() => Group) |     @ManyToMany({entity: () => Group, mappedBy: 'members'}) | ||||||
|     groups!: Collection<Group>; |     groups: Collection<Group> = new Collection<Group>(this); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -5,6 +5,6 @@ import { TeacherRepository } from '../../data/users/teacher-repository.js'; | ||||||
| 
 | 
 | ||||||
| @Entity({ repository: () => TeacherRepository }) | @Entity({ repository: () => TeacherRepository }) | ||||||
| export class Teacher extends User { | export class Teacher extends User { | ||||||
|     @ManyToMany(() => Class) |     @ManyToMany({entity: () => Class, mappedBy: 'teachers'}) | ||||||
|     classes!: Collection<Class>; |     classes!: Collection<Class>; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ export function errorHandler(err: unknown, _req: Request, res: Response, _: Next | ||||||
|         logger.warn(`An error occurred while handling a request: ${err} (-> HTTP ${err.status})`); |         logger.warn(`An error occurred while handling a request: ${err} (-> HTTP ${err.status})`); | ||||||
|         res.status(err.status).json(err); |         res.status(err.status).json(err); | ||||||
|     } else { |     } else { | ||||||
|         logger.error(`Unexpected error occurred while handing a request: ${JSON.stringify(err)}`); |         logger.error(`Unexpected error occurred while handing a request: ${err}`); | ||||||
|         res.status(500).json(err); |         res.status(500).json(err); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| import { Collection, EntityDTO } from '@mikro-orm/core'; | import { EntityDTO } from '@mikro-orm/core'; | ||||||
| import { getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | import { getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | ||||||
| import { Group } from '../entities/assignments/group.entity.js'; | import { Group } from '../entities/assignments/group.entity.js'; | ||||||
| import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | ||||||
|  | @ -9,8 +9,6 @@ import { fetchAssignment } from './assignments.js'; | ||||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
| import { putObject } from './service-helper.js'; | import { putObject } from './service-helper.js'; | ||||||
| import { Student } from '../entities/users/student.entity.js'; | import { Student } from '../entities/users/student.entity.js'; | ||||||
| import { GroupRepository } from '../data/assignments/group-repository.js'; |  | ||||||
| import { assert } from 'console'; |  | ||||||
| 
 | 
 | ||||||
| export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> { | export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> { | ||||||
|     const assignment = await fetchAssignment(classId, assignmentNumber); |     const assignment = await fetchAssignment(classId, assignmentNumber); | ||||||
|  | @ -76,13 +74,9 @@ export async function createGroup(groupData: GroupDTO, classid: string, assignme | ||||||
|     const groupRepository = getGroupRepository(); |     const groupRepository = getGroupRepository(); | ||||||
|     const newGroup = groupRepository.create({ |     const newGroup = groupRepository.create({ | ||||||
|         assignment: assignment, |         assignment: assignment, | ||||||
|         members: members, |         members: members | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     assert(newGroup.groupNumber !== undefined, "NO GROUPNUMBER WAS ASSIGNED"); |  | ||||||
| 
 |  | ||||||
|     console.log(newGroup); |  | ||||||
| 
 |  | ||||||
|     try { |     try { | ||||||
|         await groupRepository.save(newGroup); |         await groupRepository.save(newGroup); | ||||||
|     } catch(e) { |     } catch(e) { | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger