public ActionResult Purchase(PurchaseTicketViewModel ticket) { try { // TODO: Add insert logic here var currentUserId = User.Identity.GetUserId(); var bus = context.BusVehicles .Where(b => b.Id == ticket.Id) .SingleOrDefault(); if (bus == null) { return(new HttpStatusCodeResult(404)); } var viewModel = new AvailableBusViewModel() { Id = bus.Id, Capacity = bus.Capacity, Class = bus.Class, Route = bus.Route, Fare = bus.Fare, DepartureTime = bus.DepartureTime, }; if (bus.DepartureTime < DateTime.Now) { ViewBag.Error = "This bus has already departed."; return(View(viewModel)); } else if (bus.DepartureTime > DateTime.Now.AddDays(AppConstants.MAX_DAYS_BEFORE_RESERVATION)) { ViewBag.Error = "You can only purchase tickets as late as two weeks before departure."; return(View(viewModel)); } Reservation reservation = new Reservation() { TotalSeat = ticket.PassengersCount, PurchasedOn = DateTime.Now, PassengerID = context.Passengers .Where(u => u.UserID == currentUserId) .Select(p => p.Id) .SingleOrDefault(), BusID = bus.Id, TotalAmount = ticket.PassengersCount * bus.Fare, IsConfirmed = false }; context.Reservations.InsertOnSubmit(reservation); context.SubmitChanges(); return(RedirectToAction("List")); } catch { return(View()); } }
public ActionResult Purchase(int id) { var bus = context.BusVehicles .Where(b => b.Id == id) .SingleOrDefault(); var viewModel = new AvailableBusViewModel() { Id = bus.Id, Capacity = bus.Capacity, Class = bus.Class, Route = bus.Route, Fare = bus.Fare, DepartureTime = bus.DepartureTime, }; return(View(viewModel)); }