Ejemplo n.º 1
0
        public IActionResult UpdateMovie(int movieID, [FromBody] MovieViewModel updatedMovie)
        {
            // Validate
            if (!ModelState.IsValid)
            {
                return(StatusCode(412));
            }
            // Check if the movie exists
            if (!_MovieService.MovieExistsByID(movieID))
            {
                return(NotFound());
            }

            var updatedMovieDTO = _MovieService.UpdateMovie(movieID, updatedMovie);

            if (updatedMovieDTO == null)
            {
                return(BadRequest("Failed to update movie"));
            }
            // If genre is being updated
            if (updatedMovie.genre != null)
            {
                if (!_GenreService.DeleteMovieFromGenres(movieID))
                {
                    return(BadRequest("Failed to remove movie-genre relations"));
                }
                // Add an entry for all genres in the movie-genre relation table
                if (!_GenreService.AddGenreToMovie(updatedMovieDTO.movieID, updatedMovie.genre))
                {
                    return(BadRequest("Failed to add genre to movie"));
                }
            }
            updatedMovieDTO.genre = _GenreService.GetGenreByMovieID(movieID);

            return(Ok(updatedMovieDTO));
        }