Ejemplo n.º 1
0
        public IHttpActionResult CreateNewRental(MovieRentalDto 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 MovieRental
                {
                    Customers = customer,
                    Movies    = movie,
                    DateAdded = DateTime.Now
                };
                _context.MovieRental.Add(rental);
            }
            _context.SaveChanges();

            return(Ok());
        }
        public IHttpActionResult CreateNewRentals(MovieRentalDto movieRentalDto)
        {
            var customer = _context.Customers.Single(
                c => c.Id == movieRentalDto.CustomerId);

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

            foreach (var movie in movies)
            {
                if (movieRentalDto.MovieIds.Count == 0)
                {
                    return(BadRequest("No Movie Ids have been given."));
                }
                movie.NumberAvailable--;
                var rental = new Rental
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now
                };

                _context.Rentals.Add(rental);
            }

            _context.SaveChanges();
            return(Ok());
        }
Ejemplo n.º 3
0
        public IHttpActionResult CreateRental(MovieRentalDto movieRental)
        {
            var customer = db.Customers.Find(movieRental.CustomerId);

            foreach (var movieId in movieRental.MovieId)
            {
                var movie = db.Movies.Find(movieId);
                if (movie == null)
                {
                    throw new Exception(HttpStatusCode.BadRequest.ToString());
                }

                var movieRentalObj = new MovieRental();
                movieRentalObj.Customers        = customer;
                movieRentalObj.Movies           = movie;
                movieRentalObj.Movies.Available = movieRentalObj.Movies.Available - 1;
                movieRentalObj.RentalDate       = DateTime.Now;

                db.MovieRentals.Add(movieRentalObj);
            }

            db.SaveChanges();

            return(Ok());
        }
Ejemplo n.º 4
0
        public IHttpActionResult CreateMovieRental(MovieRentalDto movieRental)
        {
            var customer = _db.Customers.SingleOrDefault(c => c.Id == movieRental.CustomerId);

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

            if (movieRental.MovieIds.Count == 0)
            {
                return(BadRequest("No movies selected"));
            }

            var movies = _db.Movies.Where(m => movieRental.MovieIds.Contains(m.Id)).ToList();

            if (movieRental.MovieIds.Count != movies.Count)
            {
                BadRequest("One or more invalid MovieIds");
            }

            foreach (var movie in movies)
            {
                if (movie.QuantityAvailable == 0)
                {
                    BadRequest("Movie is out of stock");
                }

                --movie.QuantityAvailable;

                var movieToRent = new RentalInfo
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now
                };

                _db.RentalInfos.Add(movieToRent);
            }

            _db.SaveChanges();
            return(Ok());
        }