Ejemplo n.º 1
0
        public async Task <IActionResult> clearCart()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var cartVm = await _cartApiClient.GetCartByUser(userId);

            var orderVm = await _orderApiClient.GetOrderByUser(userId);

            var     lstProduct  = new List <OrderDetailVm>();
            OrderVm od          = new();
            int     y           = orderVm.Count();
            var     lstCartItem = cartVm.cartItemVms.ToList();

            if (lstCartItem.Count > 0)
            {
                foreach (var x in lstCartItem)
                {
                    var pVm = new OrderDetailVm()
                    {
                    };
                    pVm.ProductId = x.productVm.Id;
                    pVm.Quantity  = x.Quantity;
                    pVm.UnitPrice = x.productVm.Price;
                    pVm.OrderId   = y + 1;

                    lstProduct.Add(pVm);
                }
                ;
            }
            await _cartApiClient.clearCart(userId);

            await _orderApiClient.CreateOrder(userId, lstProduct);

            return(RedirectToAction("Index", "Order"));
        }
        public IActionResult Checkout(CheckoutViewModel request)
        {
            var model        = GetCheckoutViewModel();
            var orderDetails = new List <OrderDetailVm>();

            foreach (var item in model.CartItems)
            {
                orderDetails.Add(new OrderDetailVm()
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                });
            }
            var checkoutRequest = new CheckoutRequest()
            {
                Address      = request.CheckoutModel.Address,
                Name         = request.CheckoutModel.Name,
                Email        = request.CheckoutModel.Email,
                PhoneNumber  = request.CheckoutModel.PhoneNumber,
                OrderDetails = orderDetails,
                UserName     = request.CheckoutModel.UserName
            };

            //TODO: Add to API
            _orderApiClient.CreateOrder(checkoutRequest);


            TempData["SuccessMsg"] = "Order puschased successful";
            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Checkout(CheckoutViewModel checkOutViewModel)
        {
            var checkoutViewModel = GetCheckoutViewModel();
            var orderDetails      = new List <OrderDetailViewModel>();

            foreach (var item in checkoutViewModel.CartItems)
            {
                orderDetails.Add(new OrderDetailViewModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                });
            }

            var checkoutRequest = new CheckoutRequest
            {
                Name         = checkOutViewModel.CheckoutRequest.Name,
                Address      = checkOutViewModel.CheckoutRequest.Address,
                Email        = checkOutViewModel.CheckoutRequest.Email,
                Phone        = checkOutViewModel.CheckoutRequest.Phone,
                OrderDetails = orderDetails
            };
            var order = await _orderApiClient.CreateOrder(checkoutRequest);

            if (order)
            {
                TempData["Success"] = "Order purchase successfully";
                HttpContext.Session.Remove(SystemConstant.OrderSettings.CART_SESSION);
            }

            return(View(checkoutViewModel));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Checkout(CheckoutViewModel request)
        {
            string userId = "";
            var    model  = GetCheckOutViewModel();

            request.CartItems = model.CartItems;

            var orderDetails = new List <OrderDetailViewModel>();

            var checkoutRequest = new CheckoutRequest()
            {
                Name                 = request.CheckoutRequest.Name,
                Address              = request.CheckoutRequest.Address,
                Email                = request.CheckoutRequest.Email,
                PhoneNumber          = request.CheckoutRequest.PhoneNumber,
                OrderDetailViewModel = orderDetails
            };

            if (User.Identity.IsAuthenticated)
            {
                userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                checkoutRequest.UserId = Guid.Parse(userId);
            }
            else
            {
                TempData["LoginRequired"] = "Please login before check out!";

                return(View(request));
            }

            foreach (var item in model.CartItems)
            {
                orderDetails.Add(new OrderDetailViewModel()
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                    Price     = item.Price
                });
            }


            //TODO: send request to API
            var result = await _orderApiClient.CreateOrder(checkoutRequest);

            if (result)
            {
                TempData["Success"] = "Order purchase successful!";
            }

            return(View(request));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create(OrderCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var result = await _orderApiClient.CreateOrder(request);

            if (result == 1)
            {
                TempData["result"] = "Thêm mới đơn hàng thành công";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Thêm đơn hàng thất bại");
            return(View(request));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Checkout(CheckoutViewModel request)
        {
            var model        = GetCheckoutViewModel();
            var orderDetails = new List <OrderDetailViewModel>();

            foreach (var item in model.CartItems)
            {
                orderDetails.Add(new OrderDetailViewModel()
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                    Price     = item.Price
                });
            }
            var checkoutRequest = new CheckoutRequest()
            {
                Address      = request.CheckoutModel.Address,
                Name         = request.CheckoutModel.Name,
                PhoneNumber  = request.CheckoutModel.PhoneNumber,
                Email        = request.CheckoutModel.Email,
                OrderDetails = orderDetails
            };



            if (!ModelState.IsValid)
            {
                return(View(request));
            }

            var result = await _orderApiClient.CreateOrder(checkoutRequest);

            if (result)
            {
                TempData["result"] = "Đặt hàng thành công!";
                return(View(GetCheckoutViewModel()));
            }

            TempData["result"] = "Đặt hàng thất bại";
            //return View(request);


            return(View(GetCheckoutViewModel()));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Payment(OrderCreateRequest request)
        {
            List <CategoryViewModel> categoryList = await _categoryApiClient.GetList();

            ViewBag.CategoryList = categoryList;

            cart = SessionExtensions.Get <List <CartItems> >(HttpContext.Session, "cart");
            var list = new List <CartItems>();

            if (cart != null)
            {
                list = cart;
            }
            ViewBag.Cart = list;

            decimal total = 0;

            foreach (var item in list)
            {
                total += (item.Price * item.Quantity);
            }
            ViewBag.total = total;

            if (!ModelState.IsValid)
            {
                return(View(request));
            }

            var id = await _orderApiClient.CreateOrder(request);

            cart = SessionExtensions.Get <List <CartItems> >(HttpContext.Session, "cart");
            var orderDetail = new OrderDetailsCreateRequest();

            foreach (var item in cart)
            {
                orderDetail.OrderId   = id;
                orderDetail.ProductId = item.Id;
                orderDetail.Price     = item.Price;
                orderDetail.Quantity  = item.Quantity;
                await _orderApiClient.CreateOrderDetail(orderDetail);
            }
            return(RedirectToAction("Success"));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Checkout(CheckoutViewModel request)
        {
            var model = GetCheckoutViewModel();

            var username = User.Identity.Name;

            if (username == request.CheckoutModel.ShipName)
            {
                request.CheckoutModel.ShipName = username;
            }
            var orderDetails = new List <OrderDetailViewModel>();

            foreach (var item in model.CartItems)
            {
                orderDetails.Add(new OrderDetailViewModel()
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                    Price     = item.Price
                });
            }
            var checkoutRequest = new OrderCreateRequest()
            {
                ShipAddress     = request.CheckoutModel.ShipAddress,
                ShipName        = request.CheckoutModel.ShipName,
                ShipEmail       = request.CheckoutModel.ShipEmail,
                ShipPhoneNumber = request.CheckoutModel.ShipPhoneNumber,
                OrderDetails    = orderDetails,
                Status          = request.CheckoutModel.Status,
                UserId          = request.CheckoutModel.UserId,
                OrderDate       = DateTime.Now
            };

            //TODO: Add to API

            await _orderApiClient.CreateOrder(checkoutRequest);

            _notyf.Information("Đơn đặt hàng thành công");
            TempData["SuccessMsg"] = "Đơn đặt hàng thành công";
            return(View(model));
        }