diff --git a/examples/functional/lib/project_management.rb b/examples/functional/lib/project_management.rb index 8621c5a..c79c6e6 100644 --- a/examples/functional/lib/project_management.rb +++ b/examples/functional/lib/project_management.rb @@ -1,4 +1,5 @@ require_relative "../../../shared/lib/project_management" require_relative "project_management/handler" require_relative "project_management/issue" +require_relative "project_management/repository" require_relative "project_management/issue_state" diff --git a/examples/functional/lib/project_management/handler.rb b/examples/functional/lib/project_management/handler.rb index 541ad86..3e90b84 100644 --- a/examples/functional/lib/project_management/handler.rb +++ b/examples/functional/lib/project_management/handler.rb @@ -1,6 +1,8 @@ module ProjectManagement class Handler - def initialize(event_store) = @event_store = event_store + def initialize(event_store) + @repository = Repository.new(event_store) + end def call(cmd) case cmd @@ -28,20 +30,14 @@ def stop(id) = with_state(id) { |state| Issue.stop(state) } private - def stream_name(id) = "Issue$#{id}" - def with_state(id) - state = - @event_store - .read - .stream(stream_name(id)) - .reduce(IssueState.initial(id)) { |state, event| state.apply(event) } + state = @repository.load(id, IssueState.initial(id)) case yield(state) in StandardError raise Error in Event => event - @event_store.append(event, stream_name: stream_name(id)) + @repository.store(id, event) end end end diff --git a/examples/functional/lib/project_management/repository.rb b/examples/functional/lib/project_management/repository.rb new file mode 100644 index 0000000..0ae4e3c --- /dev/null +++ b/examples/functional/lib/project_management/repository.rb @@ -0,0 +1,22 @@ +module ProjectManagement + class Repository + def initialize(event_store) + @event_store = event_store + end + + def load(id, initial_state) + @event_store + .read + .stream(stream_name(id)) + .reduce(initial_state) { |state, event| state.apply(event) } + end + + def store(id, events) + @event_store.append(events, stream_name: stream_name(id)) + end + + private + + def stream_name(id) = "Issue$#{id}" + end +end