Beispiel #1
0
        public IActionResult Post([FromBody] MovieDataViewModel newMovie)
        {
            try
            {
                if (newMovie == null)
                {
                    return(BadRequest("Movie can not be null"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var movie = Mapper.Map <MovieData>(newMovie);

                if (_repo.Create(movie) != null)
                {
                    return(Ok(newMovie));
                }

                return(BadRequest("Movie already existing"));
            }
            catch (Exception ex)
            {
                return(new ObjectResult(Response)
                {
                    StatusCode = 500,
                    DeclaredType = typeof(Exception)
                });
            }
        }
        public ActionResult Edit(int id)
        {
            var viewModel = new MovieDataViewModel(_context.movies.SingleOrDefault(m => m.id == id))
            {
                genres = _context.genres.ToList()
            };

            return(View("MovieData", viewModel));
        }
        public ActionResult New()
        {
            var viewModel = new MovieDataViewModel()
            {
                genres = _context.genres.ToList(),
            };

            return(View("MovieData", viewModel));
        }
        public ActionResult Save(Movies movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new MovieDataViewModel(movie)
                {
                    genres = _context.genres.ToList()
                };

                return(View("MovieData", viewModel));
            }
            //Movies movies = viewModel.movie;
            if (movie.id == 0)
            {
                movie.dateAdded = DateTime.Now;
                _context.movies.Add(movie);
            }
            else
            {
                Movies movieInDB = _context.movies.Single(m => m.id == movie.id);
                movieInDB.Name          = movie.Name;
                movieInDB.releaseDate   = movie.releaseDate;
                movieInDB.genreId       = movie.genreId;
                movieInDB.numberInStock = movie.numberInStock;
            }
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e);
            }

            return(RedirectToAction("Index", "Movies"));
        }