public IHttpActionResult CreateNewRentals(NewRentalViewModel newRental) { 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(); 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()); }
private bool ValidateBooking(Rental rental) { bool isValid = true; if (rental.From > rental.To) { ModelState.AddModelError("Booking.From", "Start date cannot be after end date"); var newRentalViewModel = new NewRentalViewModel() { Cars = DbContext.Cars, Rental = rental }; isValid = false; } List <Rental> rentalFromDb = DbContext.Rentals.Where(r => r.CarId == rental.CarId).ToList(); foreach (var currentRentals in rentalFromDb) { if (DateHelpers.HasSharedDateIntervals(rental.From, rental.To, currentRentals.From, currentRentals.To)) { ModelState.AddModelError("Booking.From", "Date already occupied."); var newRentalViewModel = new NewRentalViewModel() { Cars = DbContext.Cars, Rental = rental }; isValid = false; } } return(isValid); }
public IHttpActionResult NewRental(NewRentalViewModel newRenatl) { var customer = _context.Customers.Single(c => c.Id == newRenatl.CustomerId); var movies = _context.Movies.Where(m => newRenatl.MoviesIds.Contains(m.Id)); foreach (var movie in movies) { if (movie.NumberInStock == 0) { return(BadRequest("Movie is not available.")); } movie.NumberInStock--; var rental = new Rental { Customer = customer, Movie = movie, RentDate = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public void StartMovieRental(NewRentalViewModel rvm) { var config = new MapperConfiguration(cfg => { cfg.CreateMap <NewRentalViewModel, Rentals>(); cfg.IgnoreUnmapped(); }); IMapper mapper = config.CreateMapper(); Rentals r = mapper.Map <NewRentalViewModel, Rentals>(rvm); rr.StartMovieRental(r); }
public IActionResult Create() { var newRentalViewModel = new NewRentalViewModel() { Cars = DbContext.Cars }; return(View(newRentalViewModel)); }
public ActionResult New() { var movies = _context.Movies.ToList(); var customers = _context.Customers.ToList(); var viewModel = new NewRentalViewModel() { Movies = movies, Customers = customers }; return(View("New", viewModel)); }
public ActionResult New(int id) { var customer = _context.Customers.Include(c => c.Membership.MembershipType).SingleOrDefault(c => c.Id == id); if (customer.Membership.ExpiryDate < DateTime.Now) { customer.Membership.MembershipTypeId = MembershipType.PayAsYouGo; customer.SetMembershipDuration(customer); } var movies = _context.Movies.Include(g => g.MovieGenre).ToList(); var games = _context.Games.Include(g => g.GameGenre).Include(g => g.GamePlatform).ToList(); var viewModel = new NewRentalViewModel { Customer = customer, Movies = movies, Games = games }; _context.SaveChanges(); return(View(viewModel)); }