Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created March 9, 2022 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/17eaaac3dfb4f51ba9c696e6cb26add0 to your computer and use it in GitHub Desktop.
Save dcomartin/17eaaac3dfb4f51ba9c696e6cb26add0 to your computer and use it in GitHub Desktop.
public class ArriveHandler : IRequestHandler<Arrive>
{
private readonly ShipmentDbContext _dbContext;
public ArriveHandler(ShipmentDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Unit> Handle(Arrive 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.InTransit)
{
throw new InvalidOperationException("Stop has already arrived.");
}
stop.Status = StopStatus.Arrived;
stop.Arrived = request.Arrived;
await _dbContext.SaveChangesAsync();
return Unit.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment