Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moya adds moveup projector #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.4
2.7.5
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ $ ./scripts/request complete -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -D 2017-01-
# Abandon
$ ./scripts/request abandon -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2 -D 2017-01-01

# Move up
$ ./scripts/request moveup -i aac35923-39b4-4c39-ad5d-f79d67bb2fb2

# List
$ ./scripts/request list -l outstanding
$ ./scripts/request list -l scheduled
$ ./scripts/request list -l completed
$ ./scripts/request list -l movedup
```

## Application Structure
Expand Down
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def processors(db_connection, tracker)
EventSourceryTodoApp::Reactors::TodoCompletedNotifier.new(
tracker: tracker,
db_connection: db_connection,
),
EventSourceryTodoApp::Projections::MovedupTodos::Projector.new(
tracker: tracker,
db_connection: db_connection,
)
]
end
Expand Down
10 changes: 10 additions & 0 deletions app/aggregates/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Todo
apply StakeholderNotifiedOfTodoCompletion do |event|
end

apply TodoMoveup do |event|
end

def add(payload)
raise UnprocessableEntity, "Todo #{id.inspect} already exists" if added?

Expand Down Expand Up @@ -73,6 +76,13 @@ def abandon(payload)
)
end

def moveup(payload)
apply_event(TodoMoveup,
aggregate_id: id,
body: payload,
)
end

private

def added?
Expand Down
41 changes: 41 additions & 0 deletions app/commands/todo/moveup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'app/aggregates/todo'

module EventSourceryTodoApp
module Commands
module Todo
module Moveup
class Command
attr_reader :payload, :aggregate_id

def self.build(**args)
new(**args).tap(&:validate)
end

def initialize(params)
@payload = params.slice(:todo_id)
@aggregate_id = payload.delete(:todo_id)
end

def validate
end
end

class CommandHandler
def initialize(repository: EventSourceryTodoApp.repository)
@repository = repository
end

def handle(command)
aggregate = repository.load(Aggregates::Todo, command.aggregate_id)
aggregate.moveup(command.payload)
repository.save(aggregate)
end

private

attr_reader :repository
end
end
end
end
end
1 change: 1 addition & 0 deletions app/events/todo_moveup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TodoMoveup = Class.new(EventSourcery::Event)
42 changes: 42 additions & 0 deletions app/projections/movedup_todos/projector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module EventSourceryTodoApp
module Projections
module MovedupTodos
class Projector
include EventSourcery::Postgres::Projector

projector_name :movedup_todos

table :query_movedup_todos do
column :todo_id, 'UUID NOT NULL'
column :title, :text
column :description, :text
column :due_date, DateTime
column :stakeholder_email, :text
end

project TodoMoveup do |event|
table.insert(
todo_id: event.aggregate_id,
title: event.body['title'],
description: event.body['description'],
due_date: event.body['due_date'],
stakeholder_email: event.body['stakeholder_email'],
)
end

project TodoAmended do |event|
table.where(
todo_id: event.aggregate_id,
).update(
event.body.slice('title', 'description', 'due_date', 'stakeholder_email')
)
end

project TodoCompleted, TodoAbandoned do |event|
table.where(todo_id: event.aggregate_id).delete
end

end
end
end
end
12 changes: 12 additions & 0 deletions app/projections/movedup_todos/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module EventSourceryTodoApp
module Projections
module MovedupTodos
# Query handler that queries the projection table.
class Query
def self.handle
EventSourceryTodoApp.projections_database[:query_movedup_todos].all
end
end
end
end
end
16 changes: 16 additions & 0 deletions app/web/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
require 'app/commands/todo/add'
require 'app/commands/todo/amend'
require 'app/commands/todo/complete'
require 'app/commands/todo/moveup'
require 'app/projections/completed_todos/query'
require 'app/projections/outstanding_todos/query'
require 'app/projections/scheduled_todos/query'
require 'app/projections/movedup_todos/query'


module EventSourceryTodoApp
class Server < Sinatra::Base
Expand Down Expand Up @@ -70,6 +73,12 @@ def json_params
status 200
end

put '/todo/:todo_id/moveup' do
command = Commands::Todo::Moveup::Command.build(json_params)
Commands::Todo::Moveup::CommandHandler.new.handle(command)
status 200
end

# Queries

get '/todos/outstanding' do
Expand All @@ -92,5 +101,12 @@ def json_params
)
status 200
end

get '/todos/movedup' do
body JSON.pretty_generate(
EventSourceryTodoApp::Projections::MovedupTodos::Query.handle
)
status 200
end
end
end
2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
require 'app/events/todo_added'
require 'app/events/todo_amended'
require 'app/events/todo_completed'
require 'app/events/todo_moveup'
require 'app/events/stakeholder_notified_of_todo_completion'
require 'app/errors'
require 'app/projections/completed_todos/projector'
require 'app/projections/outstanding_todos/projector'
require 'app/projections/scheduled_todos/projector'
require 'app/projections/movedup_todos/projector'
require 'app/reactors/todo_completed_notifier'

# Configure EventSourcery and our Postgres event store.
Expand Down
18 changes: 18 additions & 0 deletions scripts/request
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,29 @@ command :complete do |c|
end
end

command :moveup do |c|
c.syntax = "#{script_name} Move up [options]"
c.summary = 'Move up a Todo item via the Todo web API'
c.option '-i ID', '--id ID', 'Todo ID'
c.action do |args, options|
todo_id = options.id
unless todo_id
$stderr.puts "Error: you must specify a Todo ID for the new Todo"
$stderr.puts "You can generate one using `#{script_name} uuid`."
exit 1
end
payload = slice_and_compact(options.default)
puts "Moving up todo [#{todo_id}]: #{payload}"
Request.call(:put, "/todo/#{todo_id}/moveup", payload)
end
end

command :list do |c|
LISTS = [
'outstanding',
'scheduled',
'completed',
'movedup'
]

c.syntax = "#{script_name} list [options]"
Expand Down