示例#1
0
        public async Task <BasketCart> UpdateItem(string username, BasketCartItem item)
        {
            var redisCart = await _context.Database.StringGetAsync(username);

            if (redisCart.IsNullOrEmpty)
            {
                return(null);
            }
            try
            {
                var cart  = JsonConvert.DeserializeObject <BasketCart>(redisCart);
                int index = cart.Items.FindIndex(i => i.ProductId.Equals(item.ProductId));
                if (index == -1)
                {
                    return(null);
                }
                cart.Items[index] = item;
                if (await CreateOrUpdateCart(cart))
                {
                    return(cart);
                }
                return(null);
            }
            catch
            {
                return(null);
            }
        }
        public async Task <BasketCart> AddBasketItem(string username, BasketCartItem basketCartItem)
        {
            var basketFromRedis = await _context.Redis.StringGetAsync(username);

            if (basketFromRedis.IsNullOrEmpty)
            {
                await UpdateBasketItems(new BasketCart { Username = username });

                basketFromRedis = await _context.Redis.StringGetAsync(username);
            }

            try
            {
                var basket = JsonConvert.DeserializeObject <BasketCart>(basketFromRedis);
                basket.Items.Add(basketCartItem);
                if (await UpdateBasketItems(basket))
                {
                    return(basket);
                }
                return(null);
            }
            catch
            {
                return(null);
            }
        }
示例#3
0
        public async Task <BasketCart> AddItem(string username, BasketCartItem item)
        {
            var redisCart = await _context.Database.StringGetAsync(username);

            if (redisCart.IsNullOrEmpty)
            {
                await CreateOrUpdateCart(new BasketCart { Username = username });

                redisCart = await _context.Database.StringGetAsync(username);
            }
            try
            {
                var cart = JsonConvert.DeserializeObject <BasketCart>(redisCart);
                cart.Items.Add(item);
                if (await CreateOrUpdateCart(cart))
                {
                    return(cart);
                }
                return(null);
            }
            catch
            {
                return(null);
            }
        }
示例#4
0
 public async Task <IActionResult> Delete_from_Basket_item(string username, BasketCartItem item_to_delete)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(BadRequest());
     }
     return(Ok(await _repository.DeleteBasketItem(username, item_to_delete)));
 }
        public async Task <IActionResult> DeleteItemFromBasket(BasketCartItem item, string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(BadRequest("Couldn't find basket"));
            }
            if (await _repos.DeleteItem(username, item))
            {
                return(Ok("Deleted item from basket"));
            }

            return(StatusCode(500, "Failed to delete item from basket"));
        }
        public async Task <IActionResult> AddItemToBasket(BasketCartItem item, string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(BadRequest("Couldn't find basket"));
            }
            var basket = await _repos.AddItem(username, item);

            if (basket == null)
            {
                return(StatusCode(500, "Failed to add item to basket"));
            }
            return(Ok(basket));
        }
示例#7
0
        public async Task <IActionResult> Add_To_Basket_Item(string username, BasketCartItem item_to_add)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(BadRequest());
            }
            var basket_that_receives = await _repository.AddBasketItem(username, item_to_add);

            if (basket_that_receives == null)
            {
                return(StatusCode(500, "basket could not receive items"));
            }
            return(Ok(basket_that_receives));
        }
示例#8
0
        public async Task <bool> DeleteItem(string username, BasketCartItem item)
        {
            var redisCart = await _context.Database.StringGetAsync(username);

            if (redisCart.IsNullOrEmpty)
            {
                return(false);
            }
            try
            {
                var cart = JsonConvert.DeserializeObject <BasketCart>(redisCart);
                cart.Items.RemoveAll(i => i.ProductId.Equals(item.ProductId));
                return(await CreateOrUpdateCart(cart));
            }
            catch
            {
                return(false);
            }
        }
        public async Task <bool> DeleteBasketItem(string username, BasketCartItem targetItem)
        {
            var basketFromRedis = await _context.Redis.StringGetAsync(username);

            if (basketFromRedis.IsNullOrEmpty)
            {
                return(false);
            }

            try
            {
                var basket = JsonConvert.DeserializeObject <BasketCart>(basketFromRedis);
                basket.Items.RemoveAll(item => item.ProductId.Equals(targetItem.ProductId));
                return(await UpdateBasketItems(basket));
            }
            catch
            {
                return(false);
            }
        }
示例#10
0
 public async Task <IActionResult> ControlProductStock([FromBody] BasketCartItem basketCartItem)
 {
     return(Ok(await _stockRepository.ControlProductStock(basketCartItem)));
 }