//NOTE: Order can ONLY be updated when the order status is not yet dispatched or in draft order status public void Update(OrderEdit orderEdit) { /*Validate*/ if (orderEdit == null) { throw new ArgumentNullException(nameof(orderEdit)); } var orderEntity = _dbContext.Orders.SingleOrDefault(o => o.Id == orderEdit.OrderId); if (orderEntity == null) { throw new OrderNotFoundException(); } // Validate data var onlyOrderItems = orderEdit.OrderItems.Select(item => item.OrderItem); _orderValidator.ValidateAndThrow(new Order { Buyer = orderEdit.Buyer, ShippingAddress = orderEdit.ShippingAddress, OrderItems = onlyOrderItems }); /*Ensure products that are ordered must be in stock, check available count*/ _orderItemsValidator.ValidateItemsAndThrow(onlyOrderItems, _dbContext); /*Update Order Object*/ using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction()) { try { UpdateBuyer(orderEdit, orderEntity); //UpdateOrderItems(order, buyerId, shippingAddressId); transaction.Commit(); } catch { transaction.Rollback(); throw; } } }
public void PlaceOrder(Order order) { /*Validate*/ if (order == null) { throw new ArgumentNullException(nameof(order)); } // TODO: Data Cleanse: Merge similar products that exist in collection for easy process by adding together their quantity // _orderValidator.ValidateAndThrow(order); /*Ensure products that are ordered must be in stock, check available count*/ _orderItemsValidator.ValidateItemsAndThrow(order.OrderItems, _dbContext); /*Place Order*/ int orderId = default(int); using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction()) { try { // Create Order (int buyerId, int shippingAddressId) = CreateBuyer(order); orderId = PlaceOrder(order, buyerId, shippingAddressId); //TODO: Update Inventory transaction.Commit(); } catch { transaction.Rollback(); throw; } } /* Send notification */ SendNotification(order, orderId); }