-
-
Save dcomartin/8ac0f848678921f250d9aaaeb2821f1b 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
namespace RouteExample2; | |
public class Route | |
{ | |
public Guid RouteId { get; } | |
public string Name { get; } | |
public int AlarmCount => Locations.Sum(x => x.VendingMachine?.NewAlarms.Count ?? 0); | |
public DateTime LastAlarmDate => Locations.Max(x => x.VendingMachine?.NewAlarms.Max(y => y.Date) ?? DateTime.MinValue); | |
private List<Location> Locations { get; } = new(); | |
public Route(Guid routeId, string name) | |
{ | |
RouteId = routeId; | |
Name = name; | |
} | |
public Guid AddLocation(string name, LatLong coords) | |
{ | |
var locationId = Guid.NewGuid(); | |
Locations.Add(new Location(locationId, name, coords)); | |
return locationId; | |
} | |
public Guid AddVendingMachineToLocation(Guid locationId) | |
{ | |
var location = Locations.SingleOrDefault(x => x.LocationId == locationId); | |
if (location == null) | |
{ | |
throw new InvalidOperationException("Location is not associated to route."); | |
} | |
var vendingMachineId = Guid.NewGuid(); | |
location.AssociateVendingMachine(new VendingMachine(vendingMachineId)); | |
return vendingMachineId; | |
} | |
public void Alarm(Guid vendingMachineId, DateTime date) | |
{ | |
var location = Locations.SingleOrDefault(x => x.VendingMachine != null && x.VendingMachine.VendingMachineId == vendingMachineId); | |
if (location == null) | |
{ | |
throw new InvalidOperationException("Vending machine is not associated to route."); | |
} | |
location.VendingMachine!.AddAlarm(location.LocationId, date); | |
} | |
} | |
public class Location | |
{ | |
public Guid LocationId { get; } | |
public string Name { get; } | |
public LatLong Coords{ get; } | |
public VendingMachine? VendingMachine { get; private set; } | |
public Location(Guid locationId, string name, LatLong coords) | |
{ | |
LocationId = locationId; | |
Name = name; | |
Coords = coords; | |
} | |
public void AssociateVendingMachine(VendingMachine vendingMachine) | |
{ | |
VendingMachine = vendingMachine; | |
} | |
} | |
public class VendingMachine | |
{ | |
public Guid VendingMachineId { get; } | |
public List<AlarmTriggered> NewAlarms { get; } = new(); | |
public VendingMachine(Guid vendingMachine) | |
{ | |
VendingMachineId = vendingMachine; | |
} | |
public void AddAlarm(Guid locationId, DateTime dateTime) | |
{ | |
NewAlarms.Add(new AlarmTriggered(VendingMachineId, locationId, dateTime)); | |
} | |
} | |
public record LatLong(double Lat, double Lng); | |
public record AlarmTriggered(Guid VendingMachineId, Guid LocationId, DateTime Date); | |
public class Example | |
{ | |
public void Usage() | |
{ | |
var route = new Route(Guid.NewGuid(), "Route 1"); | |
var locationId = route.AddLocation("Location 1", new LatLong(1, 1)); | |
var vendingMachineId = route.AddVendingMachineToLocation(locationId); | |
route.Alarm(vendingMachineId, DateTime.UtcNow); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment