public void AddComputerToOrder(string orderId, AddComputersToOrderDto computersToOrderDto) { User user = AuthenticationService.AuthenticateUser(_username, _password); ValidationService.ValidateAddComputersToOrderDto(computersToOrderDto); Order order = QueryService.FindOrder(orderId); if (!order.User.Username.Equals(user.Username)) { throw new Exception("Order doesn't belong to user. User: " + _username); } Entities entities = DatabaseContext.GetEntities(); foreach (var orderLineDto in computersToOrderDto.OrderLines) { OrderLine orderLine = order.OrderLines.FirstOrDefault(ol => ol.Computer.Guid == orderLineDto.ComputerId); if (orderLine != null) { orderLine.Quantity += orderLineDto.Quantity; } else { order.OrderLines.Add(TransformationService.AddOrderLineToOrderLine(orderLineDto)); } } entities.Save(); }
public static void ValidateAddComputersToOrderDto(AddComputersToOrderDto computersToOrderDto) { computersToOrderDto.OrderLines.ToList().ForEach(orderLine => { if (string.IsNullOrEmpty(orderLine.ComputerId)) { throw new Exception("Computer id cannot be null or empty"); } if (orderLine.Quantity < 0) { throw new Exception("Computer quantity cannot be negative"); } }); }