feat: PUT request op assignment geimplementeerd
This commit is contained in:
		
							parent
							
								
									709d5f019a
								
							
						
					
					
						commit
						800d52257c
					
				
					 6 changed files with 37 additions and 6 deletions
				
			
		|  | @ -3,6 +3,7 @@ import { createAssignment, deleteAssignment, getAllAssignments, getAssignment, g | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | ||||||
| import {requireFields} from "./error-helper"; | import {requireFields} from "./error-helper"; | ||||||
| import {BadRequestException} from "../exceptions/bad-request-exception"; | import {BadRequestException} from "../exceptions/bad-request-exception"; | ||||||
|  | import { getLogger } from '../logging/initalize.js'; | ||||||
| 
 | 
 | ||||||
| export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> { | export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const classId = req.params.classid; |     const classId = req.params.classid; | ||||||
|  |  | ||||||
|  | @ -10,7 +10,8 @@ import { | ||||||
|     getClass, |     getClass, | ||||||
|     getClassStudents, |     getClassStudents, | ||||||
|     getClassTeacherInvitations, |     getClassTeacherInvitations, | ||||||
|     getClassTeachers |     getClassTeachers, | ||||||
|  |     putClass | ||||||
| } from '../services/classes.js'; | } from '../services/classes.js'; | ||||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||||
| import {requireFields} from "./error-helper"; | import {requireFields} from "./error-helper"; | ||||||
|  | @ -41,6 +42,16 @@ export async function getClassHandler(req: Request, res: Response): Promise<void | ||||||
|     res.json({ class: cls }); |     res.json({ class: cls }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export async function putClassHandler(req: Request, res: Response): Promise<void> { | ||||||
|  |     const classId = req.params.id; | ||||||
|  |     requireFields({ classId }); | ||||||
|  | 
 | ||||||
|  |     const newData = req.body as Partial<ClassDTO>; | ||||||
|  |     const cls = await putClass(classId, newData); | ||||||
|  | 
 | ||||||
|  |     res.json({ class: cls }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function deleteClassHandler(req: Request, res: Response): Promise<void> { | export async function deleteClassHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const classId = req.params.id; |     const classId = req.params.id; | ||||||
|     const cls = await deleteClass(classId); |     const cls = await deleteClass(classId); | ||||||
|  |  | ||||||
|  | @ -4,28 +4,28 @@ import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||||
| import { Class } from '../entities/classes/class.entity.js'; | import { Class } from '../entities/classes/class.entity.js'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | import { getLogger } from '../logging/initalize.js'; | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | ||||||
|  | import { mapToGroupDTO } from './group.js'; | ||||||
| 
 | 
 | ||||||
| export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||||
|     return { |     return { | ||||||
|         id: assignment.id!, |         id: assignment.id!, | ||||||
|         class: assignment.within.classId!, |         within: assignment.within.classId!, | ||||||
|         title: assignment.title, |         title: assignment.title, | ||||||
|         description: assignment.description, |         description: assignment.description, | ||||||
|         learningPath: assignment.learningPathHruid, |         learningPath: assignment.learningPathHruid, | ||||||
|         language: assignment.learningPathLanguage, |         language: assignment.learningPathLanguage, | ||||||
|         // Groups: assignment.groups.map(group => group.groupNumber),
 |  | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||||
|     return { |     return { | ||||||
|         id: assignment.id!, |         id: assignment.id!, | ||||||
|         class: assignment.within.classId!, |         within: assignment.within.classId!, | ||||||
|         title: assignment.title, |         title: assignment.title, | ||||||
|         description: assignment.description, |         description: assignment.description, | ||||||
|         learningPath: assignment.learningPathHruid, |         learningPath: assignment.learningPathHruid, | ||||||
|         language: assignment.learningPathLanguage, |         language: assignment.learningPathLanguage, | ||||||
|         // Groups: assignment.groups.map(mapToGroupDTO),
 |         // groups: assignment.groups.map(mapToGroupDTO),
 | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ import { | ||||||
|     getClassHandler, |     getClassHandler, | ||||||
|     getClassStudentsHandler, getClassTeachersHandler, |     getClassStudentsHandler, getClassTeachersHandler, | ||||||
|     getTeacherInvitationsHandler, |     getTeacherInvitationsHandler, | ||||||
|  |     putClassHandler, | ||||||
| } from '../controllers/classes.js'; | } from '../controllers/classes.js'; | ||||||
| import assignmentRouter from './assignments.js'; | import assignmentRouter from './assignments.js'; | ||||||
| 
 | 
 | ||||||
|  | @ -22,6 +23,8 @@ router.post('/', createClassHandler); | ||||||
| 
 | 
 | ||||||
| router.get('/:id', getClassHandler); | router.get('/:id', getClassHandler); | ||||||
| 
 | 
 | ||||||
|  | router.put('/:id', putClassHandler); | ||||||
|  | 
 | ||||||
| router.delete('/:id', deleteClassHandler); | router.delete('/:id', deleteClassHandler); | ||||||
| 
 | 
 | ||||||
| router.get('/:id/teacher-invitations', getTeacherInvitationsHandler); | router.get('/:id/teacher-invitations', getTeacherInvitationsHandler); | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submissi | ||||||
| import { fetchClass } from './classes.js'; | 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'; | ||||||
| 
 | 
 | ||||||
| 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(); | ||||||
|  |  | ||||||
|  | @ -9,9 +9,10 @@ import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||||
| import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation'; | import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation'; | ||||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||||
| import {fetchTeacher} from "./teachers"; | import {fetchTeacher} from "./teachers"; | ||||||
| import {fetchStudent} from "./students"; | 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'; | ||||||
| 
 | 
 | ||||||
| export async function fetchClass(classid: string): Promise<Class> { | export async function fetchClass(classid: string): Promise<Class> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -57,6 +58,16 @@ 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> { | ||||||
|  |     const cls = await fetchClass(classId); | ||||||
|  | 
 | ||||||
|  |     const classRepository = getClassRepository(); | ||||||
|  |     classRepository.assign(cls, classData as Partial<EntityDTO<Class>>); | ||||||
|  |     await classRepository.getEntityManager().flush(); | ||||||
|  | 
 | ||||||
|  |     return mapToClassDTO(cls); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function deleteClass(classId: string): Promise<ClassDTO> { | export async function deleteClass(classId: string): Promise<ClassDTO> { | ||||||
|     const cls = await fetchClass(classId); |     const cls = await fetchClass(classId); | ||||||
| 
 | 
 | ||||||
|  | @ -107,6 +118,7 @@ export async function deleteClassStudent(classId: string, username: string): Pro | ||||||
|      |      | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     classRepository.assign(cls, { students: cls.students.filter((student) => student.username !== username) }); |     classRepository.assign(cls, { students: cls.students.filter((student) => student.username !== username) }); | ||||||
|  |     await classRepository.getEntityManager().flush(); | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  | @ -116,6 +128,7 @@ export async function deleteClassTeacher(classId: string, username: string): Pro | ||||||
|      |      | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     classRepository.assign(cls, { teachers: cls.teachers.filter((teacher) => teacher.username !== username) }); |     classRepository.assign(cls, { teachers: cls.teachers.filter((teacher) => teacher.username !== username) }); | ||||||
|  |     await classRepository.getEntityManager().flush(); | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  | @ -126,6 +139,7 @@ export async function addClassStudent(classId: string, username: string): Promis | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     classRepository.assign(cls, { students: [...cls.students, newStudent] }); |     classRepository.assign(cls, { students: [...cls.students, newStudent] }); | ||||||
|  |     await classRepository.getEntityManager().flush(); | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  | @ -136,6 +150,7 @@ export async function addClassTeacher(classId: string, username: string): Promis | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     classRepository.assign(cls, { teachers: [...cls.teachers, newTeacher] }); |     classRepository.assign(cls, { teachers: [...cls.teachers, newTeacher] }); | ||||||
|  |     await classRepository.getEntityManager().flush(); | ||||||
| 
 | 
 | ||||||
|     return mapToClassDTO(cls); |     return mapToClassDTO(cls); | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet