Ejemplo n.º 1
0
        public async Task <JsonResult> AddToCart(AddToCartDTO model)
        {
            IDataResult <CartDTO> cart = await _cartService.AddToCart(model);

            //todo cart IsSuccess Kontrolü Yap
            return(Json(new { totalPrice = cart.Data.CartTotal, CartCount = cart.Data.CartItemsCount }));
        }
Ejemplo n.º 2
0
        public static CartItemDTO NewCartItem(Product product, AddToCartDTO model)
        {
            decimal additionalPrices = 0;

            if (model.DemandTypes.Count != 0)
            {
                var productDemands = product.ProductDemands;
                foreach (var demandType in model.DemandTypes)
                {
                    additionalPrices += demandType.ChoosedDemandPrice;
                }
            }
            product.Price += additionalPrices;

            product.ProductDemands = null;
            CartItemDTO cartModel = new CartItemDTO
            {
                DemandTypes = model.DemandTypes,
                ProductId   = product.Id,
                Product     = product,
                Quantity    = model.Quantity,
                UnitPrice   = product.Price,
                Note        = model.Note,
                ImageName   = product.ProductImages.FirstOrDefault().ImageName
            };

            cartModel.ApplyDiscount(product.DiscountRate);
            return(cartModel);
        }
Ejemplo n.º 3
0
        public async Task <IDataResult <CartDTO> > AddToCart(AddToCartDTO model)
        {
            var key = _httpContextAccessor.HttpContext.Request.Cookies["key"]?.ToString() ?? Guid.NewGuid().ToString();

            string existCart = await _redisManager.GetDb().StringGetAsync(key);

            CartDTO cart;

            if (!string.IsNullOrEmpty(existCart))
            {
                cart = JsonSerializer.Deserialize <CartDTO>(existCart);
            }
            else
            {
                cart = new CartDTO();
            }

            //! Sepette indirim etkinliğini bu satırda kontrol et ve cart'ta ki indirime apply et

            var existCartItem = cart.CartItems.FirstOrDefault(x => x.ProductId == model.ProductId && x.DemandTypes.SequenceEqual(model.DemandTypes));

            if (existCartItem != null)
            {
                cart.CartItems.Remove(existCartItem);
                existCartItem.Quantity += model.Quantity;
                cart.CartItems.Add(existCartItem);
            }
            else
            {
                var product = (await _productService.GetByIdAsync(model.ProductId)).Data;
                product.ProductDemands = null;
                CartItemDTO cartModel = CartHelper.NewCartItem(product, model);
                cart.CartItems.Add(cartModel);
            }

            TimeSpan expireCart = TimeSpan.FromMinutes(1);
            var      status     = await _redisManager.GetDb().StringSetAsync(key, JsonSerializer.Serialize(cart), expireCart);

            if (!status)
            {
                cart = null;
            }
            else
            {
                _httpContextAccessor.HttpContext.Response.Cookies.Append("key", key, new CookieOptions
                {
                    Expires  = DateTime.Now + expireCart,
                    HttpOnly = true,
                    SameSite = SameSiteMode.Lax,
                    Secure   = true
                });
            }
            return(ResultHelper <CartDTO> .DataResultReturn(cart));
        }
Ejemplo n.º 4
0
        public async Task <DataRespone <string> > AddToCart(AddToCartDTO addToCartDTO)
        {
            if (addToCartDTO == null)
            {
                throw new MyBadRequestException("Your cart is null.");
            }

            DataResp result = await _cartService.addToCart(addToCartDTO.Bill, addToCartDTO.BillDetail);

            if (!result.Success)
            {
                throw new MyBadRequestException(result.Error);
            }

            return(new DataRespone <string> {
                Status = true, Data = "Congrat!Adding to cart is sucess", Errors = null
            });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds product to cart
        /// </summary>
        /// <returns></returns>
        public ActionResult AddToCart(ProductDetailsViewModel model)
        {
            try
            {
                var userEmail = CookieManager.GetEmailFromUserCookie();

                AddToCartDTO addToCartDto;

                //This would mean that the user has logged in from the AddToCart page (using login link on top right of page).
                //Therefore, the ProductViewModel parameter quantity value will be zero in this exceptional case.
                //Just create AddToCartViewModel object and return View passing in AddToCartViewModel object without modify cart of user logging in
                if (model.SelectedQuantity == 0)
                {
                    var userCartItems = services.CartService.GetUserCartItems(userEmail);

                    //create associated viewmodel and pass into view
                    addToCartDto                = new AddToCartDTO();
                    addToCartDto.ProductID      = model.ProductID;
                    addToCartDto.ProductName    = model.ProductName;
                    addToCartDto.CartItemsCount = userCartItems.Count();
                    addToCartDto.SubTotal       = userCartItems.Sum(ci => ci.Product.Price * ci.Quantity);
                    return(View(addToCartDto));
                }

                // Get the matching cart and product instances
                var cartItem = services.CartService.GetCartItem(userEmail, model.ProductID);

                if (cartItem == null)
                {
                    // Create a new cart item if no cart item exists
                    cartItem = new CartItem
                    {
                        ProductID   = model.ProductID,
                        Email       = userEmail,
                        Quantity    = model.SelectedQuantity,
                        DateCreated = DateTime.Now,
                    };

                    services.CartService.AddCartItem(cartItem);
                }
                else
                {
                    // If the item does exist in the cart,
                    // then increase cart item quantity by quantity parameter amount
                    cartItem.Quantity += model.SelectedQuantity;
                    services.CartService.UpdateCartItem(cartItem);
                }

                var cartItems = services.CartService.GetUserCartItems(userEmail, Constants.DATABASE_TABLE_PRODUCTS);

                //create associated viewmodel and pass into view
                addToCartDto                = new AddToCartDTO();
                addToCartDto.ProductID      = cartItem.ProductID;
                addToCartDto.ProductName    = cartItem.Product.Name;
                addToCartDto.CartItemsCount = cartItems.Count();
                addToCartDto.SubTotal       = cartItems.Sum(ci => ci.Product.Price * ci.Quantity);

                return(View(addToCartDto));
            }
            catch (Exception ex)
            {
                ExceptionManager.LogException(ex, Path.GetFileName(Request.PhysicalPath));
                return(RedirectToAction(Constants.CONTROLLER_ACTION_INDEX, Constants.CONTROLLER_ERROR));
            }
        }
Ejemplo n.º 6
0
 public async Task <Orders> AddToCart([FromBody] AddToCartDTO obj)
 {
     return(await _cartService.AddToCart(obj.prodID, obj.quantity, obj.price, obj.sessionID, obj.cstID));
 }