Esempio n. 1
0
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            //var newRental = new NewRentalDto();
            //newRental.CustomerId = 2;
            //newRental.MovieIds = new List<int> {2,3,4};

            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 " + movie.Name + " is nor available."));
                }

                movie.NumberAvailable--;

                var newItem = new Rentals
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now,
                };

                _context.Rentals.Add(newItem);
            }

            _context.SaveChanges();

            return(Ok());
        }
Esempio n. 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 out of stock."));
                }

                movie.NumberAvailable--;
                var rental = new Rentals
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now
                };
                _context.Rentals.Add(rental);
            }
            _context.SaveChanges();

            return(Ok());
        }
Esempio n. 3
0
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            if (newRental.MovieIds.Count == 0)
            {
                return(BadRequest("No movie ids have been given"));
            }

            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 (movies.Count != newRental.MovieIds.Count)
            {
                return(BadRequest("On or more of the movie ids was invalid"));
            }

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

                movie.NumberAvailable--;

                var rental = new Rentals
                {
                    Customer   = customer,
                    Movies     = movie,
                    DateRented = DateTime.Now
                };

                _context.Rentals.Add(rental);
            }

            _context.SaveChanges();

            return(Ok());
        }
Esempio n. 4
0
        public IHttpActionResult AddMovieRental(NewRentalDto newRentalDto)
        {
            if (newRentalDto.MovieIds == null || newRentalDto.MovieIds.Count == 0)
            {
                return(BadRequest("No Movie Ids were provided"));
            }

            var customer = _context.Customers.FirstOrDefault(c => c.Id == newRentalDto.CustomerId);

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

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

            if (newRentalDto.MovieIds.Count != movies.Count)
            {
                return(BadRequest("One or more movie ids entered given was invalid or you have subscribed for the same movie more than once"));
            }

            foreach (var movie in movies)
            {
                if (movie.NumberAvailable == 0)
                {
                    return(BadRequest(String.Format("Movie: {0} is out of stock.", movie.Name)));
                }

                var rental = new Rentals()
                {
                    Movie      = movie,
                    Customer   = customer,
                    RentedDate = DateTime.Now
                };

                _context.Rentals.Add(rental);

                movie.NumberAvailable--;
            }

            _context.SaveChanges();

            return(Ok(true));
        }
Esempio n. 5
0
        [HttpPost] //aplies with restful conventions
        public IHttpActionResult NewRentals(NewRentalDto newRental)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest()); //helper method from IHttpActionresult
            }
            try
            {
                if (newRental.MovieId.Count == 0)
                {
                    return(BadRequest("No Movie Ids have been given"));
                }

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

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

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

                if (movies.Count != newRental.MovieId.Count)
                {
                    return(BadRequest("One or more movie Ids are invalid"));
                }


                foreach (var movie in movies)
                {
                    if (movie.NumberAvailable == 0)
                    {
                        return(BadRequest("Movie is not available"));
                    }
                    movie.NumberAvailable--;
                    var rental = new Rentals
                    {
                        Movies     = movie,
                        Customer   = customer,
                        DateRented = DateTime.Now
                    };
                    _context.Rentals.Add(rental);
                }

                _context.SaveChanges();
                //return Created(new Uri(Request.RequestUri + "/" + rentals.Id), newRental);//URI unified resource identifier = /api/customers/10
                //When you use created you provide the URL to the newly created resource. Since we are now creating multiple new records we
                //dont use created
                return(Ok());
            }

            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }