Created
September 12, 2025 23:22
-
-
Save rcarubbi/8a658eb812d6b599f088306fb6519ade to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sealed class PlaceOrderUseCase : IPlaceOrderUseCase | |
| { | |
| private readonly IOrderRepository _orders; | |
| private readonly IPaymentGateway _payments; | |
| public PlaceOrderUseCase(IOrderRepository orders, IPaymentGateway payments) | |
| { | |
| _orders = orders; | |
| _payments = payments; | |
| } | |
| public PlaceOrderResult Execute(PlaceOrderCommand cmd) | |
| { | |
| var order = Order.Create(cmd.CustomerCode, cmd.Currency); | |
| foreach (var line in cmd.Lines) | |
| order.AddItem(line.Sku, line.Quantity, line.UnitPrice); | |
| order.Confirm(); // invariantes do agregado | |
| var auth = _payments.Authorize(new PaymentRequest(order.Number, order.Currency, order.Total())); | |
| switch (auth.Status) | |
| { | |
| case PaymentAuthStatus.Approved: | |
| order.MarkPaid(auth.Code); | |
| break; | |
| case PaymentAuthStatus.Declined: | |
| order.MarkPaymentFailed(auth.Code); | |
| break; | |
| case PaymentAuthStatus.Pending: | |
| order.MarkAwaitingSettlement(auth.Code); | |
| break; | |
| } | |
| _orders.Save(order); | |
| return new PlaceOrderResult(order.Number, order.Total(), order.Currency); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment