Guest User

Untitled

a guest
Jan 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. [Route("apid/movies")]
  2. [ApiController]
  3. public class MoviesDController : ControllerBase
  4. {
  5. private readonly IRatingRepository _repository;
  6. private readonly IUserRepository _userRepository;
  7. private readonly IMovieRepository _movieRepository;
  8. public MoviesDController(IRatingRepository repository, IUserRepository userRepository, IMovieRepository movieRepository)
  9. {
  10. _repository = repository;
  11. _userRepository = userRepository;
  12. _movieRepository = movieRepository;
  13. }
  14.  
  15. // POST: apid/movies
  16. [HttpPost]
  17. public IActionResult Upsert([FromBody] Rating rating)
  18. {
  19. if (!ModelState.IsValid)
  20. {
  21. return BadRequest(ModelState);
  22. }
  23.  
  24. //1) Get the User
  25. var user = _userRepository.GetById(rating.UserId);
  26. if (user == null)
  27. {
  28. return NotFound("User not found...");
  29. }
  30.  
  31. //2) Get the Movie
  32. var movie = _movieRepository.GetById(rating.MovieId);
  33. if (movie == null)
  34. {
  35. return NotFound("Movie not found...");
  36. }
  37.  
  38. //3) Get Rating
  39. var existingRating = _repository.GetMovieRating(rating.UserId, rating.MovieId);
  40. if (existingRating == null)
  41. {
  42. //Insert
  43. _repository.InsertRating(rating);
  44.  
  45. return Ok("Rating added");
  46. }
  47.  
  48. existingRating.RatingValue = rating.RatingValue;
  49. //Update
  50. _repository.UpdateRating(existingRating);
  51.  
  52. return Ok("Rating updated");
  53. }
  54. }
Add Comment
Please, Sign In to add comment