public static void UpdateOrder(this Order entity, OrderModel viewModel) { entity.CustomerNumber = viewModel.CustomerNumber; entity.OrderDate = viewModel.OrderDate; entity.DeliveryDate = viewModel.DeliveryDate; if (viewModel.OrderPositions != null) { if (entity.OrderPositions == null) entity.OrderPositions = new List<OrderPosition>(); foreach (OrderPositionModel pos in viewModel.OrderPositions) { var posEntity = entity.OrderPositions.SingleOrDefault(x => x.ID == pos.PositionOrderId); if (posEntity == null || posEntity.ID == 0) { posEntity = new OrderPosition(); posEntity.UpdateOrderPosition(pos); entity.OrderPositions.Add(posEntity); } else { posEntity.UpdateOrderPosition(pos); } } } entity.TotalPrice = entity.OrderPositions.Sum(x => x.Total); }
public int AddOrder(OrderModel orderModel) { if (orderModel.OrderPositions.Count < 1) return -1; var order = new Order { CustomerNumber = orderModel.CustomerNumber.Trim(), TotalPrice = orderModel.TotalPrice, DeliveryDate = orderModel.DeliveryDate, OrderDate = DateTime.Now }; foreach (var position in orderModel.OrderPositions) { var positionOrder = new OrderPosition { OrderID = order.ID, Pieces = position.Pieces != null ? position.Pieces.Trim() : null, PositionNumber = position.PositionNumber, Price = position.Price, Text = position.Text != null ? position.Text.Trim() : null, Total = position.Total }; order.TotalPrice += positionOrder.Total; order.OrderPositions.Add(positionOrder); } try { _orderBusinessService.AddOrder(order); } catch (Exception ex) { //TODO: throw; } return order.ID; }