-
-
Save dcomartin/322df373ac684031b1b13cdd9e928439 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 class DispatchShipmentHandler : ICommandHandler<DispatchShipment> | |
| { | |
| private readonly Db _db; | |
| private readonly IEmailer _emailer; | |
| private readonly IEventPublisher _eventPublisher; | |
| private readonly ILogger _logger; | |
| public DispatchShipmentHandler(Db db, IEmailer emailer, IEventPublisher eventPublisher, ILogger logger) | |
| { | |
| _db = db; | |
| _emailer = emailer; | |
| _eventPublisher = eventPublisher; | |
| _logger = logger; | |
| } | |
| public void Handle(DispatchShipment command) | |
| { | |
| var shipment = _db.GetById(command.ShipmentId); | |
| if (shipment == null) | |
| { | |
| throw new InvalidOperationException("Shipment not found."); | |
| } | |
| if (shipment.Status != ShipmentStatus.Ready) | |
| { | |
| throw new InvalidOperationException("Shipment is not ready for dispatch."); | |
| } | |
| shipment.Status = ShipmentStatus.Dispatched; | |
| shipment.DispatchedAt = DateTime.UtcNow; | |
| _db.Save(shipment); | |
| _emailer.NotifyCustomer(shipment); | |
| _eventPublisher.Publish(new ShipmentDispatched(shipment.Id, shipment.DispatchedAt)); | |
| _logger.Log($"Shipment {shipment.Id} dispatched."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment