From 3d5b96f23158f969e7cace16d66429effc20d578 Mon Sep 17 00:00:00 2001 From: zhangtianyi Date: Mon, 24 Jan 2022 23:54:48 +0800 Subject: [PATCH] add middleware GradeCreateValidCheck --- core/grading/controller.go | 2 +- core/grading/service.go | 19 +++++++++++++++++++ frontend/src/actions/moduleActions.js | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/grading/controller.go b/core/grading/controller.go index 9dea3ee..e3a7cbc 100644 --- a/core/grading/controller.go +++ b/core/grading/controller.go @@ -30,7 +30,7 @@ func (controller GradingController) CreateGradeRouter(route *mux.Router) { route.Use(utils.DecodeBodyMiddleware(&models.Grade{})) route.Use(controller.GradeCreatePermissionCheck()) // TODO: check if controllerade is valid (i.e., between min and max mark) - + route.Use(controller.GradeCreateValidCheck()) route.HandleFunc("", utils.DBCreateHandleFunc(controller.DB, true)).Methods(http.MethodPost) } diff --git a/core/grading/service.go b/core/grading/service.go index 14f97ce..ea52d4c 100644 --- a/core/grading/service.go +++ b/core/grading/service.go @@ -70,3 +70,22 @@ func (controller GradingController) DownloadPermissionCheck() mux.MiddlewareFunc }) } } + +func (controller GradingController) GradeCreateValidCheck() mux.MiddlewareFunc { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data := r.Context().Value(utils.DecodeBodyContextKey).(*models.Grade) + + var rubric models.Rubric + controller.DB.Model(&models.Rubric{}).Where("id = ?", data.RubricID).Find(&rubric) + + if data.Grade < rubric.MinMark { + utils.HandleResponse(w, "Grade cannot below minimum mark", http.StatusBadRequest) + } else if data.Grade > rubric.MaxMark { + utils.HandleResponse(w, "Grade cannot above maximum mark", http.StatusBadRequest) + } else { + next.ServeHTTP(w, r) + } + }) + } +} diff --git a/frontend/src/actions/moduleActions.js b/frontend/src/actions/moduleActions.js index 0e236c1..e74cbd9 100644 --- a/frontend/src/actions/moduleActions.js +++ b/frontend/src/actions/moduleActions.js @@ -459,6 +459,11 @@ export const postGrade = async (moduleId, gradeData) => { rubricId: gradeData.RubricID, comment: gradeData.Comment, grade: gradeData.Grade, + }).catch((err) => { + let errMessage = err.response.data.message + if (errMessage) { + alert("Grade For Rubric " + gradeData.RubricID + " : " + errMessage) + } }); };