public async Task <ActionResult <CartTransferObject> > GetCart(string cartId) { var cart = await _cartRepository.Get(cartId); if (cart != null) { var transferObject = new CartTransferObject { CartItems = cart.CartItems.Select(x => new CartItemTransferObject { ProductId = x.ProductId, GrossPrice = x.GrossPrice, Id = x.Id, Name = x.Name, NetPrice = x.NetPrice, Sku = x.Sku, Quantity = x.Quantity, Tax = x.Tax, UnitPrice = x.UnitPrice }).ToList(), CreatedDate = cart.CreatedDate, Gross = cart.Gross, Id = cart.Id, LastUpdated = cart.LastUpdated, Net = cart.Net, Tax = cart.Tax, TotalItems = cart.ItemCount }; return(Ok(transferObject)); } return(NotFound()); }
public async Task <ActionResult <CartTransferObject> > UpdateCart(CartTransferObject cart) { try { var response = await _cartService.Post(cart); return(Ok(response)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void Create(CartTransferObject cartTransferObject, int orderId) { foreach (var item in cartTransferObject.CartItems) { _context.OrderItems.Add(new OrderItem { GrossPrice = item.GrossPrice, Name = item.Name, NetPrice = item.NetPrice, OrderId = orderId, Quantity = item.Quantity, Sku = item.Sku, Tax = item.Tax, UnitPrice = item.UnitPrice }); } }
public async Task <ActionResult <CartTransferObject> > UpdateCart(CartTransferObject cart) { double gross = 0.00; double tax = 0.00; double net = 0.00; var itemCount = 0; if (string.IsNullOrEmpty(cart.Id)) { cart.Id = Guid.NewGuid().ToString(); foreach (var item in cart.CartItems) { await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject { Sku = item.Sku, TransactionType = "RESERVE", TransactionCount = item.Quantity }); item.UnitPrice = await _productClient.GetItemBasePrice(item.ProductId); item.NetPrice = item.UnitPrice * item.Quantity; itemCount += item.Quantity; net += (double)item.NetPrice; item.Tax = item.NetPrice * (decimal)0.2; tax += (double)item.Tax; item.GrossPrice = item.NetPrice + item.Tax; gross += (double)item.GrossPrice; _cartItemRepository.Create(new CartItem { CartId = cart.Id, GrossPrice = item.GrossPrice, Name = item.Name, NetPrice = item.NetPrice, Quantity = item.Quantity, Sku = item.Sku, Tax = item.Tax, UnitPrice = item.UnitPrice, }); } var saveCart = new Cart { Id = cart.Id, CreatedDate = DateTime.Now, Gross = (decimal)gross, ItemCount = itemCount, LastUpdated = DateTime.Now, Net = (decimal)net, Tax = (decimal)tax }; cart.Gross = (decimal)gross; cart.Net = (decimal)net; cart.Tax = (decimal)tax; cart.TotalItems = itemCount; cart.CreatedDate = DateTime.Now; cart.LastUpdated = DateTime.Now; _cartRepository.Create(saveCart); await _cartRepository.SaveChanges(); return(Ok(cart)); } else { var updateCart = await _cartRepository.Get(cart.Id); if (updateCart == null) { return(NotFound()); } foreach (var item in cart.CartItems) { item.NetPrice = item.UnitPrice * item.Quantity; itemCount += item.Quantity; net += (double)item.NetPrice; item.Tax = item.NetPrice * (decimal)0.2; tax += (double)item.Tax; item.GrossPrice = item.NetPrice + item.Tax; gross += (double)item.GrossPrice; } updateCart.LastUpdated = DateTime.Now; cart.LastUpdated = updateCart.LastUpdated; cart.TotalItems = updateCart.ItemCount; _cartRepository.Update(updateCart); foreach (var item in updateCart.CartItems) { _cartItemRepository.Update(item); if (_cartItemRepository.GetEntityState(item) == Microsoft.EntityFrameworkCore.EntityState.Deleted) { await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject { Sku = item.Sku, TransactionType = "FREE", TransactionCount = item.Quantity }); } else if (_cartItemRepository.GetEntityState(item) == Microsoft.EntityFrameworkCore.EntityState.Added) { await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject { Sku = item.Sku, TransactionType = "RESERVE", TransactionCount = item.Quantity }); } } await _cartRepository.SaveChanges(); return(Ok(cart)); } }
public async Task <CartTransferObject> Post(CartTransferObject entity) { return(await _httpService.Post <CartTransferObject>($"{baseUri}api/cartservice/v1/updatecart", entity)); }