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); } }
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); } }
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)); }
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)); }
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); } }
public async Task <IActionResult> ControlProductStock([FromBody] BasketCartItem basketCartItem) { return(Ok(await _stockRepository.ControlProductStock(basketCartItem))); }