/// <summary> /// dispatch the dvd /// </summary> /// <param name="dvdRequest"></param> /// <param name="dvds"></param> /// <returns></returns> private static bool dispatch(Rental dvdRequest, DVD dvd) { try { // add dvd to rental var therental = db.Rentals.First(r => r.RentalID == dvdRequest.RentalID); var theDVD = db.DVDs.First(d => d.DvdID == dvd.DvdID); therental.DateSent = DateTime.Now; therental.DateDue = DateTime.Now.AddDays(14); therental.QueuePosition = null; therental.Dvd = dvd; therental.DvdID = dvd.DvdID; db.SaveChanges(); theDVD.Rentals.Add(therental); theDVD.State = "rented"; // dispatch db.SaveChanges(); } catch (Exception) { return false; } return true; }
/// <summary> /// checks if dvd is available, then dispatches it and sends user a message /// </summary> /// <param name="dvdRequest">the rental wanting a dvd</param> /// <returns>true if dispatched, false if not</returns> public static bool RequestDVD(Rental dvdRequest) { var dvds = db.DVDs.Where(d => d.MovieID == dvdRequest.MovieID && d.State.Equals("available")); if (dvds.Any()) { return dispatch(dvdRequest, dvds.First()) && msgSend(dvdRequest.User, dvdRequest.Movie); } // no DVDs available return false; }
/// <summary> /// handles a returned dvd and realocates it /// </summary> /// <param name="dvdReturn"></param> /// <returns></returns> public static bool ReturnDVD(Rental dvdReturn) { try { // "return" dvd var toReturn = db.Rentals.First(r => r.RentalID == dvdReturn.RentalID); toReturn.DateReturned = DateTime.Now; toReturn.Dvd.State = "available"; db.SaveChanges(); } catch (Exception) { return false; } // hand over to newt user return realocateDVD(dvdReturn.Dvd); }
/// <summary> /// the code which created a new queue item /// </summary> /// <param name="id"></param> private void rentMovie(int id) { try { var movie = db.Movies.First(m => m.MovieID == id); var user = db.Users.First(u => u.Username == User.Identity.Name); var position = 0; if (user.Queue.Any()) { if (user.Queue.Any(r => r.QueuePosition != null)) { position = user.Queue.Count(r => r.QueuePosition != null) + 1; } } var newQueueItem = new Rental { DateDue = null, DateOfRental = DateTime.Now, DateReturned = null, DateSent = null, Movie = movie, MovieID = movie.MovieID, User = user, UserID = user.UserID, QueuePosition = position }; db.Rentals.Add(newQueueItem); db.SaveChanges(); user.Queue.Add(newQueueItem); movie.Rentals.Add(newQueueItem); db.SaveChanges(); // try getting a dvd Dispatch.RequestDVD(newQueueItem); } catch (Exception) { throw new AddToQueueFailedException(); } }