public async Task <IActionResult> Index() { try { CouponCode cp = null; if (!string.IsNullOrEmpty(cart.Coupon?.Code)) // check if cart contains coupon code { cp = await couponDAL.FindCouponByCodeAsync(cart.Coupon.Code); if (cp == null) // if coupon code is not found or outdated { ModelState.AddModelError("", "Coupon Code " + cart.Coupon.Code.ToUpper() + " is not existed or out of date"); cart.RemoveCoupon(); // remove coupon code from cart } else { cart.Coupon = cp; } } if (cart.Lines.Count() > 0) { foreach (CartLine cartLine in cart.Lines) { Tour tour = await tourDAL.FindTourByTourIdAsync(cartLine.Tour.Id); //find tour which is active if (tour == null || tour.FromDate < DateTime.Now) { ModelState.AddModelError("", "Tour " + cartLine.Tour.Id.ToUpper() + " is not existed or available"); } else { cartLine.Tour = tour; cartLine.Tour.TakenSlot = await tourDAL.GetTakenSlotByTourIdAsync(cartLine.Tour.Id); if ((cartLine.AdultTicket + cartLine.KidTicket) > (cartLine.Tour.MaxGuest - cartLine.Tour.TakenSlot)) { ModelState.AddModelError("", "Not enough tickets of tour " + cartLine.Tour.Id.ToUpper()); } } } } CartViewModel model = new CartViewModel { Cart = cart }; return(View(model)); } catch (Exception ex) { logger.Error(ex.Message); throw; } }
public async Task <IActionResult> Details(string id) { try { if (string.IsNullOrWhiteSpace(id)) { return(NotFound()); } Tour tour = await tourDAL.FindTourByTourIdEmpAsync(id); if (tour == null) { return(NotFound()); } tour.Destinations = await tourDAL.FindDestinationsByTourIdAsync(id); tour.Guides = await tourDAL.FindGuidesByTourIdAsync(id); tour.TakenSlot = await tourDAL.GetTakenSlotByTourIdAsync(id); return(View(tour)); } catch (Exception ex) { logger.Error(ex.Message); throw; } }
public async Task <IActionResult> Details(string id) { try { Tour tour = await tourDAL.FindTourByTourIdAsync(id); if (tour == null) { return(NotFound()); } else { tour.Destinations = await tourDAL.FindDestinationsByTourIdAsync(id); tour.TakenSlot = await tourDAL.GetTakenSlotByTourIdAsync(id); } CartLine model = new CartLine { Tour = tour }; return(View(model)); } catch (Exception ex) { logger.Error(ex.Message); throw; } }
public async Task <IActionResult> Checkout() { try { if (cart.Lines.Count() <= 0) { ModelState.AddModelError("", "There is no items in cart to check out"); } else { foreach (CartLine cartLine in cart.Lines) //load and check tours in cart is valid { Tour tour = await tourDAL.FindTourByTourIdAsync(cartLine.Tour.Id); //find tour which is active if (tour == null || tour.FromDate < DateTime.Now) { ModelState.AddModelError("", "Tour " + cartLine.Tour.Id.ToUpper() + " is not existed or available"); } else { cartLine.Tour = tour; cartLine.Tour.TakenSlot = await tourDAL.GetTakenSlotByTourIdAsync(cartLine.Tour.Id); if ((cartLine.AdultTicket + cartLine.KidTicket) > (cartLine.Tour.MaxGuest - cartLine.Tour.TakenSlot)) { ModelState.AddModelError("", "Not enough tickets of tour " + cartLine.Tour.Id.ToUpper()); } } } //end load tour AppUser customer = await userManager.FindByNameAsync(User.Identity.Name); //get user infomation if (!string.IsNullOrEmpty(cart.Coupon.Code)) { CouponCode cp = await couponDAL.FindCouponByCodeAsync(cart.Coupon.Code); if (cp == null) { ModelState.AddModelError("", "Coupon Code " + cart.Coupon.Code.ToUpper() + " is not existed or out of date"); cart.RemoveCoupon(); // remove coupon code from cart } else { cart.Coupon = cp; } } if (ModelState.IsValid) { DateTime now = DateTime.Now; Order order = new Order { Id = User.Identity.Name.ToUpper() + now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00") + now.Millisecond.ToString("000"), Cart = cart, Customer = customer }; return(View(order)); //if everything is ok then dispay view for user to confirm payment and place order } } return(RedirectToAction("Index", "Cart")); //if model state has error, return to cart view to display error } catch (Exception ex) { logger.Error(ex.Message); throw; } }