public PartialViewResult GradeStudent(int courseId) { var model = new GradeStudentViewModel() { CourseId = courseId }; return(PartialView(model)); }
public JsonResult GradeStudent(GradeStudentViewModel model) { if (!ModelState.IsValid) { return(Json("'Success':'false', 'Error' : 'Invalid grade information! Grades should be between 2 and 6.'")); } var result = Service.GradeStudent(model, HttpContext.User.Identity.GetUserId()); return(Json(!result.Succeded ? $"'Success':'false','Error':'{result.Message}'" : "'Success':'true'")); }
public IExecutionResult GradeStudent(GradeStudentViewModel model, string userId) { var repo = unitOfWork.GetRepository <StudentsCourses>(); var checks = repo.FirstOrDefault( where : r => r.StudentId == model.StudentId && r.CourseId == model.CourseId, select: r => new { IsTrainer = r.Course.Trainer.Id == userId, CourseEndDate = r.Course.EndDate }); if (checks == null) { return(new ExecutionResult() { Succeded = false, Message = CourseMessages.CannotGradeNotSignedUp() }); } if (!checks.IsTrainer) { return(new ExecutionResult() { Succeded = false, Message = CourseMessages.NotTrainerInCourse() }); } if (checks.CourseEndDate.CompareTo(DateTime.Now) > 0) { return(new ExecutionResult() { Succeded = false, Message = CourseMessages.CannotGradeEndDateHasntPassed() }); } var relationship = repo.FirstOrDefault(where : r => r.StudentId == model.StudentId && r.CourseId == model.CourseId); relationship.Grade = model.Grade; repo.Update(relationship); unitOfWork.Save(); return(new ExecutionResult() { Succeded = true, Message = CourseMessages.SuccessfullyGraded() }); }