public Cart UpdateCartItem(CartItem cartItem) { if (cartItem.Quantity != 0) { _cartRepository.AddOrUpdateCartItem(cartItem.CartItemToCartItemEntity()); } else { _cartRepository.RemoveCartItem(cartItem.Id); } _eventor.Publish(new CartChangeEvent()); return(GetCart()); }
public Order CreateOreder(OrderCreateCreteria creteria) { var order = new Order { ShipmentId = creteria.ShippingId, Shipment = (double)creteria.Cart.Shipment, Customer = creteria.Customer, Discount = (double)creteria.Cart.Discount, Id = Guid.NewGuid().ToString(), SubTotal = (double)creteria.Cart.SubTotal, Taxes = (double)creteria.Cart.Taxes, Total = (double)creteria.Cart.Total, PaymentId = creteria.PaymentId }; _orderRepository.StartTransaction(); try { if (!_orderRepository.SaveOrder(order.ModelToEntity())) { _orderRepository.RollbackTransaction(); return(null); } foreach (var item in creteria.Cart.CartItems) { var orderItem = new OrderItem { Currency = item.Currency.Code, Discount = item.Discount, ProductId = item.Product.Id, Id = Guid.NewGuid().ToString(), OrderId = order.Id, Quantity = item.Quantity, SubTotal = item.SubTotal }; order.Items.Add(orderItem); if (!_orderRepository.SaveOrderItem(orderItem.ModelToEntity())) { _orderRepository.RollbackTransaction(); return(null); } } order.Customer = creteria.Customer; if (string.IsNullOrEmpty(order.Customer.Id)) { order.Customer.Id = Guid.NewGuid().ToString(); } var customerEntity = order.Customer.ModelToEntity(); customerEntity.OrderId = order.Id; if (!_orderRepository.SaveOrderCustomer(customerEntity)) { _orderRepository.RollbackTransaction(); return(null); } _orderRepository.EndTransaction(); _cartService.ClearCart(); _globalEventor.Publish(new Events.CartChangeEvent()); } catch (Exception) { _orderRepository.RollbackTransaction(); } return(order); }