Skip to content

Commit

Permalink
Dumber code, better examples to present
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Nov 24, 2023
1 parent 90c33c4 commit 60f4465
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/aggregate_root/lib/project_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

require_relative "../../../shared/lib/project_management"
require_relative "project_management/handler"
require_relative "project_management/repository"
require_relative "project_management/issue"
53 changes: 43 additions & 10 deletions examples/aggregate_root/lib/project_management/handler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ProjectManagement
class Handler
def initialize(event_store)
@repository = AggregateRoot::Repository.new(event_store)
@repository = Repository.new(event_store)
end

def call(cmd)
Expand All @@ -23,17 +23,50 @@ def call(cmd)
raise Error
end

def create(cmd) = with_aggregate(cmd.id) { |issue| issue.open }
def resolve(cmd) = with_aggregate(cmd.id) { |issue| issue.resolve }
def close(cmd) = with_aggregate(cmd.id) { |issue| issue.close }
def reopen(cmd) = with_aggregate(cmd.id) { |issue| issue.reopen }
def start(cmd) = with_aggregate(cmd.id) { |issue| issue.start }
def stop(cmd) = with_aggregate(cmd.id) { |issue| issue.stop }

private

def with_aggregate(id, &block)
@repository.with_aggregate(Issue.new(id), "Issue$#{id}", &block)
def create(cmd)
@repository.with_aggregate(
Issue.new(cmd.id),
stream_name(cmd.id)
) { |issue| issue.open }
end

def resolve(cmd)
issue = Issue.new(cmd.id)
@repository.load(issue, stream_name(cmd.id))
issue.resolve
@repository.store(issue, stream_name(cmd.id))
end

def close(cmd)
issue = Issue.new(cmd.id)
@repository.load(issue, stream_name(cmd.id))
issue.close
@repository.store(issue, stream_name(cmd.id))
end

def reopen(cmd)
issue = Issue.new(cmd.id)
@repository.load(issue, stream_name(cmd.id))
issue.reopen
@repository.store(issue, stream_name(cmd.id))
end

def start(cmd)
issue = Issue.new(cmd.id)
@repository.load(issue, stream_name(cmd.id))
issue.start
@repository.store(issue, stream_name(cmd.id))
end

def stop(cmd)
issue = Issue.new(cmd.id)
@repository.load(issue, stream_name(cmd.id))
issue.stop
@repository.store(issue, stream_name(cmd.id))
end

def stream_name(id) = "Issue$#{id}"
end
end
3 changes: 3 additions & 0 deletions examples/aggregate_root/lib/project_management/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ProjectManagement
Repository = AggregateRoot::Repository
end

0 comments on commit 60f4465

Please sign in to comment.