Exemplo n.º 1
0
        public IHttpActionResult CreateNewRentals(NewRentalModel newRentalModel)
        {
            var customer = _applicationDbContext.Customers.ToList().Single(m => m.Id == newRentalModel.CustomerId);

            var moviesInDb = _applicationDbContext.Movies.Where(z => newRentalModel.MovieIdList.Contains(z.Id))
                             .ToList();

            foreach (var movie in moviesInDb)
            {
                if (movie.NumberAvailable == 0)
                {
                    return(BadRequest("Movie is not available"));
                }
                movie.NumberAvailable--;
                var rental = new Rental
                {
                    DateRented = DateTime.Today,
                    Customer   = customer,
                    Movie      = movie
                };
                _applicationDbContext.Rentals.Add(rental);
            }
            _applicationDbContext.SaveChanges();
            return(Ok());
        }
Exemplo n.º 2
0
        public IHttpActionResult CreateNewRentals(NewRentalModel newRental)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == newRental.CustomerId);

            if (customer == null)
            {
                return(BadRequest("Customer Id is not valid"));
            }

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

            if (newRental.MovieIds.Count != movies.Count)
            {
                return(BadRequest("Movie Id is not valid"));
            }

            try
            {
                string mess = "";
                foreach (var movie in movies)
                {
                    if (movie.NumberAvailable == 0)
                    {
                        mess += $"Movie {movie.Name} not available - ";
                    }
                    else
                    {
                        mess += movie.Name + " - ";
                        movie.NumberAvailable--;
                        var rental = new Rental()
                        {
                            Customer   = customer,
                            Movie      = movie,
                            DateRented = DateTime.Now
                        };
                        _context.Rentals.Add(rental);


                        _context.SaveChanges();
                    }
                }
                return(Ok(mess));
            } catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }