public IHttpActionResult CreateRentals(RentalsDto rentalsDto) { if (!ModelState.IsValid) { return(BadRequest()); } var rental = Mapper.Map <RentalsDto, Rentals>(rentalsDto); var movie = _context.Movies.Single(m => m.Id == rentalsDto.MovieId); if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available at the moment")); } movie.NumberAvailable--; _context.Rentals.Add(rental); _context.SaveChanges(); rentalsDto.Id = rental.Id; return(Created(new Uri(Request.RequestUri + "/" + rental.Id), rentalsDto)); }
public IHttpActionResult Post(RentalsDto rentalsDto) { var customer = _context.Customer.Single(c => c.Id == rentalsDto.CustomerId); var movies = _context.Movies.Where(c => rentalsDto.MoviesId.Contains(c.Id)).ToList(); if (customer == null && movies == null) { return(NotFound()); } foreach (var movie in movies) { if (movie.NumbersAvailable == 0) { return(BadRequest("Movie Not Available")); } movie.NumbersAvailable--; var rent = new Rentals { DateRented = DateTime.Now, Customer = customer, Movies = movie, }; _context.Rentals.Add(rent); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult Create(RentalsDto createRental) { if (createRental.MovieIds.Count() == 0) { return(BadRequest("No movies selected.")); } var customer = _dbContext.Customers.SingleOrDefault(c => c.Id == createRental.CustomerId); if (customer == null) { return(BadRequest("Customer invalid.")); } var movies = _dbContext.Movies.Where(m => createRental.MovieIds.Contains(m.Id)).ToList(); if (movies.Count() != createRental.MovieIds.Count()) { return(BadRequest("One or more movies are invalid.")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("No movies available")); } movie.NumberAvailable--; Rental rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _dbContext.Rentals.Add(rental); } _dbContext.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRental(RentalsDto newRental) { if (newRental.MovieIds.Count() == 0) { return(BadRequest("No movieIds were provided.")); } var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid.")); } var movies = _context.Movies.Where( m => newRental.MovieIds.Contains(m.Id)).ToList(); if (movies.Count() != newRental.MovieIds.Count()) { return(BadRequest("One or more movieIds were 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, RentalDate = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult Get(RentalsDto rentalsDto) { return(Ok()); }