public IHttpActionResult CreateNewRentals(NewRentalsDto 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 Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalsDto newRentals) { var customer = _context.Customers.Single(m => m.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("This movie is not available at the moment")); } movie.NumberAvailable--; var rental = new NewRentals { Customer = customer, Movie = movie, DateRented = DateTime.Now, DateReturned = DateTime.Now.AddDays(30) }; _context.NewRentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalsDto newRentalsDto) { var customer = _context.Customers.SingleOrDefault(c => c.Id == newRentalsDto.CustomerId); if (customer == null) { return(BadRequest("Invalid Customer Id.")); } //Tolist() changes movies from IQueryable to List var moviesInDb = _context.Movies.Where(m => newRentalsDto.MovieIds.Contains(m.Id)).ToList(); foreach (var m in moviesInDb) { if (m.AvailableNumber <= 0) { return(BadRequest("Movie is not available in the stock")); } m.AvailableNumber--; var rental = new Rental() { Customer = customer, Movies = m, RentDate = DateTime.Now }; _context.Rentals.Add(rental); } try { _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); } return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalsDto newRental) { var customer = _context.Customers.SingleOrDefault(x => x.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("Customer ID is invalid.")); } if (newRental.MovieIds.Count == 0) { return(BadRequest("No Movie ID's have been given")); } var movies = _context.Movies.Where(x => newRental.MovieIds.Contains(x.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest()); } movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult NewRentals(NewRentalsDto viewModel) { var customer = _context.Customers.SingleOrDefault(c => c.Id == viewModel.CustomerId); // Edge case if (viewModel.MovieIds.Count == 0) { return(BadRequest("MovieIds cannot be empty.")); } // Edge case if (customer == null) { return(BadRequest($"Customer with id {viewModel.CustomerId} was not found.")); } var movies = _context.Movies.Where(m => viewModel.MovieIds.Contains(m.Id)).ToList(); // Edge casee if (movies.Count != viewModel.MovieIds.Count) { return(BadRequest("One or more MovieIds are invalid.")); } foreach (var movie in movies) { // Edge case if (movie.NumberAvailable == 0) { return(BadRequest($"Movie with id {movie.Id} is not available.")); } movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, RentDate = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalsDto newRentalsDto) { // To have all this conditions is a defensive approach and it's usually used for public APIs // For internal APIs is recomended to use the optimistic approach // For optimistic remove validation for customerId and movieIds, but leave avaliability check to maintain the logic if (newRentalsDto == null || !newRentalsDto.MovieIds.Any()) { return(BadRequest("No MovieIds have been given.")); } var customer = _context.Customers.SingleOrDefault(x => x.Id == newRentalsDto.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid.")); } var movies = _context.Movies.Where(x => newRentalsDto.MovieIds.Contains(x.Id)).ToList(); if (movies.Count != newRentalsDto.MovieIds.Count) { return(BadRequest("One or more movieIds are invalid.")); } foreach (var movie in movies) { if (movie.NumberAvaliable == 0) { return(BadRequest($"Movie with id {movie.Id} is not avaliable")); } movie.NumberAvaliable--; _context.Rentals.Add(new Rental() { Customer = customer, Movie = movie, DateRented = DateTime.Now }); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateRentail(NewRentalsDto newRentalsDto) { var customer = _context.Customers.SingleOrDefault( c => c.Id == newRentalsDto.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid")); } if (newRentalsDto.MovieIds.Count() == 0) { return(BadRequest("No Movie Ids have been given.")); } var movies = _context.Movies.Where( m => newRentalsDto.MovieIds.Contains(m.Id)).ToList(); if (movies.Count != newRentalsDto.MovieIds.Count) { return(BadRequest("One or more movie id is invalid")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie 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 CreateNewRentals(NewRentalsDto newRentalsDto) { var movies = _context.Movies.Where(m => newRentalsDto.MovieIds.Contains(m.Id)).ToList(); var customers = _context.Customers.Single(c => c.Id == newRentalsDto.CustomerId); if (movies.Count != newRentalsDto.MovieIds.Count) { return(BadRequest("One or more movies are invalid!")); } try { foreach (var movie in movies) { if (movie.NumberAvailible == 0) { return(BadRequest("Movie is not avalible.")); } movie.NumberAvailible--; var rentals = new Rental { Customer = customers, Movie = movie, DateRented = DateTime.Now, }; _context.Rentals.Add(rentals); } _context.SaveChanges(); } catch (DbEntityValidationException e) { Console.WriteLine(e); } return(Ok()); }
public IHttpActionResult GetMovies(NewRentalsDto rentalDto) { if (!ModelState.IsValid) { return(BadRequest()); } var customer = _context.Customers.Single(_ => _.Id == rentalDto.CustomerId); var movies = _context.Movies.Where(_ => rentalDto.MovieIds.Contains(_.Id)); foreach (var movie in movies) { movie.NumberAvailable--; var newRental = new NewRental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.NewRentals.Add(newRental); } try { _context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { Console.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage); } } } //rentalDto.CustomerId = rental.CustomerId; //return Created(new Uri(Request.RequestUri + "/" + rental.CustomerId), rentalDto); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalsDto newRentalsDto) { //if (!ModelState.IsValid) // return BadRequest(); //if (newRentalsDto.MovieIds.Count == 0) // return BadRequest("No Movie Ids have been given"); var customer = _context.Customers.Single(c => c.Id == newRentalsDto.CustomerId); var movies = _context.Movies.Where(m => newRentalsDto.MovieIds.Contains(m.Id)).ToList(); //if (movies.Count != newRentalsDto.MovieIds.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 rentals = new Rentals { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rentals); } //var rentals = Mapper.Map<NewRentalsDto, Rentals>(newRentalsDto); _context.SaveChanges(); return(NotFound()); }