Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add middleware GradeCreateValidCheck #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/grading/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
19 changes: 19 additions & 0 deletions core/grading/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
5 changes: 5 additions & 0 deletions frontend/src/actions/moduleActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
});
};

Expand Down