-
-
Save dcomartin/0cfbc89d042d5b7433469e0b9772f347 to your computer and use it in GitHub Desktop.
This file contains 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 PickupHandler : IRequestHandler<Pickup> | |
{ | |
private readonly ShipmentDbContext _dbContext; | |
private readonly IBus _bus; | |
public PickupHandler(ShipmentDbContext dbContext, IBus bus) | |
{ | |
_dbContext = dbContext; | |
_bus = bus; | |
} | |
public async Task<Unit> Handle(Pickup request, CancellationToken cancellationToken) | |
{ | |
var stop = await _dbContext.Stops.SingleOrDefaultAsync(x => x.StopId == request.StopId); | |
if (stop == null) | |
{ | |
throw new InvalidOperationException("Stop does not exist."); | |
} | |
if (stop.Status == StopStatus.Departed) | |
{ | |
throw new InvalidOperationException("Stop has already departed."); | |
} | |
if (stop.Status == StopStatus.InTransit) | |
{ | |
throw new InvalidOperationException("Stop has not arrived yet."); | |
} | |
if (request.Departed < stop.Arrived) | |
{ | |
throw new InvalidOperationException("Departed Date/Time cannot be before Arrived Date/Time."); | |
} | |
stop.Status = StopStatus.Departed; | |
stop.Departed = request.Departed; | |
await _dbContext.SaveChangesAsync(); | |
await _bus.Publish(new PickedUp(request.StopId, request.Departed)); | |
return Unit.Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment