public IHttpActionResult CreateNewRentals(NewRentalDto rentalDto) { //if (rentalDto.MovieIds.Count == 0) // return BadRequest("No Movie Ids have been given."); var customer = _context.Customers.SingleOrDefault(c => c.Id == rentalDto.CustomerId); //if (customer == null) // return BadRequest("CustomerId is not valid."); var movies = _context.Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); //if (movies.Count == rentalDto.MovieIds.Count) // return BadRequest("One or more MovieId is invalid."); foreach (var movie in movies) { if (movie.AailableNumber == 0) { return(BadRequest("Movie is not available.")); } movie.AailableNumber--; _context.Rentals.Add(new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }); _context.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("Customer Id 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 MoviesIds are invalid"); foreach (var movie in movies) { if (movie.NumberAvailable == 0) return BadRequest("Movie is not available"); var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; movie.NumberAvailable--; _context.Rentals.Add(rental); } _context.SaveChanges(); return Ok(); }
public IHttpActionResult CreateNewRentals(NewRentalDto newRental) { if (!ModelState.IsValid) { BadRequest(); } var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList(); //if (newRental.MovieIds.Count != movies.Count) // return BadRequest("Movie Not Available"); foreach (var movie in movies) { movie.NumberAvailable--; if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available")); } //avoiding mapping var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now, DateReturned = newRental.DateReturned }; _context.Rentals.Add(rental); } _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.MoviesIds.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 newRental) { //defensive coding to project. but is poluting the visibility of code //can use optimistic case, and suppress unnecessary edge case handling, but will be vulnerable to hackers //if (newRental.MovieIds.Count == 0) // return BadRequest("No movie ids have ben given."); //var customer = _context.Customers.SingleOrDefault(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 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(); //Optimistic coding from source: 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 RegisterMovies(NewRentalDto newRentalDto) { if (!ModelState.IsValid) { return(BadRequest()); } //I need to extract the Customer ID from Dto if (newRentalDto.MovieIds.Count == 0) { return(BadRequest("No Movie Ids have been given")); } var customer = _myContext.Customers.SingleOrDefault(c => c.Id == newRentalDto.CustomerID); if (customer == null) { return(BadRequest("Customer is not valid")); } //Now that we have our customer id we need to extract the movies ids from the list but only the one that exist in our movies var movies = _myContext.Movies.Where(m => newRentalDto.MovieIds.Contains(m.Id)).ToList(); //Check if the movie IDs passsed from DTO all existed in DB if (movies.Count != newRentalDto.MovieIds.Count) { BadRequest("One or more MovieIds are invalid"); } //Spin thru all the movies foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available" + movie.Id.ToString().Trim())); } //For each movie rented we decrement one movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, //Customer = new Customer //{ // Id = 14 //}, //Movie = new Movie //{ // Id = 3 //}, DateRented = DateTime.Now }; _myContext.Rentals.Add(rental); } _myContext.SaveChanges(); return(Ok()); }
[HttpPost] //aplies with restful conventions public IHttpActionResult NewRentals(NewRentalDto newRental) { if (!ModelState.IsValid) { return(BadRequest()); //helper method from IHttpActionresult } try { if (newRental.MovieId.Count == 0) { return(BadRequest("No Movie Ids have been given")); } var customer = _context.Customers.SingleOrDefault(c => newRental.CustomerId == c.Id); if (customer == null) { return(BadRequest("CustomerId is not valid")); } var movies = _context.Movies.Where(m => newRental.MovieId.Contains(m.Id)).ToList(); if (movies.Count != newRental.MovieId.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 Rentals { Movies = movie, Customer = customer, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); //return Created(new Uri(Request.RequestUri + "/" + rentals.Id), newRental);//URI unified resource identifier = /api/customers/10 //When you use created you provide the URL to the newly created resource. Since we are now creating multiple new records we //dont use created return(Ok()); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } }
public IHttpActionResult CreateNewRentals(NewRentalDto newRental) { //What Edge case we have to face in rental api? //Customer ID is inValid //no MovieID //one or more movie are Invalid //one or more movie Not available //Definsive VS Optimistic //****************Definsive Approch //helpfull when you use public api used by differant application and different teams //for enternal use I prefer optimistic less condiftion less validation //if (newRental.MovieIds.Count == 0) // return BadRequest("no MovieID have been given"); //we move up because no movie so nesseccary quary customer //var customer = _context.Customers.SingleOrDefault( // c => c.Id == newRental.CustomerId); //if (customer == null) // return BadRequest("Invalid Cutomer ID"); //var movies = _context.Movies.Where( // m => newRental.MovieIds.Contains(m.Id)).ToList(); //same select * from movie where id in (1,2,3); //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("one or more movie Not available"); // movie.NumberAvailable--; // var rental = new Rental // { // Customer = customer, // Movie = movie, // DateRented = DateTime.Now // }; // _context.Rentals.Add(rental); //} //************** //****************Optimistic Approch var customer = _context.Customers.Single( c => c.Id == newRental.CustomerId); var movies = _context.Movies.Where( m => newRental.MovieIds.Contains(m.Id)).ToList(); //same select * from movie where id in (1,2,3); foreach (var movie in movies) { if (movie.NumberAvailable == 0) //it could be lead by hacker to minus { return(BadRequest("one or more 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(NewRentalDto rentalDto) { if (rentalDto == null || rentalDto.MovieIds.Count == 0) { return(BadRequest("No movies given")); } var customer = _context.Customers.SingleOrDefault(c => c.Id == rentalDto.CustomerId); if (customer == null) { return(BadRequest("CustomerId is not valid")); } var rentals = _context.Rentals .Where(r => r.Customer.Id == rentalDto.CustomerId && r.DateReturned == null).ToList(); var count = rentals.Count(); if (count == 5) { return(BadRequest("You have reached the maximum of 5 rentals.")); } var movies = _context.Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList(); if (movies.Count != rentalDto.MovieIds.Count) { return(BadRequest("Not all movies found")); } foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest(movie.Name + " is not available")); } var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; if (count < 5) { _context.Rentals.Add(rental); } else { _context.SaveChanges(); return(BadRequest("You have reached the maximum of 5 rentals.")); } count += 1; movie.NumberAvailable -= 1; } _context.SaveChanges(); return(Ok()); }
public IHttpActionResult New(NewRentalDto newRentalDto) { return(Json("")); }
public ICollection <NewRentalDto> CreateNewRentals(NewRentalDto newRental) { var rental = new List <NewRentalDto>(); return(rental); }