public ActionResult Rent(VideoRentViewModel model) { Video video = db.Videos.Include(v => v.Copies) .SingleOrDefault(v => v.ID == model.ID && v.Copies.Where(c => c.RentedDate == null).Count() > 0); if (video == null) { return(HttpNotFound()); } DateTime now = DateTime.UtcNow; VideoCopy copy = video.Copies.Where(c => c.RentedDate == null).First(); copy.RentedDate = now; copy.RenterID = UserID; copy.RentedDays = model.Days; db.Entry(copy).State = EntityState.Modified; UserHistory history = new UserHistory(); history.Copy = copy; history.ID = Guid.NewGuid(); history.PointsEarned = (video.Age == VideoAge.New ? 2 : 1); history.RentedDate = now; history.RentedDays = model.Days; history.UserID = UserID; db.UserHistory.Add(history); db.SaveChanges(); return(Json(true, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Rent a particual video, if available. /// </summary> /// <param name="model"></param> /// <returns></returns> public IHttpActionResult Rent(VideoRentViewModel model) { // Note that I would prefer to have a service deal with the renting // and other operations on videos or video copies because // this code is duplicated in the api service. I'm not doing that here // to avoid complexity. // // The call the could be service.Rent(model.ID, UserID, days); for both // uses. Video video = db.Videos.Include(v => v.Copies) .SingleOrDefault(v => v.ID == model.ID && v.Copies.Where(c => c.RentedDate == null).Count() > 0); if (video == null) { return(NotFound()); } DateTime now = DateTime.UtcNow; VideoCopy copy = video.Copies.Where(c => c.RentedDate == null).First(); copy.RentedDate = now; copy.RenterID = UserID; copy.RentedDays = model.Days; db.Entry(copy).State = EntityState.Modified; UserHistory history = new UserHistory(); history.Copy = copy; history.ID = Guid.NewGuid(); history.PointsEarned = (video.Age == VideoAge.New ? 2 : 1); history.RentedDate = now; history.RentedDays = model.Days; history.UserID = UserID; db.UserHistory.Add(history); db.SaveChanges(); return(Json(history)); }