public Grade TryUpdateStudentGradeInCourse(int studentId, int courseId, GradeContract gradeContract, out string message) { if (!Utilities.IsValidGradeContract(gradeContract, out message)) { return(null); } try { var grade = _gradesRepository.GetGradeOfStudentInCourse(studentId, courseId); if (grade == null) { message = "Student with matching id doesn't exist"; return(null); } grade.TestGrade = gradeContract.Grade; return(_gradesRepository.UpdateStudentGradeInCourse(grade.Id, grade)); } catch (DALException e) { // LOG throw new BLException("There was a problem adding new student", e); } }
public static bool IsValidGradeContract(GradeContract gradeContract, out string message) { if (gradeContract.Grade > 100 || gradeContract.Grade < 0) { message = "Grade should be between 0 to 100"; return(false); } message = string.Empty; return(true); }
public IActionResult AddGrade(int studentId, int courseId, [FromBody] GradeContract gradeContract) { try { var grade = _businessLogic.TryAddGrade(studentId, courseId, gradeContract, out var message); if (grade == null) { return(BadRequest(message)); } return(Ok(grade)); } catch (BLException e) { // Log here return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public Grade TryAddGrade(int studentId, int courseId, GradeContract gradeContract, out string message) { if (!Utilities.IsValidGradeContract(gradeContract, out message)) { return(null); } try { var student = _studentRepository.GetStudentById(studentId); if (student == null) { message = "Student with matching id doesn't exist"; return(null); } var course = _coursesRepository.GetCourseById(courseId); if (course == null) { message = "Course with matching id doesn't exist"; return(null); } var studentInCourse = _studentInCourseRepository.GetOrCreateStudentInCourse(studentId, courseId); var grade = new Grade { TestGrade = gradeContract.Grade, StudentInCourseId = studentInCourse.Id }; message = string.Empty; return(_gradesRepository.AddGrade(grade)); } catch (DALException e) { // LOG throw new BLException("There was a problem adding new grade", e); } }