Skip to content

Commit

Permalink
Extract repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Nov 27, 2023
1 parent abbbb91 commit d15e9c1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/query_based/lib/project_management.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "../../../shared/lib/project_management"
require_relative "project_management/issue"
require_relative "project_management/repository"
require_relative "project_management/issue_projection"
require_relative "project_management/handler"
11 changes: 4 additions & 7 deletions examples/query_based/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)
@event_store = event_store
@repository = Repository.new(event_store)
end

def call(cmd)
Expand Down Expand Up @@ -65,13 +65,10 @@ def stop(id)

private

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

def with_aggregate(id)
issue =
IssueProjection.new(@event_store).call(Issue.initial, stream_name(id))

@event_store.append(yield(issue), stream_name: stream_name(id))
issue = @repository.load(id, Issue.initial)
events = yield(issue)
@repository.store(id, events)
end
end
end
10 changes: 2 additions & 8 deletions examples/query_based/lib/project_management/issue_projection.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
module ProjectManagement
class IssueProjection
def initialize(event_store)
@event_store = event_store
end

def call(initial_issue, stream_name)
@event_store
.read
.stream(stream_name)
def self.call(query, initial_issue)
query
.reduce(initial_issue) do |issue, event|
case event
when IssueOpened
Expand Down
23 changes: 23 additions & 0 deletions examples/query_based/lib/project_management/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ProjectManagement
class Repository
def initialize(event_store)
@event_store = event_store
end

def load(id, initial_state)
query =
@event_store
.read
.stream(stream_name(id))
IssueProjection.call(query, initial_state)
end

def store(id, events)
@event_store.append(events, stream_name: stream_name(id))
end

private

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

0 comments on commit d15e9c1

Please sign in to comment.