Skip to content

Commit

Permalink
One step back
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Fiedler <[email protected]>
Co-authored-by: Piotr Jurewicz <[email protected]>
  • Loading branch information
3 people committed Nov 22, 2023
1 parent a2089ec commit a6c4c8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
12 changes: 6 additions & 6 deletions examples/decider/lib/project_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ module ProjectManagement
class Configuration
def call(event_store, command_bus)
handler = CommandHandler.new(event_store)
command_bus.register(CreateIssue, handler.public_method(:create))
command_bus.register(ReopenIssue, handler.public_method(:reopen))
command_bus.register(ResolveIssue, handler.public_method(:resolve))
command_bus.register(CloseIssue, handler.public_method(:close))
command_bus.register(StartIssueProgress, handler.public_method(:start))
command_bus.register(StopIssueProgress, handler.public_method(:stop))
command_bus.register(CreateIssue, handler.public_method(:handle))
command_bus.register(ReopenIssue, handler.public_method(:handle))
command_bus.register(ResolveIssue, handler.public_method(:handle))
command_bus.register(CloseIssue, handler.public_method(:handle))
command_bus.register(StartIssueProgress, handler.public_method(:handle))
command_bus.register(StopIssueProgress, handler.public_method(:handle))
end
end
end
21 changes: 7 additions & 14 deletions examples/decider/lib/project_management/command_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@ module ProjectManagement
class CommandHandler
def initialize(event_store) = @event_store = event_store

def create(cmd) = with_event_store(cmd)
def resolve(cmd) = with_event_store(cmd)
def close(cmd) = with_event_store(cmd)
def reopen(cmd) = with_event_store(cmd)
def start(cmd) = with_event_store(cmd)
def stop(cmd) = with_event_store(cmd)

private

def stream_name(id) = "Issue$#{id}"

def with_event_store(cmd)
def handle(cmd)
state, version =
@event_store
.read
.stream(stream_name(cmd.id))
.reduce(
[Issue.initial_state(cmd.id), -1]
) do |(state, version), event|
[Issue.evolve(state, event), version + 1]
end
[Issue.evolve(state, event), version + 1]
end

events = Issue.decide(cmd, state)
raise Error unless events
Expand All @@ -33,5 +22,9 @@ def with_event_store(cmd)
expected_version: version
)
end

private

def stream_name(id) = "Issue$#{id}"
end
end

0 comments on commit a6c4c8e

Please sign in to comment.