public IActionResult updateCourseRating([FromBody] StudentCourseRatingDTO courseRatingDTO) { try { // TODO: also add the rating to mentor table var result = repository.UpdateCourseRating(courseRatingDTO); if (result == 1) { return(Ok(new { Message = "Rating submitted successfully." })); } else if (result == 3) { return(NotFound(new { Message = "Error: Course not found!" })); } else if (result == 4) { return(Conflict(new { Message = "Action cannot be completed. Either the course is already complete or has been rejected by Mentor." })); } else if (result == 5) { return(BadRequest(new { Message = "The course is still ongoing. You must complete the course to rate mentor." })); } return(BadRequest(new { Message = "Internal server error. Try again later." })); } catch (Exception e) { return(BadRequest(e.InnerException)); } }
public int UpdateCourseRating(StudentCourseRatingDTO courseRatingDTO) { try { var user = context.MODUsers.Where(u => u.Email == courseRatingDTO.Email).FirstOrDefault(); var course = (from studentCourse in context.StudentCourses join student in context.MODUsers on studentCourse.User equals student where student.Email == courseRatingDTO.Email && studentCourse.CourseId == courseRatingDTO.CourseId select studentCourse).FirstOrDefault(); var mentor = (from m in context.MODUsers join skill in context.MentorSkills on m equals skill.User where skill.SkillId == course.SkillId select m).FirstOrDefault(); if (course != null) { if (course.Rating < 6) { return(4); // already rated } else if (course.Status != 5 || courseRatingDTO.Rating > 5) { return(5); // course not completed yet } course.Rating = courseRatingDTO.Rating; mentor.TotalRatingsCount += 1; mentor.TotalRatingsSum += courseRatingDTO.Rating; mentor.TotalRating = mentor.TotalRatingsSum / mentor.TotalRatingsCount; var result = context.SaveChanges(); if (result > 0) { return(1); // success } return(2); // error updating status failed } return(3); // course not found } catch (Exception e) { throw e; } }