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 {requireFields} from "./error-helper";
|
||||
import {BadRequestException} from "../exceptions/bad-request-exception";
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
|
||||
export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
|
|
|
@ -10,7 +10,8 @@ import {
|
|||
getClass,
|
||||
getClassStudents,
|
||||
getClassTeacherInvitations,
|
||||
getClassTeachers
|
||||
getClassTeachers,
|
||||
putClass
|
||||
} from '../services/classes.js';
|
||||
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
||||
import {requireFields} from "./error-helper";
|
||||
|
@ -41,6 +42,16 @@ export async function getClassHandler(req: Request, res: Response): Promise<void
|
|||
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> {
|
||||
const classId = req.params.id;
|
||||
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 { getLogger } from '../logging/initalize.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { mapToGroupDTO } from './group.js';
|
||||
|
||||
export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
|
||||
return {
|
||||
id: assignment.id!,
|
||||
class: assignment.within.classId!,
|
||||
within: assignment.within.classId!,
|
||||
title: assignment.title,
|
||||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
language: assignment.learningPathLanguage,
|
||||
// Groups: assignment.groups.map(group => group.groupNumber),
|
||||
};
|
||||
}
|
||||
|
||||
export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO {
|
||||
return {
|
||||
id: assignment.id!,
|
||||
class: assignment.within.classId!,
|
||||
within: assignment.within.classId!,
|
||||
title: assignment.title,
|
||||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
language: assignment.learningPathLanguage,
|
||||
// Groups: assignment.groups.map(mapToGroupDTO),
|
||||
// groups: assignment.groups.map(mapToGroupDTO),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
getClassHandler,
|
||||
getClassStudentsHandler, getClassTeachersHandler,
|
||||
getTeacherInvitationsHandler,
|
||||
putClassHandler,
|
||||
} from '../controllers/classes.js';
|
||||
import assignmentRouter from './assignments.js';
|
||||
|
||||
|
@ -22,6 +23,8 @@ router.post('/', createClassHandler);
|
|||
|
||||
router.get('/:id', getClassHandler);
|
||||
|
||||
router.put('/:id', putClassHandler);
|
||||
|
||||
router.delete('/:id', deleteClassHandler);
|
||||
|
||||
router.get('/:id/teacher-invitations', getTeacherInvitationsHandler);
|
||||
|
|
|
@ -8,6 +8,7 @@ import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submissi
|
|||
import { fetchClass } from './classes.js';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
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> {
|
||||
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 { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import {fetchTeacher} from "./teachers";
|
||||
import {fetchStudent} from "./students";
|
||||
import {fetchStudent, getStudent} from "./students";
|
||||
import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher";
|
||||
import {mapToTeacherDTO} from "../interfaces/teacher";
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
|
||||
export async function fetchClass(classid: string): Promise<Class> {
|
||||
const classRepository = getClassRepository();
|
||||
|
@ -57,6 +58,16 @@ export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
|
|||
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> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
|
@ -107,6 +118,7 @@ export async function deleteClassStudent(classId: string, username: string): Pro
|
|||
|
||||
const classRepository = getClassRepository();
|
||||
classRepository.assign(cls, { students: cls.students.filter((student) => student.username !== username) });
|
||||
await classRepository.getEntityManager().flush();
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
@ -116,6 +128,7 @@ export async function deleteClassTeacher(classId: string, username: string): Pro
|
|||
|
||||
const classRepository = getClassRepository();
|
||||
classRepository.assign(cls, { teachers: cls.teachers.filter((teacher) => teacher.username !== username) });
|
||||
await classRepository.getEntityManager().flush();
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
@ -126,6 +139,7 @@ export async function addClassStudent(classId: string, username: string): Promis
|
|||
|
||||
const classRepository = getClassRepository();
|
||||
classRepository.assign(cls, { students: [...cls.students, newStudent] });
|
||||
await classRepository.getEntityManager().flush();
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
@ -136,6 +150,7 @@ export async function addClassTeacher(classId: string, username: string): Promis
|
|||
|
||||
const classRepository = getClassRepository();
|
||||
classRepository.assign(cls, { teachers: [...cls.teachers, newTeacher] });
|
||||
await classRepository.getEntityManager().flush();
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue