示例#1
0
        [HttpPut]              // Added by PW as preferred method. Microsoft suggest calling the function Put<Controller>
        public IHttpActionResult PutMovie(int id, MovieDto movieDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Movie movieInDb = _movieRepository.GetById(id);

            if (movieInDb == null)
            {
                return(BadRequest());
            }
            //throw new HttpResponseException(HttpStatusCode.NotFound);   // Old Code

            // Map the Movie DTO to the found Movie entity in the database.
            // Because we are supplying both souce and destination objects, AutoMapper
            // knows what types we are using, so instead of the following:-
            // Mapper.Map<MovieDto, Movie>(movieDto, movieInDb);
            // We can write as follows:-
            Mapper.Map(movieDto, movieInDb);

            _movieRepository.Update(movieInDb);
            _movieRepository.Commit();

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#2
0
        [HttpPost]              // Added by PW as preferred method. Microsoft suggest calling the function Post<ControllerName>
        public IHttpActionResult PostNewRental(NewRentalDto newRental)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (newRental.MovieIds.Count == 0)
            {
                return(BadRequest("No Movie IDs have been supplied"));
            }

            Customer customer = _customerRepository.GetById(newRental.CustomerId);

            if (customer == null)
            {
                return(BadRequest("Invalid customer ID supplied"));
            }


            // Get the movies where the movie ID is in the array of IDs supplied in the DTO
            var movies = db.Movies.Where(
                m => newRental.MovieIds.Contains(m.MovieId)).ToList();

            if (movies.Count != newRental.MovieIds.Count)
            {
                return(BadRequest("One or more movie IDs is incorrect."));
            }


            foreach (var movie in movies)
            {
                if (movie.NumberAvailable == 0)
                {
                    return(BadRequest("Movie is not available"));
                }

                var rental = new Rental();
                rental.CustomerId = newRental.CustomerId;
                rental.MovieId    = movie.MovieId;
                rental.DateRented = newRental.DateRented;

                _rentalRepository.Insert(rental);

                // Decrease the available number of this movie to rent
                movie.NumberAvailable--;    // Decrease the number available
                _movieRepository.Update(movie);
            }

            _rentalRepository.Commit();
            _movieRepository.Commit();

            return(Ok());
        }
示例#3
0
        public ActionResult DeleteConfirmed(int id)
        {
            MovieRepository movieRepository = new MovieRepository(db);
            Movie           movie           = movieRepository.GetById(id);

            if (movie == null)
            {
                return(HttpNotFound());
            }
            movieRepository.Delete(movie);
            movieRepository.Commit();
            return(RedirectToAction("Index"));
        }
示例#4
0
        public void ContextTest()
        {
            MainContext context = new MainContext("MainConnection");
            MovieRepository repository = new MovieRepository(context);

            var movie = repository.Add(new MovieDomain.Entities.Movie
            {
                Title = "Good Will Hunting"
            });

            repository.Commit();

            Assert.IsNotNull(movie);

        }
示例#5
0
        public ActionResult Edit(Movie movie)
        {
            if (ModelState.IsValid)
            {
                MovieRepository movieRepository = new MovieRepository(db);
                movie.DateAdded = DateTime.Now;
                movieRepository.SetState(movie, EntityState.Modified);
                movieRepository.Update(movie);
                movieRepository.Commit();
                return(RedirectToAction("Index"));
            }

            // Got here, so show the edit form again.
            AddUpdateMovieModel addUpdateMovieModel = new AddUpdateMovieModel(movie);   // Create the model from details
                                                                                        // supplied into the controller
            GenreRepository genreRepository = new GenreRepository(db);
            var             genres          = genreRepository.GetAll().ToList();

            addUpdateMovieModel.genres    = genres;
            addUpdateMovieModel.PageTitle = "Edit Movie";
            return(View(addUpdateMovieModel));
        }
示例#6
0
        public ActionResult Create(Movie movie)
        {
            if (movie.MovieId == 0)
            {
                MovieRepository movieRepository = new MovieRepository(db);
                movie.DateAdded = DateTime.Now;
                movieRepository.Insert(movie);  // Add the movie to the repository
                movieRepository.Commit();       // SaveChanges to the repository
                return(RedirectToAction("Index"));
            }


            // Got here, so show the Create form again with details input so far
            GenreRepository genreRepository = new GenreRepository(db);
            var             genres          = genreRepository.GetAll().ToList();

            AddUpdateMovieModel addUpdateMovieModel = new AddUpdateMovieModel(movie);   // Create the model from details supplied into the controller

            addUpdateMovieModel.genres    = genres;
            addUpdateMovieModel.PageTitle = "Create Movie";

            return(View(addUpdateMovieModel));
        }