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)); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("The movie is not available")); } movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRental(NewRentalDTO newRentalDto) { var customer = _context.Customers.Single(c => c.Id == newRentalDto.CustomerId); var books = _context.Books.Where(m => newRentalDto.BookIds.Contains(m.Id)).ToList(); foreach (var book in books) // looping because it's a one to many relationship { if (book.NumberAvailable <= 0) { return(BadRequest("Books is not available for rental")); } book.NumberAvailable--; Rental rental = new Rental { Customer = customer, Book = book, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(NewRentalDTO newRental) { if (!ModelState.IsValid) { return(BadRequest()); } //var customer = _context.Customers.Single(m=>m.Id == newRental.CustomerId).Select(Mapper.Map<Rental, NewRentalDTO>);/*.SingleOrDefault(c => c.Id == newRental.CustomerId).*/ var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where(m => newRental.MoviesIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.AvailableMovies == 0) { return(BadRequest(string.Format("{0} is nos available to rent.", movie.Name))); } movie.AvailableMovies--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now, }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRental(NewRentalDTO rentalDTO) { var customer = db.Customers.Get(rentalDTO.CustomerId); var movies = db.Movies.Find(m => rentalDTO.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 Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; db.Rentals.Add(rental); } db.Complete(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO newRental) { if (newRental.MovieIds.Count == 0) { return(BadRequest("No Movie Ids have been given.")); } var customer = _context.Customers.SingleOrDefault(i => i.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid.")); } var movies = _context.Movies.Where(i => newRental.MovieIds.Contains(i.Id)).ToList(); if (movies.Count != newRental.MovieIds.Count) { return(BadRequest("One or more MovieIds are invalid.")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; var rental = new Rental() { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }