From 47577c89e08270abc28f6d43e5bde686fd930f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Tue, 7 May 2024 22:22:20 +0200 Subject: [PATCH] start with protocal for events todo * find a better name * missing user id for events * implement for all events * want to store also old content, ids etc. ? --- lib/radiator/event_store.ex | 9 +++- lib/radiator/event_store/event_data.ex | 3 ++ lib/radiator/outline/event/abstract_event.ex | 43 +++++++++++++++++++ .../event/node_content_changed_event.ex | 2 +- .../outline/event/node_deleted_event.ex | 1 + .../outline/event/node_moved_event.ex | 1 + lib/radiator/outline/event_consumer.ex | 2 +- lib/radiator_web/live/episode_live/index.ex | 4 +- test/radiator/outline/event_consumer_test.exs | 2 +- 9 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 lib/radiator/outline/event/abstract_event.ex diff --git a/lib/radiator/event_store.ex b/lib/radiator/event_store.ex index eb732d4a..d9baae21 100644 --- a/lib/radiator/event_store.ex +++ b/lib/radiator/event_store.ex @@ -4,10 +4,17 @@ defmodule Radiator.EventStore do """ alias Radiator.EventStore.EventData + alias Radiator.Outline.Event.AbstractEvent alias Radiator.Repo def persist_event(event) do - # persist event + {:ok, _stored_event} = + create_event_data(%{ + data: AbstractEvent.payload(event), + event_type: AbstractEvent.event_type(event), + uuid: event.event_id + }) + event end diff --git a/lib/radiator/event_store/event_data.ex b/lib/radiator/event_store/event_data.ex index 81578f62..e85ae4b7 100644 --- a/lib/radiator/event_store/event_data.ex +++ b/lib/radiator/event_store/event_data.ex @@ -1,4 +1,7 @@ defmodule Radiator.EventStore.EventData do + @moduledoc """ + EventData schema represents a persistend event. + """ use Ecto.Schema import Ecto.Changeset diff --git a/lib/radiator/outline/event/abstract_event.ex b/lib/radiator/outline/event/abstract_event.ex new file mode 100644 index 00000000..8c206b34 --- /dev/null +++ b/lib/radiator/outline/event/abstract_event.ex @@ -0,0 +1,43 @@ +defprotocol Radiator.Outline.Event.AbstractEvent do + def payload(event) + def event_type(event) +end + +alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeInsertedEvent} + +defimpl Radiator.Outline.Event.AbstractEvent, for: NodeInsertedEvent do + def payload(event) do + event.node + end + + def event_type(_event), do: "NodeInsertedEvent" +end + +defimpl Radiator.Outline.Event.AbstractEvent, for: NodeContentChangedEvent do + def payload(event) do + %{node_id: event.node_id, content: event.content} + end + + def event_type(_event), do: "NodeInsertedEvent" +end + +defimpl Radiator.Outline.Event.AbstractEvent, for: NodeDeletedEvent do + def payload(event) do + event.node_id + end + + def event_type(_event), do: "NodeDeletedEvent" +end + +defimpl Radiator.Outline.Event.AbstractEvent, for: NodeMovedEvent do + def payload(event) do + %{node_id: event.node_id, parent_id: event.parent_id, prev_id: event.prev_id} + end + + def event_type(_event), do: "NodeInsertedEvent" +end + +# defmodule Radiator.Outline.Event.AbstractEvent do +# @callback payload(event :: any()) :: map() +# @callback event_type(event :: any()) :: String.t() +# end diff --git a/lib/radiator/outline/event/node_content_changed_event.ex b/lib/radiator/outline/event/node_content_changed_event.ex index 7059eb71..30e5c435 100644 --- a/lib/radiator/outline/event/node_content_changed_event.ex +++ b/lib/radiator/outline/event/node_content_changed_event.ex @@ -1,5 +1,5 @@ defmodule Radiator.Outline.Event.NodeContentChangedEvent do @moduledoc false - defstruct [:event_id, :node] + defstruct [:event_id, :node_id, :content] end diff --git a/lib/radiator/outline/event/node_deleted_event.ex b/lib/radiator/outline/event/node_deleted_event.ex index 9eaa521a..2bd1012e 100644 --- a/lib/radiator/outline/event/node_deleted_event.ex +++ b/lib/radiator/outline/event/node_deleted_event.ex @@ -1,3 +1,4 @@ defmodule Radiator.Outline.Event.NodeDeletedEvent do @moduledoc false + defstruct [:event_id, :node_id] end diff --git a/lib/radiator/outline/event/node_moved_event.ex b/lib/radiator/outline/event/node_moved_event.ex index 797a0c2c..01636cac 100644 --- a/lib/radiator/outline/event/node_moved_event.ex +++ b/lib/radiator/outline/event/node_moved_event.ex @@ -1,3 +1,4 @@ defmodule Radiator.Outline.Event.NodeMovedEvent do @moduledoc false + defstruct [:event_id, :node_id, :parent_id, :prev_id] end diff --git a/lib/radiator/outline/event_consumer.ex b/lib/radiator/outline/event_consumer.ex index ddfcb710..8ea8a6f5 100644 --- a/lib/radiator/outline/event_consumer.ex +++ b/lib/radiator/outline/event_consumer.ex @@ -50,7 +50,7 @@ defmodule Radiator.Outline.EventConsumer do end def handle_change_node_content_result({:ok, node}, command) do - %NodeContentChangedEvent{node: node, event_id: command.event_id} + %NodeContentChangedEvent{node_id: node.id, event_id: command.event_id} |> EventStore.persist_event() |> Dispatch.broadcast() diff --git a/lib/radiator_web/live/episode_live/index.ex b/lib/radiator_web/live/episode_live/index.ex index f9e9651c..c5206912 100644 --- a/lib/radiator_web/live/episode_live/index.ex +++ b/lib/radiator_web/live/episode_live/index.ex @@ -87,9 +87,9 @@ defmodule RadiatorWeb.EpisodeLive.Index do |> reply(:noreply) end - def handle_info(%NodeContentChangedEvent{node: node, event_id: event_id}, socket) do + def handle_info(%NodeContentChangedEvent{node_id: node_id, event_id: event_id}, socket) do socket - |> push_event("update", %{node: node, event_id: event_id}) + |> push_event("update", %{node_id: node_id, event_id: event_id}) |> reply(:noreply) end diff --git a/test/radiator/outline/event_consumer_test.exs b/test/radiator/outline/event_consumer_test.exs index 5324e8b8..6269cdb7 100644 --- a/test/radiator/outline/event_consumer_test.exs +++ b/test/radiator/outline/event_consumer_test.exs @@ -21,7 +21,7 @@ defmodule Radiator.Outline.EventConsumerTest do } user_id = "user_id" - event_id = "event_id" + event_id = Ecto.UUID.generate() command = Command.build("insert_node", attributes, user_id, event_id) EventConsumer.handle_events([command], 0, nil)