public ActionResult AddMovie(IMDB movie)
 {
     if (ModelState.IsValid)
     {
         DbRepository.AddMovie(movie);
         DbRepository.Save();
     }
     return(RedirectToAction("MovieIndex"));
 }
Beispiel #2
0
        public IActionResult Post([FromBody] MovieViewModel movie)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newMovie = _mapper.Map <MovieViewModel, Movie>(movie);
                    if (movie.PosterUrl.StartsWith("/upload"))
                    {
                        string webRootPath = _env.WebRootPath;
                        string oldPath     = webRootPath + movie.PosterUrl;
                        if (System.IO.File.Exists(oldPath))
                        {
                            string ext     = oldPath.Substring(oldPath.LastIndexOf('.'));
                            string newPath = Path.Combine(webRootPath, "images", "movies") + "/" + movie.Name + ext;
                            System.IO.File.Move(oldPath, newPath);
                            newMovie.PosterUrl = "/images/movies/" + movie.Name + ext;
                        }
                    }
                    var id = _repository.AddMovie(newMovie);
                    if (id != 0)
                    {
                        var result = _repository.GetMovieById(id);
                        return(Created($"api/movies/{id}", _mapper.Map <Movie, MovieViewModel>(result)));
                    }
                    else
                    {
                        return(BadRequest("Movie already exists in database"));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }

                //return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to add movie to database: {ex}");
                return(BadRequest("Failed to add Movie to database"));
            }
        }