Ejemplo n.º 1
0
        public async Task <IActionResult> CreateAsync(int movieId, [FromBody] CreateMovieRatingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var movie = await _movieRepository.FindByAsync(movieId);

            if (movie == null)
            {
                return(NotFound($"Coulnd't find any movie with id {movieId}"));
            }

            var user = await _userRepository.FindByUserNameAsync(model.UserName);

            if (user == null)
            {
                return(NotFound($"Coulnd't find any user with user name: {model.UserName}"));
            }

            try
            {
                var movieRating = await _movieRatingRepository.CreateAsync(movie, user, model.Rating.Value);

                return(Ok(new CreateMovieRatingResult
                {
                    MovieId = movieRating.MovieId,
                    RatingId = movieRating.Id,
                    Rating = movieRating.Rating
                }));
            }
            catch (Exception ex)
            {
                //Log Exception
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }