feat: PUT request geabstraheerd naar service-helper.ts
This commit is contained in:
		
							parent
							
								
									800d52257c
								
							
						
					
					
						commit
						541e8ab2d5
					
				
					 4 changed files with 57 additions and 17 deletions
				
			
		|  | @ -43,6 +43,20 @@ export async function getAssignmentHandler(req: Request, res: Response): Promise | ||||||
|     res.json({ assignment }); |     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> { | export async function deleteAssignmentHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const id = Number(req.params.id); |     const id = Number(req.params.id); | ||||||
|     const classid = req.params.classid; |     const classid = req.params.classid; | ||||||
|  |  | ||||||
|  | @ -9,6 +9,9 @@ import { fetchClass } from './classes.js'; | ||||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | 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> { | export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -58,6 +61,14 @@ export async function getAssignment(classid: string, id: number): Promise<Assign | ||||||
|     return mapToAssignmentDTO(assignment); |     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> { | export async function deleteAssignment(classid: string, id: number): Promise<AssignmentDTO> { | ||||||
|     const assignment = await fetchAssignment(classid, id); |     const assignment = await fetchAssignment(classid, id); | ||||||
|     const cls = await fetchClass(classid); |     const cls = await fetchClass(classid); | ||||||
|  |  | ||||||
|  | @ -13,6 +13,7 @@ import {fetchStudent, getStudent} from "./students"; | ||||||
| import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher"; | import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher"; | ||||||
| import {mapToTeacherDTO} from "../interfaces/teacher"; | import {mapToTeacherDTO} from "../interfaces/teacher"; | ||||||
| import { EntityDTO } from '@mikro-orm/core'; | import { EntityDTO } from '@mikro-orm/core'; | ||||||
|  | import { putObject } from './service-helper.js'; | ||||||
| 
 | 
 | ||||||
| export async function fetchClass(classid: string): Promise<Class> { | export async function fetchClass(classid: string): Promise<Class> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -58,12 +59,10 @@ export async function createClass(classData: ClassDTO): Promise<ClassDTO> { | ||||||
|     return mapToClassDTO(newClass); |     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 cls = await fetchClass(classId); | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     await putObject<Class, ClassDTO>(cls, classData, getClassRepository()); | ||||||
|     classRepository.assign(cls, classData as Partial<EntityDTO<Class>>); |  | ||||||
|     await classRepository.getEntityManager().flush(); |  | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     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> { | export async function deleteClassStudent(classId: string, username: string): Promise<ClassDTO> { | ||||||
|     const cls = await fetchClass(classId); |     const cls = await fetchClass(classId); | ||||||
|      |      | ||||||
|     const classRepository = getClassRepository(); |     const newStudents = { students: cls.students.filter((student) => student.username !== username) }; | ||||||
|     classRepository.assign(cls, { students: cls.students.filter((student) => student.username !== username) }); |     await putObject<Class, ClassDTO>(cls, newStudents, getClassRepository()); | ||||||
|     await classRepository.getEntityManager().flush(); |  | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     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> { | export async function deleteClassTeacher(classId: string, username: string): Promise<ClassDTO> { | ||||||
|     const cls = await fetchClass(classId); |     const cls = await fetchClass(classId); | ||||||
|      |      | ||||||
|     const classRepository = getClassRepository(); |     const newTeachers = { teachers: cls.teachers.filter((teacher) => teacher.username !== username) }; | ||||||
|     classRepository.assign(cls, { teachers: cls.teachers.filter((teacher) => teacher.username !== username) }); |     await putObject<Class, ClassDTO>(cls, newTeachers, getClassRepository()); | ||||||
|     await classRepository.getEntityManager().flush(); |  | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  | @ -137,9 +134,8 @@ export async function addClassStudent(classId: string, username: string): Promis | ||||||
|     const cls = await fetchClass(classId); |     const cls = await fetchClass(classId); | ||||||
|     const newStudent = await fetchStudent(username); |     const newStudent = await fetchStudent(username); | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const newStudents = { students: [...cls.students, newStudent] } | ||||||
|     classRepository.assign(cls, { students: [...cls.students, newStudent] }); |     await putObject<Class, ClassDTO>(cls, newStudents, getClassRepository()); | ||||||
|     await classRepository.getEntityManager().flush(); |  | ||||||
|      |      | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  | @ -148,9 +144,8 @@ export async function addClassTeacher(classId: string, username: string): Promis | ||||||
|     const cls = await fetchClass(classId); |     const cls = await fetchClass(classId); | ||||||
|     const newTeacher = await fetchTeacher(username); |     const newTeacher = await fetchTeacher(username); | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const newTeachers = { teachers: [...cls.teachers, newTeacher] }; | ||||||
|     classRepository.assign(cls, { teachers: [...cls.teachers, newTeacher] }); |     await putObject<Class, ClassDTO>(cls, newTeachers, getClassRepository());     | ||||||
|     await classRepository.getEntityManager().flush(); |  | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										20
									
								
								backend/src/services/service-helper.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								backend/src/services/service-helper.ts
									
										
									
									
									
										Normal 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(); | ||||||
|  | } | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet