public override VehicleState When(VehicleState state, object @event) { return(@event switch { VehicleRegistered e => With(state, x => { x.Id = e.VehicleId; x.CustomerId = e.CustomerId; x.State = e.State; x.Registration = e.Registration; x.MaxSpeed = e.MaxSpeed; x.MaxTemperature = e.MaxTemperature; }), VehicleMaxSpeedAdjusted e => With(state, x => x.MaxSpeed = e.MaxSpeed), VehicleMaxTemperatureAdjusted e => With(state, x => x.MaxTemperature = e.MaxTemperature), VehicleOverheated e => Overheated(e.Temperature), VehicleSpeeingDetected e => Speeding(e.RecordedSpeed), _ => state });
public static Func <Task> GetHandler(IAsyncDocumentSession session, object @event) { return(@event switch { VehicleRegistered e => () => session.StoreAsync( new VehicleItem { Id = GetDbId(e.VehicleId), Registration = e.Registration, MakeModel = e.MakeModel, MaxSpeed = e.MaxSpeed, MaxTemperature = e.MaxTemperature, State = e.State, Sensors = new List <VehicleItem.VehicleSensor>() } ), VehicleMaxSpeedAdjusted e => () => Update(e.VehicleId, x => x.MaxSpeed = e.MaxSpeed), VehicleMaxTemperatureAdjusted e => () => Update(e.VehicleId, x => x.MaxTemperature = e.MaxTemperature), VehicleSpeeingDetected e => () => Update(e.VehicleId, x => x.State = "Speeding"), VehicleOverheated e => () => Update(e.VehicleId, x => x.State = "Overheated"), Messages.Sensor.Events.SensorTelemetryReceived e => () => UpdateOneSensor( e.VehicleId, e.SensorId, sensor => { sensor.Speed = e.Speed; sensor.Temperature = e.Temperature; } ), _ => (Func <Task>)null });