コード例 #1
0
        public IHttpActionResult CreateNewRental(MovieRentalsDto newRental)
        {
            var customer   = _db.Customers.Single(c => c.CustomerID == newRental.CustomerID);
            var moviesList = _db.Movies.Where(c => newRental.MovieIDs.Contains(c.MovieID)).ToList();

            Console.WriteLine(moviesList);
            foreach (var movie in moviesList)
            {
                if (movie.NumberAvailable == 0)
                {
                    return(BadRequest());
                }

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

                _db.MovieRentals.Add(rental);
            }

            _db.SaveChanges();
            return(Ok("Done"));
        }
コード例 #2
0
        public IHttpActionResult CreateNewMovieRentals(MovieRentalsDto newRentals)
        {
            var customer = _context.Customers.Single
                               (c => c.Id == newRentals.customerId);

            var movies = _context.Movies.Where
                             (m => newRentals.movieIds.Contains(m.Id));

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

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

                _context.MovieRentals.Add(rental);
            }

            _context.SaveChanges();
            return(Ok());
        }
コード例 #3
0
        // POST api/<controller>
        public IHttpActionResult Post(MovieRentalsDto movieRentalDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }



            if (movieRentalDto.MoviesId.Count == 0)
            {
                return(BadRequest("List of movies is empty"));
            }

            var customer = _context.Customers.SingleOrDefault(c => c.Id == movieRentalDto.CustomerId);

            if (customer == null)
            {
                return(BadRequest("Customer ID is invalid"));
            }

            var movies = _context.Movies.Where(m => movieRentalDto.MoviesId.Contains(m.Id)).ToList();

            if (movies.Count != movieRentalDto.MoviesId.Count)
            {
                return(BadRequest("One or more movies are invalid"));
            }

            var customerRentalLimit    = customer.MaxRentMoviesAtOnce;
            var moviesRentedByCustomer = _context.MovieRentals
                                         .Where(m => m.Customer.Id == customer.Id &&
                                                m.DateReturned == null).Count();

            if (movieRentalDto.MoviesId.Count + moviesRentedByCustomer > customerRentalLimit)
            {
                return(BadRequest("Customer Movies Rented is Up to Limit"));
            }


            MovieRental movieRental;

            foreach (Movie movie in movies)
            {
                if (movie.Avaliable == 0)
                {
                    return(BadRequest("Movie is not avaliable."));
                }

                movie.Avaliable--;
                movieRental            = new MovieRental();
                movieRental.Customer   = customer;
                movieRental.Movie      = movie;
                movieRental.DateRented = DateTime.Now;
                _context.MovieRentals.Add(movieRental);
            }
            _context.SaveChanges();

            return(Ok());
        }