private async Task <IActionResult> inputOrderItem(List <CartItemDto> itemsInCart, int?orderId, double?customerGst) { if (itemsInCart == null || orderId == null) { return(NotFound("Nothing in shopping cart!")); } foreach (var item in itemsInCart) { var newOrderItem = new OrderItem(); newOrderItem.Id = orderId.GetValueOrDefault(); newOrderItem.Code = Convert.ToInt32(item.code); newOrderItem.ItemName = item.name; newOrderItem.Note = item.note; newOrderItem.Quantity = Convert.ToDouble(item.quantity); newOrderItem.SupplierCode = item.supplier_code ?? ""; newOrderItem.Supplier = ""; newOrderItem.CommitPrice = Convert.ToDecimal(item.sales_price) / Convert.ToDecimal(1 + customerGst ?? 0.15); newOrderItem.Cat = _iitem.getCat("cat", newOrderItem.Code); // _context.CodeRelations.Where(c => c.Code == Convert.ToInt32(item.code)).FirstOrDefault().Cat; newOrderItem.SCat = _iitem.getCat("scat", newOrderItem.Code); //_context.CodeRelations.Where(c => c.Code == Convert.ToInt32(item.code)).FirstOrDefault().SCat; newOrderItem.SsCat = _iitem.getCat("sscat", newOrderItem.Code); //_context.CodeRelations.Where(c => c.Code == Convert.ToInt32(item.code)).FirstOrDefault().SsCat; await _context.AddAsync(newOrderItem); } // await _context.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> addShippingAddress(int userId, [FromBody] AddShippingDto newShipping) { var shippingToAdd = new Card(); shippingToAdd.MainCardId = userId; shippingToAdd.Name = newShipping.name; shippingToAdd.Company = newShipping.company; shippingToAdd.Address1 = newShipping.address1; shippingToAdd.Address2 = newShipping.address2; shippingToAdd.Address3 = newShipping.address3; shippingToAdd.City = newShipping.city; shippingToAdd.Country = newShipping.country; shippingToAdd.Phone = newShipping.phone; shippingToAdd.Contact = newShipping.contact; shippingToAdd.Zip = newShipping.zip; shippingToAdd.Note = newShipping.note; shippingToAdd.Email = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); try { await _context.AddAsync(shippingToAdd); await _context.SaveChangesAsync(); } catch (Exception e) { throw e; } return(Ok()); }
public async Task <IActionResult> addToCart(int userId, [FromBody] AddItemToCartDto itemToCart) { if (itemToCart == null) { return(NotFound()); } if (_context.Carts.Any(c => c.Code == itemToCart.code && c.Name == itemToCart.name && c.SupplierCode == itemToCart.supplier_code && c.CardId == userId && c.SalesPrice == itemToCart.sales_price.ToString())) { //Add new qty to this item var existingItem = _context.Carts.Where(c => c.Code == itemToCart.code && c.Name == itemToCart.name && c.SupplierCode == itemToCart.supplier_code && c.CardId == userId && 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.Carts.Where(c => c.Id == existingItem.Id && c.CardId == userId).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)); } }