예제 #1
0
        public IHttpActionResult CreateMovie(MovieDto movieDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var movie = Mapper.Map <MovieDto, Movie>(movieDto);

            _context.Movies.Add(movie);
            _context.SaveChanges();

            movieDto.Id = movie.Id;

            return(Created(new Uri(Request.RequestUri + "/" + movie.Id), movieDto));
        }
예제 #2
0
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            var customer = _context.Customers.Single(
                c => c.Id == newRental.CustomerId);

            var movies = _context.Movies.Where(
                m => newRental.MovieIds.Contains(m.Id)).ToList();

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

                movie.NumberAvailable--;

                var rental = new Rental
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now
                };

                _context.Rentals.Add(rental);
            }

            _context.SaveChanges();

            return(Ok());
        }
예제 #3
0
        public ActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new MovieFormViewModel(movie)
                {
                    Genres = _context.Genres.ToList()
                };

                return(View("MovieForm", viewModel));
            }

            if (movie.Id == 0)
            {
                movie.DateAdded = DateTime.Now;
                _context.Movies.Add(movie);
            }
            else
            {
                var movieInDb = _context.Movies.Single(m => m.Id == movie.Id);

                movieInDb.Name          = movie.Name;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.NumberInStock = movie.NumberInStock;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.Genre         = movie.Genre;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Movies"));
        }
예제 #4
0
        public IHttpActionResult CreateCustomer(CustomerDto customerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var customer = Mapper.Map <CustomerDto, Customer>(customerDto);

            _context.Customers.Add(customer);
            _context.SaveChanges();

            customerDto.Id = customer.Id;

            return(Created(new Uri(Request.RequestUri + "/" + customer.Id), customerDto));
        }
예제 #5
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipType.ToList()
                };

                return(View("CustomerForm", viewModel));
            }

            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

                customerInDb.Name                     = customer.Name;
                customerInDb.Birthdate                = customer.Birthdate;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
            }

            _context.SaveChanges();

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