Skip to content

Instantly share code, notes, and snippets.

@dasch
Last active Jul 7, 2017
Embed
What would you like to do?
Event Sourcing Example

The rules of the reservation of a train are the following:

  • We cannot reserve seats in a train if it bumps up the occupancy over 70%
  • All the reserved seats should be in the same coach (we cannot separate families)
  • Preferably, we should avoid bumping the occupancy of a coach over 80%
module Trains exposing (..)
type Command
= ReserveSeat { count : Int }
handle : Command -> Model -> Result String Event
handle cmd model =
case cmd of
ReserveSeats { count } ->
if model.reservedSeats + count > model.availableSeats then
Err "No enough available seats"
else
SeatsReserved { count = count }
type alias Model =
{ availableSeats : Int
, reservedSeats : Int
}
type Event
= SeatsReserved { count : Int }
init : Model
init = { availableSeats = 200, reservedSeats = 0 }
apply : Event -> Model -> Model
apply event model =
case event of
SeatsReserved { count } ->
{ model | reservedSeats = model.reservedSeats + count }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment