Exemplo n.º 1
0
        public async Task <ActionResult <MovieDTO> > CreateMovie(MovieForUpdateDTO movieToCreateDTO)
        {
            try
            {
                Director director = await _repository.Get <Director>(movieToCreateDTO.DirectorID);

                Genre genre = await _repository.Get <Genre>(movieToCreateDTO.GenreID);

                if (director == null)
                {
                    return(BadRequest($"The director with the id: {movieToCreateDTO.DirectorID} could not be found."));
                }
                if (genre == null)
                {
                    return(BadRequest($"The genre with the id: {movieToCreateDTO.GenreID} could not be found."));
                }

                Movie movie = new Movie
                {
                    Title       = movieToCreateDTO.Title,
                    BudgetInUsd = movieToCreateDTO.BudgetInUsd,
                    Country     = movieToCreateDTO.Country,
                    Language    = movieToCreateDTO.Language,
                    Rating      = movieToCreateDTO.Rating,
                    Length      = movieToCreateDTO.Length,
                    Year        = movieToCreateDTO.Year,
                    Director    = director,
                    Genre       = genre
                };
                var movieFromRepo = await _repository.Add(movie);

                if (movieFromRepo != null)
                {
                    return(Created($"/api/v1.0/movies/{movie.Id}", _mapper.Map <MovieDTO>(movie)));
                }
                return(BadRequest("Failed to create movie."));
            }
            catch (Exception e)
            {
                var result = new { Status = StatusCodes.Status500InternalServerError, Data = $"Failed to create the movie. Exception thrown when attempting to add data to the database: {e.Message}" };
                return(this.StatusCode(StatusCodes.Status500InternalServerError, result));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> UpdateMovieDetails(int id, MovieForUpdateDTO movieDTO)
        {
            try
            {
                var movieFromRepo = await _repository.Get <Movie>(id);

                if (movieFromRepo == null)
                {
                    return(NotFound($"Could not find the movie with the id {id}"));
                }

                var movieForUpdate = _mapper.Map(movieDTO, movieFromRepo);

                await _repository.Update(movieForUpdate);

                return(NoContent());
            }

            catch (Exception e)
            {
                var result = new { Status = StatusCodes.Status500InternalServerError, Data = $"Failed to update the movie. Exception thrown: {e.Message}" };
                return(this.StatusCode(StatusCodes.Status500InternalServerError, result));
            }
        }