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("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(NewRentalDTO newRentalDTO) { var Customer = _context.Customers.Single(m => m.Id == newRentalDTO.CustomerId); var movies = _context.Movies.Where(m => newRentalDTO.MovieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest(movie.Name + "Is Not Available")); } movie.NumberAvailable--; var NewRental = new Rental { Movie = movie, Customer = Customer, DateRented = DateTime.Now, }; _context.Rentals.Add(NewRental); } _context.SaveChanges(); return(Ok()); }
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(); var userName = User.Identity.GetUserName(); foreach (Movie movie in movies) { if (movie.AvailableStock == 0) { return(BadRequest("One or more selected movie is out of stock")); } movie.AvailableStock--; var rental = new Rental() { Customer = customer, Movie = movie, DateRented = DateTime.Now, RentedBy = userName, ReceivedBy = " " }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO newRental) { // if (newRental.MovieIds.Count == 0) // return BadRequest("No movie Id is given"); var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList(); // if (movies.Count != newRental.MovieIds.Count) // return BadRequest("One or more movies 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()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO rental) { if (rental == null) { return(BadRequest("Empty request")); } if (rental.MovieIds == null || !rental.MovieIds.Any()) { return(BadRequest($"No movies selected")); } var customer = customerDao.GetDetached(rental.CustomerId); if (customer == null) { return(BadRequest($"Invalid CustomerId: {rental.CustomerId}")); } var movieIds = rental.MovieIds.Distinct(); var movies = moviesDao.Get().Where(m => movieIds.Contains(m.Id)).ToList(); foreach (var movieId in movieIds) { var dbMovie = movies.SingleOrDefault(m => m.Id == movieId); if (dbMovie == null) { return(BadRequest($"Invalid MovieId: {movieId}")); } if (dbMovie.NumberAvailable <= 0) { return(BadRequest($"Movie not available: '{dbMovie.Name}'")); } dbMovie.NumberAvailable--; } var dateRented = DateTime.Now; IList <Movies2Customers> rentals = movieIds.Select(m => new Movies2Customers { CustomerId = rental.CustomerId, DateRented = dateRented, MovieId = m }).ToList(); this._dao.AddRange(rentals); this._dao.SaveChanges(); return(Ok()); }
[HttpPost] //because im creating a resource można tego nie dawać i w nazwie akcji dać PostCustomer, ale nie zalecane przez Mosha public IHttpActionResult CreateNewRentals(NewRentalDTO newRentalDTO) { //if (newRentalDTO.MoviesIds.Count == 0) // return BadRequest("No movies ids have been given."); OPTIMISTIC APPROACH :) //var customer = _context.Customers.SingleOrDefault(c => c.Id == newRentalDTO.CustomerId); var customer = _context.Customers.Single(c => c.Id == newRentalDTO.CustomerId); //if (customer == null) // return BadRequest("CustomerId is not valid."); OPTIMISTIC APPROACH :) var movies = _context.Movies.Where(m => newRentalDTO.MovieIds.Contains(m.Id)).ToList(); //if (movies.Count != newRentalDTO.MoviesIds.Count) // return BadRequest("One or more movies was invalid."); OPTIMISTIC APPROACH :) foreach (var movie in movies) { if (movie.NumberAvailable == 0) return BadRequest("Movie is not available"); //we need to check that because a hacker can call this api and reduce the number available to negative which cant happen movie.NumberAvailable--; var newRental = new Rental(); newRental.Customer = customer; newRental.Movie = movie; newRental.DateRented = DateTime.Now; _context.Rental.Add(newRental); var movieInDb = movie; } _context.SaveChanges(); return Ok();//nie zwracam methody Created bo ona jest używana kiedy zwaracam 1 utworzony obiekt(resource). Tutaj mamy wiele utworzonych obiektów. }
public IHttpActionResult NewRental(NewRentalDTO rental) { if (rental.MovieIds.Count == 0) { return(BadRequest("No Movie Ids have been given.")); } var customer = _context.Customers.SingleOrDefault(c => c.Id == rental.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid")); } var movies = _context.Movies.Where( m => rental.MovieIds.Contains(m.Id)).ToList(); if (movies.Count != rental.MovieIds.Count) { return(BadRequest("One or more MovieIds are invalid.")); } if (customer.ActiveRentals + movies.Count > Customer.RentalLimit) { return(BadRequest("Customer cannot rent more than 3 movies at a time.")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; customer.ActiveRentals++; var newRental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(newRental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO newRental) { // if customer == null if (newRental.CustomerID == 0) { return(BadRequest("CustomerId is not valid")); } // if there's no movies in the DTO if (newRental.MovieIds == null) { return(BadRequest("MovieIds are missing or not valid ")); } foreach (int movieId in newRental.MovieIds) { // if one (or more) movie(s) is invalid if (movieId == 0 || db.Movies.Find(movieId) == null) { return(BadRequest("There is no movie with movieId " + movieId)); } // check if one or more movie(s) is unavailable if (db.Movies.Find(movieId).NumberAvailable <= 0) { return(BadRequest("Movie is not available")); } Rental rental = new Rental(); rental.Customer = db.Customers.Find(newRental.CustomerID); rental.Movie = db.Movies.Find(movieId); rental.DateRented = DateTime.Now; db.Rentals.Add(rental); db.SaveChanges(); // diminish the number of available copies by one db.Movies.Find(movieId).NumberAvailable = (byte)(db.Movies.Find(movieId) .NumberAvailable - 1); db.SaveChanges(); // somewhere 1 shoulbd be added to NumberAvailable when the movie is returned } return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO newRental) { if (newRental.GameIds.Count == 0) //Placed this first since if we dont have any games no reseason for the extra customer queries { return(BadRequest("No Game Ids have been given")); } var customer = _context.Customers.SingleOrDefault( c => c.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid")); } var games = _context.Games.Where( //Some of these checks are a form of defensive programmig and may not be needed. Leaving them in for maintenance practice m => newRental.GameIds.Contains(m.Id)).ToList(); if (games.Count != newRental.GameIds.Count) { return(BadRequest("One or more GameIds are invalid")); } foreach (var game in games) { if (game.NumberAvailable == 0) { return(BadRequest("Game is not available")); } game.NumberAvailable--; var rental = new Rental { Customer = customer, Game = game, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRental(NewRentalDTO newRental) { if (newRental.MovieIDs.Count == 0) { return(BadRequest("No MovieIds")); } var customer = _dbContext.Customers.FirstOrDefault(c => c.ID == newRental.CustomerID); if (customer == null) { return(BadRequest("Invalid CustomerID")); } var movies = _dbContext.Movies.Where(x => newRental.MovieIDs.Contains(x.Id)).ToList(); // select * from movies where Id in (list of IDs in newRental) if (movies.Count != newRental.MovieIDs.Count) { return(BadRequest("One or More MovieIds are invalid")); } foreach (var movie in movies) { //movie not avDefault1ailaible if (movie.NumberAvailable == 0) { return(BadRequest("Movie not available")); } var rentalModel = new Rental { Customer = customer, CustomerID = newRental.CustomerID, Movie = movie, //MovieID = movie.Id, DateRented = DateTime.Now }; movie.NumberAvailable = movie.NumberAvailable - 1; _dbContext.Rentals.Add(rentalModel); } _dbContext.SaveChanges(); 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( c => c.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("Invalid Customer ID.")); } var movies = _context.Movies.Where( m => newRental.MovieIds.Contains(m.Id)).ToList(); if (movies.Count != newRental.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 rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult NewRental(NewRentalDTO newRental) { var customer = _context.Customers.SingleOrDefault(c => newRental.CustomerID == c.Id); if (customer == null) { return(BadRequest()); } if (newRental.MovieIds.Count == 0) { return(BadRequest()); } var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList(); if (movies.Count != newRental.MovieIds.Count) { return(BadRequest()); } 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 CreateNewRentals(NewRentalDTO newRental) { if (newRental.GameIds.Count == 0) { return(BadRequest("No Game Ids has been given")); } var customer = _context.Customers.SingleOrDefault(c => c.Id == newRental.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid")); } var games = _context.Games.Where(g => newRental.GameIds.Contains(g.Id)).ToList(); if (games.Count != newRental.GameIds.Count) { return(BadRequest("One or more GameIds are invalid.")); } foreach (var game in games) { if (game.NumberAvailable == 0) { return(BadRequest("Game is not available.")); } game.NumberAvailable--; var rental = new Rental { Customer = customer, Game = game, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult CreateNewRentals(NewRentalDTO newRentalDTO) { throw new NotImplementedException(); }