diff --git a/ExpressAPI/src/routes/AssignmentRoutes.ts b/ExpressAPI/src/routes/AssignmentRoutes.ts index ab2bd23456d446fd6a23a64e2bd9db7383b9b05c..c32ee2dfa6a061a997ca10a0067c592e9756814f 100644 --- a/ExpressAPI/src/routes/AssignmentRoutes.ts +++ b/ExpressAPI/src/routes/AssignmentRoutes.ts @@ -45,12 +45,23 @@ class AssignmentRoutes implements RoutesManager { } }; + private readonly assignmentAddCorrigeValidator: ExpressValidator.Schema = { + exerciseIdOrUrl: { + trim : true, + notEmpty: true, + custom : DojoValidators.exerciseIdOrUrlValidator + } + }; + registerOnBackend(backend: Express) { backend.get('/assignments/:assignmentNameOrUrl', SecurityMiddleware.check(true), this.getAssignment.bind(this)); backend.post('/assignments', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), ParamsValidatorMiddleware.validate(this.assignmentValidator), this.createAssignment.bind(this)); - backend.patch('/assignments/:assignmentNameOrUrl/publish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.publishAssignment.bind(this)); - backend.patch('/assignments/:assignmentNameOrUrl/unpublish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unpublishAssignment.bind(this)); + backend.patch('/assignments/:assignmentNameOrUrl/publish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.changeAssignmentPublishedStatus(true).bind(this)); + backend.patch('/assignments/:assignmentNameOrUrl/unpublish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.changeAssignmentPublishedStatus(false).bind(this)); + + backend.post('/assignments/:assignmentNameOrUrl/corrections', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentAddCorrigeValidator), this.addUpdateAssignmentCorrection(false).bind(this)); + backend.patch('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.addUpdateAssignmentCorrection(true).bind(this)); } // Get an assignment by its name or gitlab url @@ -171,14 +182,6 @@ class AssignmentRoutes implements RoutesManager { } } - private async publishAssignment(req: express.Request, res: express.Response) { - return this.changeAssignmentPublishedStatus(true)(req, res); - } - - private async unpublishAssignment(req: express.Request, res: express.Response) { - return this.changeAssignmentPublishedStatus(false)(req, res); - } - private changeAssignmentPublishedStatus(publish: boolean): (req: express.Request, res: express.Response) => Promise<void> { return async (req: express.Request, res: express.Response): Promise<void> => { if ( publish ) { @@ -213,6 +216,39 @@ class AssignmentRoutes implements RoutesManager { }; } + private addUpdateAssignmentCorrection(isUpdate: boolean): (req: express.Request, res: express.Response) => Promise<void> { + return async (req: express.Request, res: express.Response): Promise<void> => { + if ( req.boundParams.exercise?.assignmentName != req.boundParams.assignment?.name ) { + return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'The exercise does not belong to the assignment', DojoStatusCode.ASSIGNMENT_EXERCISE_NOT_RELATED); + } + + if ( !req.boundParams.assignment?.published ) { + return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'The assignment must be public', DojoStatusCode.ASSIGNMENT_NOT_PUBLISHED); + } + + if ( !isUpdate && req.boundParams.exercise?.isCorrection ) { + return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'This exercise is already a correction', DojoStatusCode.EXERCISE_CORRECTION_ALREADY_EXIST); + } else if ( isUpdate && !req.boundParams.exercise?.isCorrection ) { + return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'This exercise is not a correction', DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST); + } + + const lastCommit = await GitlabManager.getRepositoryLastCommit(req.boundParams.assignment!.gitlabId); + if ( lastCommit ) { + await db.exercise.update({ + where: { + id: req.boundParams.exercise!.id + }, + data : { + correctionCommit: lastCommit + } + }); + + return req.session.sendResponse(res, StatusCodes.OK); + } else { + return req.session.sendResponse(res, StatusCodes.INTERNAL_SERVER_ERROR, undefined, 'No last commit found'); + } + }; + } }