Skip to content

Commit

Permalink
implement todo deletion command
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAlexander committed May 9, 2022
1 parent dc6e9fb commit 488627b
Show file tree
Hide file tree
Showing 4 changed files with 24 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 @@ -3,6 +3,8 @@ defmodule TodoBackend.Router do

alias TodoBackend.Todos.Aggregates.Todo
alias TodoBackend.Todos.Commands.CreateTodo
alias TodoBackend.Todos.Commands.DeleteTodo

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

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

alias TodoBackend.Todos.Events.TodoCreated
alias TodoBackend.Todos.Events.TodoDeleted

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

def execute(%Todo{uuid: uuid}, %DeleteTodo{uuid: uuid}) do
%TodoDeleted{uuid: uuid}
end

def apply(%Todo{} = todo, %TodoCreated{} = created) do
%Todo{
todo
Expand All @@ -30,3 +36,8 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
order: created.order
}
end

def apply(%Todo{uuid: uuid}, %TodoDeleted{uuid: uuid}) do
nil
end
end
5 changes: 5 additions & 0 deletions lib/todo_backend/todos/commands/delete_todo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule TodoBackend.Todos.Commands.DeleteTodo do
defstruct [
:uuid
]
end
6 changes: 6 additions & 0 deletions lib/todo_backend/todos/events/todo_deleted.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule TodoBackend.Todos.Events.TodoDeleted do
@derive Jason.Encoder
defstruct [
:uuid
]
end

0 comments on commit 488627b

Please sign in to comment.