Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active June 1, 2022 21:08
Show Gist options
  • Save dcomartin/393fe18b3fc60d0f61d71335a415956e to your computer and use it in GitHub Desktop.
Save dcomartin/393fe18b3fc60d0f61d71335a415956e to your computer and use it in GitHub Desktop.
public class ShipmentService
{
private readonly ShipmentDbContext _dbContext;
public ShipmentService(ShipmentDbContext dbContext)
{
_dbContext = dbContext;
}
public void Arrive(Shipment shipment, int stopId)
{
var stop = shipment.GetStop(stopId);
var previousStopsAreNotDeparted = shipment.GetAllStops().Any(x => x.StopId < stop.StopId && x.Status != StopStatus.Departed);
if (previousStopsAreNotDeparted)
{
throw new InvalidOperationException("Previous stops have not departed.");
}
stop.SetStatus(StopStatus.Arrived);
stop.SetArrivedDateTime(DateTime.UtcNow);
}
public void Pickup(Shipment shipment, int stopId)
{
var stop = shipment.GetStop(stopId);
if (stop.Status != StopStatus.Arrived)
{
throw new InvalidOperationException("Stop is not yet arrived.");
}
stop.SetStatus(StopStatus.Departed);
stop.SetDepartedDateTime(DateTime.UtcNow);
}
public void Deliver(Shipment shipment, int stopId)
{
var stop = shipment.GetStop(stopId);
if (stop.Status != StopStatus.Arrived)
{
throw new InvalidOperationException("Stop is not yet arrived.");
}
stop.SetStatus(StopStatus.Departed);
stop.SetDepartedDateTime(DateTime.UtcNow);
}
public bool IsComplete(Shipment shipment)
{
return shipment.GetAllStops().All(x => x.Status == StopStatus.Departed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment