Skip to content

Instantly share code, notes, and snippets.

@dasch
Last active July 7, 2017 12:38
Show Gist options
  • Save dasch/e442ab8d317c1455c37731ef38adb2e9 to your computer and use it in GitHub Desktop.
Save dasch/e442ab8d317c1455c37731ef38adb2e9 to your computer and use it in GitHub Desktop.
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