private async Task <IActionResult> ClearShoppingCart(int card_id) { var recordAffected = _context.Cart.Where(c => c.CardId == card_id).ToList(); if (recordAffected.Count == 0) { return(NotFound()); } if (recordAffected.Count > 0) { foreach (var i in recordAffected) { _context.Remove(i); } // _context.RemoveRange(recordAffected); await _context.SaveChangesAsync(); } return(Ok()); }
public IActionResult delcat([FromQuery] int id) { Catalog delcat = new Catalog(); CodeRelations updateItem = new CodeRelations(); List <CodeRelations> affectedItems = new List <CodeRelations>(); delcat = _context.Catalog.Where(c => c.Id == id).FirstOrDefault(); var affectedCatLevel = delcat.LayerLevel; //update categories for affected items if (affectedCatLevel == 1) { affectedItems = _context.CodeRelations.Where(cl => cl.Cat == delcat.Cat).ToList(); foreach (var item in affectedItems) { item.Cat = ""; } } else if (affectedCatLevel == 2) { affectedItems = _context.CodeRelations.Where(cl => cl.SCat == delcat.Cat).ToList(); foreach (var item in affectedItems) { item.SCat = ""; } } else if (affectedCatLevel == 3) { affectedItems = _context.CodeRelations.Where(cl => cl.SsCat == delcat.Cat).ToList(); foreach (var item in affectedItems) { item.SsCat = ""; } } if (delcat != null) { _context.Remove(delcat); _context.SaveChanges(); } return(Ok(delcat)); }
public async Task <IActionResult> addToCart(int cardid, [FromBody] AddItemToCartDto itemToCart) { if (itemToCart == null) { return(NotFound()); } if (_context.Cart.Any(c => c.Code == itemToCart.code && c.Name == itemToCart.name && c.SupplierCode == itemToCart.supplier_code && c.CardId == cardid && c.SalesPrice == itemToCart.sales_price.ToString())) { //Add new qty to this item var existingItem = _context.Cart.Where(c => c.Code == itemToCart.code && c.Name == itemToCart.name && c.SupplierCode == itemToCart.supplier_code && c.CardId == cardid && c.SalesPrice == itemToCart.sales_price.ToString()).FirstOrDefault(); var dQuantity = Convert.ToDouble(existingItem.Quantity); if ((dQuantity + itemToCart.quantity) < 0) { return(BadRequest("qty < 0")); } dQuantity += itemToCart.quantity; existingItem.Quantity = dQuantity.ToString(); //if new qty == 0, remove this item from cart if (dQuantity == 0) { // await deleteFromCart(cardid, existingItem.Id); var itemToRemoveFromCart = new Cart(); itemToRemoveFromCart = _context.Cart.Where(c => c.Id == existingItem.Id && c.CardId == cardid).FirstOrDefault(); if (itemToRemoveFromCart == null) { return(NotFound()); } _context.Remove(itemToRemoveFromCart); } await _context.SaveChangesAsync(); //async return(NoContent()); } else { if (itemToCart.quantity <= 0) { return(BadRequest("quantity <= 0")); } var newItem = new Cart(); newItem.CardId = itemToCart.card_id; newItem.Code = itemToCart.code; newItem.Name = itemToCart.name; newItem.Barcode = itemToCart.barcode; newItem.SalesPrice = itemToCart.sales_price.ToString(); newItem.Quantity = itemToCart.quantity.ToString(); newItem.SupplierCode = itemToCart.supplier_code; newItem.Points = itemToCart.points.ToString(); await _context.AddAsync(newItem); await _context.SaveChangesAsync(); return(Ok(newItem)); } }