-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Radiator.Outline.Dispatch do | ||
@moduledoc false | ||
|
||
alias Radiator.Outline.Event | ||
alias Radiator.Outline.EventProducer | ||
|
||
def insert_node(attributes, user_id, event_id) do | ||
"insert_node" | ||
|> Event.build(attributes, user_id, event_id) | ||
|> EventProducer.enqueue() | ||
end | ||
|
||
# TODO | ||
# update_node | ||
# delete_node | ||
# move_node | ||
|
||
# list_node different case, sync call | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Radiator.Outline.Event.InsertNodeEvent do | ||
Check warning on line 1 in lib/radiator/outline/event/insert_node_event.ex GitHub Actions / Build & Test
|
||
defstruct [:event_id, :user_id, :payload] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,46 @@ | ||
defmodule Radiator.Outline.EventConsumer do | ||
Check warning on line 1 in lib/radiator/outline/event_consumer.ex GitHub Actions / Build & Test
|
||
use GenStage | ||
|
||
alias Radiator.Outline | ||
alias Radiator.Outline.EventProducer | ||
alias Radiator.Outline.Event.InsertNodeEvent | ||
|
||
def start_link(opts \\ []) do | ||
GenStage.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def init(_opts) do | ||
options = [] | ||
{:consumer, :event_producer, subscribe_to: [{EventProducer, options}]} | ||
def init(opts \\ [max_demand: 1]) do | ||
{:consumer, :event_producer, subscribe_to: [{EventProducer, opts}]} | ||
end | ||
|
||
def handle_events(events, _from, state) do | ||
IO.inspect(events, label: "EventConsumer handle_events") | ||
|
||
Enum.each(events, fn event -> | ||
process_event(event, state) | ||
IO.inspect(event, label: "EventConsumer handle_events event") | ||
end) | ||
def handle_events([event], _from, state) do | ||
process_event(event) | ||
|
||
{:noreply, [], state} | ||
end | ||
|
||
defp process_event(%InsertNodeEvent{} = event) do | ||
# validate | ||
defp process_event(%InsertNodeEvent{payload: payload} = _event) do | ||
payload | ||
|> Outline.create_node() | ||
|> handle_insert_result() | ||
|
||
# validate | ||
# true-> | ||
# database action: insert node() | ||
# create && persist event (event contains all attributes, user, event_id, timestamps) | ||
# broadcast event (topic: episode_id) | ||
# broadcast node (topic: episode_id) | ||
# false-> | ||
# log error and return error (audit log) | ||
end | ||
|
||
defp handle_result(:ok, event) do | ||
persist_event(event) | ||
broadcast_success(event) | ||
defp handle_insert_result({:ok, node}) do | ||
{:ok, node} | ||
end | ||
|
||
defp handle_result(:error, event) do | ||
broadcast_error(event) | ||
defp handle_insert_result({:error, _error}) do | ||
# log_error_please :-) | ||
|
||
:error | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Radiator.Outline.DispatchTest do | ||
use Radiator.DataCase | ||
|
||
import Radiator.AccountsFixtures | ||
import Radiator.PodcastFixtures | ||
|
||
# alias Radiator.Outline | ||
alias Radiator.Outline.Dispatch | ||
alias Radiator.Outline.Node | ||
|
||
describe "outline dispatch" do | ||
setup do | ||
%{episode: episode_fixture()} | ||
end | ||
|
||
test "insert_node does WHAT?", %{episode: episode} do | ||
user = user_fixture() | ||
|
||
node = %Node{episode_id: episode.id, content: "something very special 1!1"} | ||
attributes = Map.from_struct(node) | ||
|
||
event_id = Ecto.UUID.generate() | ||
|
||
Dispatch.insert_node(attributes, user.id, event_id) | ||
|
||
# _inserted_node = | ||
# Outline.list_nodes() | ||
# |> Enum.find(&(&1.content == "something very special 1!1")) | ||
|
||
# assert inserted_node.episode_id == node.episode_id | ||
# assert inserted_node.content == node.content | ||
end | ||
end | ||
end |