public ActionResult <Transcript> AddGrade(StudentGrade studentGrade) { if (!ModelState.IsValid) { return(BadRequest()); } if (!_studentsRepo.Exists(studentGrade.StudentID)) { return(NotFound("Student not found")); } if (!_coursesRepo.Exists(studentGrade.CourseID)) { return(NotFound("Course not found")); } var addedGrade = _repo.AddGrade(studentGrade); if (addedGrade == null) { return(BadRequest("Failed to add grade. Please check request values and try again")); // ideally we would let the consumer know exactly why the request failed } return(Created("Grade was added", addedGrade)); }
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); } }