public IHttpActionResult CreateNewRental(NewRentalDto newRentalDto) { var customer = _context.Customers.Single(c => c.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 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 CreateCustomer(CustomerDto customerDto) { if (!ModelState.IsValid) { return(BadRequest()); } var customer = Mapper.Map <CustomerDto, Customer>(customerDto); _context.Customers.Add(customer); _context.SaveChanges(); customerDto.Id = customer.Id; return(Created(new Uri(Request.RequestUri + "/" + customer.Id), customerDto)); }
public ActionResult Save(Movie movie) { if (!ModelState.IsValid) { var viewModel = new MovieFormViewModel { Movie = movie, Genre = _context.Genres.ToList() }; } if (movie.Id == 0) { movie.DateAdded = DateTime.Now; _context.Movies.Add(movie); } else { var MovieinDb = _context.Movies.Single(c => c.Id == movie.Id); MovieinDb.Name = movie.Name; MovieinDb.ReleaseDate = movie.ReleaseDate; MovieinDb.GenreId = movie.GenreId; MovieinDb.Stock = movie.Stock; } _context.SaveChanges(); return(RedirectToAction("Index", "Movies")); }
public ActionResult Save(Customer customer) { if (!ModelState.IsValid) { var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); } if (customer.Id == 0) { _context.Customers.Add(customer); } else { var customerinDb = _context.Customers.Single(c => c.Id == customer.Id); customerinDb.Name = customer.Name; customerinDb.BirthDate = customer.BirthDate; customerinDb.MembershipTypeId = customer.MembershipTypeId; customerinDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; } _context.SaveChanges(); return(RedirectToAction("Index", "Customer")); }
public IHttpActionResult CreateMovie(MovieDto movieDto) { if (!ModelState.IsValid) { return(BadRequest()); } var movie = Mapper.Map <MovieDto, Movie>(movieDto); _context.Movies.Add(movie); _context.SaveChanges(); movieDto.Id = movie.Id; return(Created(new Uri(Request.RequestUri + "/" + movie.Id), movieDto)); }