public ResponseMessage ExecuteOperation(RequestMessage request) { try { Order order = OrderRepository.CreateOrder(); order.OrderItems.AddRange(request.OrderItems); foreach (OrderItem item in order.OrderItems) { InventoryItem inventoryItem = InventoryRepository.GetInventoryItem(item.ItemCode); if (item.Quantity <= inventoryItem.QuantityOnHand) { inventoryItem.QuantityOnHand -= item.Quantity; item.Weight = item.WeightPerUnit * (float)item.Quantity; _priceCalculator.CalculatePrice(item, inventoryItem); item.State = OrderItemState.Filled; } else { item.State = OrderItemState.NotEnoughQuantityOnHand; } } order.State = order.OrderItems.All(o => o.State == OrderItemState.Filled) ? OrderState.Filled : OrderState.Processing; OrderRepository.AddOrder(order); // save inventory InventoryRepository.UpdateInventory(); // save order OrderRepository.UpdateOrders(); return(_responseMessageFactory.CreateOrderSubmissionResponseMessage(request, order)); } catch (Exception ex) { _loggerService.WriteLine("Exception during operation SubmitOrder: " + ex, "SubmitOrderError"); return(_responseMessageFactory.CreateErrorResponseMessage(ex)); } }