Ejemplo n.º 1
0
        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;
            }
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            try
            {
                IEnumerable <Tour> popularTours = await tourDAL.GetTrendingToursAsync();

                if (popularTours != null)
                {
                    foreach (Tour tour in popularTours)
                    {
                        tour.Destinations = await tourDAL.FindDestinationsByTourIdAsync(tour.Id);
                    }
                }
                AppUser user = User?.Identity?.Name == null ? null : await userManager.FindByNameAsync(User.Identity.Name);

                string avatar = "https://ztourist.blob.core.windows.net/others/avatar.png";
                if (user != null)
                {
                    avatar = user.Avatar;
                }

                if (cart != null && cart.Lines.Count() > 0)
                {
                    foreach (CartLine cartLine in cart.Lines)
                    {
                        cartLine.Tour = await tourDAL.FindTourByTourIdAsync(cartLine.Tour.Id);
                    }
                }
                NavigationViewModel model = new NavigationViewModel
                {
                    Cart         = cart,
                    PopularTours = popularTours,
                    Avatar       = avatar
                };
                return(View(model));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                throw;
            }
        }
Ejemplo n.º 4
0
        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;
            }
        }