private bool isValidCustom(MovieRentModel movieRent) { if (movieRent.movie == null) { ModelState.AddModelError("", "You should select a movie ..."); return(false); } if (movieRent.RentStartDateTime > DateTime.Today) { ModelState.AddModelError("", "Rent date should not be greater than today"); return(false); } if (movieRent.RentStartDateTime > movieRent.ReturnDateTime) { ModelState.AddModelError("", "Rent date should not be greater than return date"); return(false); } if (movieRent.ReturnDateTime > movieRent.RentStartDateTime.AddDays(movieRent.movie.MaxRentDays)) { ModelState.AddModelError("", "You cannot rent the movie more than " + movieRent.movie.MaxRentDays + " days . ."); return(false); } var rented = db.MovieRents.Where(x => x.MovieId == movieRent.MovieId && !x.IsReturned); if (movieRent.movie.NumberOfCopies - rented.Count() <= 0) { ModelState.AddModelError("", "All copies of this movie is rented out ..."); return(false); } return(true); }
// // GET: /MovieRents/Create public ActionResult Create() { MovieRentModel movieRent = new MovieRentModel(); movieRent.RentStartDateTime = DateTime.Today; movieRent.ReturnDateTime = DateTime.Today.AddDays(1); ViewData["Customers"] = new SelectList(db.Customers.OrderBy(s => s.Name), "id", "Name"); ViewData["Movies"] = new SelectList(db.Movies.OrderBy(s => s.Title), "id", "Title"); return(View(movieRent)); }
// // GET: /MovieRents/Details/5 public ActionResult Details(int id) { MovieRentModel movieRent = db.MovieRents.Single(x => x.Id == id); if (movieRent == null) { return(HttpNotFound()); } return(View(movieRent)); }
// // GET: /MovieRents/Edit/5 public ActionResult Edit(int?id) { MovieRentModel movieRent = db.MovieRents.Single(x => x.Id == id); if (movieRent == null) { return(HttpNotFound()); } ViewData["Customers"] = new SelectList(db.Customers.OrderBy(s => s.Name), "id", "Name"); ViewData["Movies"] = new SelectList(db.Movies.OrderBy(s => s.Title), "id", "Title"); return(View(movieRent)); }
public ActionResult Delete(int id, MovieRentModel movieRent) { try { MovieRentModel movrnt = db.MovieRents.Single(x => x.Id == id); db.MovieRents.Remove(movrnt); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit(MovieRentModel movieRent) { try { db.Entry(movieRent).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Create(MovieRentModel movieRent) { try { movieRent.movie = db.Movies.SingleOrDefault(x => x.Id == movieRent.MovieId); if (!isValidCustom(movieRent)) { ViewData["Customers"] = new SelectList(db.Customers.OrderBy(s => s.Name), "id", "Name"); ViewData["Movies"] = new SelectList(db.Movies.OrderBy(s => s.Title), "id", "Title"); return(View(movieRent)); } db.MovieRents.Add(movieRent); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult CreateByCustomerId(MovieRentModel movieRent) { return(Create(movieRent)); }