public IHttpActionResult CreateNewRental(NewRentalsDTO newRentalsDTO) { if (newRentalsDTO.movieIds.Count == 0) { return(BadRequest("Movies not supplied")); } var customer = _context.Customers.Single(c => c.Id == newRentalsDTO.customerId); var movieList = _context.Movies.Where( m => newRentalsDTO.movieIds.Contains(m.Id)).ToList(); foreach (Movies movie in movieList) { if (movie.NumberAvailable <= 0) { return(BadRequest("Movie is not available")); } movie.NumberAvailable--; _context.Rentals.Add(new Rentals() { Customers = customer, Movies = movie, DateRented = DateTime.Now }); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult newRental(NewRentalsDTO newRentalsDto) { //if (!ModelState.IsValid) // BadRequest(); if (newRentalsDto.moviesIds.Count == 0) { return(BadRequest("No Movies selected for rent!")); } var customer = dbContext.Customers.SingleOrDefault(c => c.Id == newRentalsDto.customerId); if (customer == null) { return(BadRequest("Invalid Customer ID!")); } var movies = dbContext.Movies.Where(m => newRentalsDto.moviesIds.Contains(m.Id)).ToList(); if (movies.Count != newRentalsDto.moviesIds.Count) { return(BadRequest("One or more Movies are invalid!")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie '" + movie.Name + "' is not available for rent!")); } movie.NumberAvailable--; var new_rental = new Rental() { Customer = customer, Movie = movie, DateRented = DateTime.Now }; dbContext.Rentals.Add(new_rental); } dbContext.SaveChanges(); return(Ok()); //throw new NotImplementedException(); }