Skip to content

Commit

Permalink
implement todo update command
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAlexander committed May 9, 2022
1 parent 488627b commit 05b8507
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/todo_backend/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ defmodule TodoBackend.Router do
alias TodoBackend.Todos.Aggregates.Todo
alias TodoBackend.Todos.Commands.CreateTodo
alias TodoBackend.Todos.Commands.DeleteTodo
alias TodoBackend.Todos.Commands.UpdateTodo

dispatch([CreateTodo], to: Todo, identity: :uuid)
dispatch([DeleteTodo], to: Todo, identity: :uuid)
dispatch([UpdateTodo], to: Todo, identity: :uuid)
end
43 changes: 43 additions & 0 deletions lib/todo_backend/todos/aggregates/todo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ defmodule TodoBackend.Todos.Aggregates.Todo do

alias TodoBackend.Todos.Commands.CreateTodo
alias TodoBackend.Todos.Commands.DeleteTodo
alias TodoBackend.Todos.Commands.UpdateTodo

alias TodoBackend.Todos.Events.TodoCreated
alias TodoBackend.Todos.Events.TodoDeleted
alias TodoBackend.Todos.Events.TodoCompleted
alias TodoBackend.Todos.Events.TodoUncompleted
alias TodoBackend.Todos.Events.TodoTitleUpdated
alias TodoBackend.Todos.Events.TodoOrderUpdated

def execute(%Todo{uuid: nil}, %CreateTodo{} = create) do
%TodoCreated{
Expand All @@ -27,6 +32,28 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
%TodoDeleted{uuid: uuid}
end

# TODO: validate
def execute(%Todo{} = todo, %UpdateTodo{} = update) do
completion_command =
if todo.completed != update.completed and not is_nil(update.completed) do
if update.completed do
%TodoCompleted{uuid: todo.uuid}
else
%TodoUncompleted{uuid: todo.uuid}
end
end

title_command =
if todo.title != update.title and not is_nil(update.title),
do: %TodoTitleUpdated{uuid: todo.uuid, title: update.title}

order_command =
if todo.order != update.order and not is_nil(update.order),
do: %TodoOrderUpdated{uuid: todo.uuid, order: update.order}

[completion_command, title_command, order_command] |> Enum.filter(&Function.identity/1)
end

def apply(%Todo{} = todo, %TodoCreated{} = created) do
%Todo{
todo
Expand All @@ -37,6 +64,22 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
}
end

def apply(%Todo{} = todo, %TodoCompleted{}) do
%Todo{todo | completed: true}
end

def apply(%Todo{} = todo, %TodoUncompleted{}) do
%Todo{todo | completed: false}
end

def apply(%Todo{} = todo, %TodoTitleUpdated{title: title}) do
%Todo{todo | title: title}
end

def apply(%Todo{} = todo, %TodoOrderUpdated{order: order}) do
%Todo{todo | order: order}
end

def apply(%Todo{uuid: uuid}, %TodoDeleted{uuid: uuid}) do
nil
end
Expand Down
8 changes: 8 additions & 0 deletions lib/todo_backend/todos/commands/update_todo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule TodoBackend.Todos.Commands.UpdateTodo do
defstruct [
:uuid,
:title,
:completed,
:order
]
end
6 changes: 6 additions & 0 deletions lib/todo_backend/todos/events/todo_completed.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule TodoBackend.Todos.Events.TodoCompleted do
@derive Jason.Encoder
defstruct [
:uuid
]
end
7 changes: 7 additions & 0 deletions lib/todo_backend/todos/events/todo_order_updated.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule TodoBackend.Todos.Events.TodoOrderUpdated do
@derive Jason.Encoder
defstruct [
:uuid,
:order
]
end
7 changes: 7 additions & 0 deletions lib/todo_backend/todos/events/todo_title_updated.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule TodoBackend.Todos.Events.TodoTitleUpdated do
@derive Jason.Encoder
defstruct [
:uuid,
:title
]
end
6 changes: 6 additions & 0 deletions lib/todo_backend/todos/events/todo_uncompleted.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule TodoBackend.Todos.Events.TodoUncompleted do
@derive Jason.Encoder
defstruct [
:uuid
]
end

0 comments on commit 05b8507

Please sign in to comment.