public async Task <IActionResult> Add([FromBody] CartDetailViewModel model)
        {
            try
            {
                await _cartDetailService.CreateAsync(model);

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public async Task <ActionResult> Update([FromBody] CartDetailViewModel model)
        {
            try
            {
                var model_db = await _cartDetailService.FindByIdAsync(model.Id);

                if (model_db == null)
                {
                    return(BadRequest("Model is not exists"));
                }
                model_db = model;
                await _cartDetailService.UpdateAsync(model_db, model_db.Id);

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
 public CartDetailPage(ObservableCollection <CartModel> products)
 {
     InitializeComponent();
     BindingContext = new CartDetailViewModel(products);
 }
 public CartDetailPage()
 {
     InitializeComponent();
     BindingContext = new CartDetailViewModel();
 }
示例#5
0
        public async Task <IActionResult> Cart()
        {
            User user;                                                                          // Current User
            Cart cart;                                                                          // Cart of User
            List <CartDetail> cartDetails;                                                      // Cart detail list of cart
            int productDetailID;                                                                // ID of product detail
            int productID;                                                                      // ID of product
            //ProductDetailBasicViewModel productDetailBasicViewModel; // View model detail of product
            ProductDetail productDetail;                                                        // Model detail of product
            //ImageBasicViewModel imageViewModel; // View model image of product
            string                     imageURL;                                                // Model image of product
            Product                    product;                                                 // Get product in cart
            int                        priceID;                                                 // Get price ID of product detail
            int                        sizeID;                                                  // Get size ID of product detail
            int                        colorID;                                                 // Get color ID of product detail
            List <ProductType>         productTypes;                                            // Get size and color of product detail
            CartDetailViewModel        cartDetailViewModel;                                     // Cart detail view model
            List <CartDetailViewModel> cartDetailViewModels = new List <CartDetailViewModel>(); // List cart detail view model
            CartViewModel              model  = new CartViewModel();                            // View model
            int                        result = 0;                                              // Check add cart is success

            // Get current user
            user = await userManager.GetUserAsync(User);

            //if (user == null)
            //{
            //    return RedirectToAction("SignIn", "Login", new { returnUrl = Url.Action("Cart", "Home") });
            //}

            // Get cart of current user
            cart = cartService.Get(user.Id);
            if (cart == null)
            {
                // Add new cart
                cart = new Cart {
                    UserId = user.Id, State = 1
                };
                result = cartService.Create(cart);
                if (result <= 0)
                {
                    return(Json(new
                    {
                        isSuccess = false,
                        message = "cart-error"
                    }));
                }
            }

            // Get product of cart
            cartDetails = cartDetailService.GetAll(cart.Id);

            foreach (var item in cartDetails) // Mapping form model to view model
            {
                // Get detail of product
                productDetailID = item.ProductDetailId.GetValueOrDefault();
                productDetail   = productDetailService.GetOne(productDetailID);

                // Get price of product detail
                priceID = productDetail.Price.GetValueOrDefault();
                productDetail.PriceNavigation = priceService.GetOne(priceID);

                // Get size and color of product detai
                sizeID       = productDetail.Size.GetValueOrDefault();
                colorID      = productDetail.Color.GetValueOrDefault();
                productTypes = productTypeService.GetSizeColor(sizeID, colorID);
                productDetail.SizeNavigation  = productTypes[0];
                productDetail.ColorNavigation = productTypes[1];

                // Get image of product
                productID = productDetail.ProductId.GetValueOrDefault();
                imageURL  = imageService.Get(productID).Url.ToString();
                product   = productService.Get(productID);

                cartDetailViewModel = new CartDetailViewModel(product, productDetail,
                                                              imageURL, item.Id, 0);

                cartDetailViewModels.Add(cartDetailViewModel);
            }
            model.Cart = cartDetailViewModels;

            return(View(model));
        }