Skip to content

Commit

Permalink
Merge pull request #500 from podlove/nodes_creator
Browse files Browse the repository at this point in the history
feat: outline_nodes can have a creator
  • Loading branch information
sorax committed Nov 28, 2023
2 parents 6f4dd11 + ca38b90 commit 4afe2a1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
15 changes: 13 additions & 2 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ defmodule Radiator.Outline do
"""
def list_nodes do
Repo.all(Node)
Node
|> order_by(desc: :updated_at)
|> Repo.all()
end

@doc """
Expand All @@ -35,7 +37,10 @@ defmodule Radiator.Outline do
** (Ecto.NoResultsError)
"""
def get_node!(id), do: Repo.get!(Node, id)
def get_node!(id) do
Node
|> Repo.get!(id)
end

@doc """
Creates a node.
Expand All @@ -55,6 +60,12 @@ defmodule Radiator.Outline do
|> Repo.insert()
end

def create_node(attrs, %{id: id}) do
%Node{creator_id: id}
|> Node.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a node.
Expand Down
13 changes: 10 additions & 3 deletions lib/radiator/outline/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ defmodule Radiator.Outline.Node do
@primary_key {:uuid, :binary_id, autogenerate: true}
schema "outline_nodes" do
field :content, :string
field :creator_id, :integer

timestamps(type: :utc_datetime)
end

@fields [
@required_fields [
:content
]

@optional_fields [
:creator_id
]

@all_fields @optional_fields ++ @required_fields

@doc false
def changeset(node, attrs) do
node
|> cast(attrs, @fields)
|> cast(attrs, @all_fields)
|> update_change(:content, &trim/1)
|> validate_required(@fields)
|> validate_required(@required_fields)
end

defp trim(content) when is_binary(content), do: String.trim(content)
Expand Down
3 changes: 2 additions & 1 deletion lib/radiator_web/live/outline_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule RadiatorWeb.OutlineLive.Index do

@impl true
def handle_event("next", %{"node" => params}, socket) do
{:ok, node} = Outline.create_node(params)
user = socket.assigns.current_user
{:ok, node} = Outline.create_node(params, user)

Endpoint.broadcast(@topic, "inserted", node)

Expand Down
23 changes: 14 additions & 9 deletions lib/radiator_web/live/outline_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
</.focus_wrap>

<ul id="inbox" class="mt-8 list-disc list-inside" phx-update="stream">
<li :for={{dom_id, node} <- @streams.nodes} id={dom_id}>
<%= node.content %>
<.link
phx-click={JS.push("delete", value: %{uuid: node.uuid}) |> hide("##{dom_id}")}
data-confirm="Delete?"
>
<.icon name="hero-x-circle" class="w-5 h-5" />
<div class="sr-only">Delete</div>
</.link>
<li :for={{dom_id, node} <- @streams.nodes} id={dom_id} class="my-2">
<div>
<%= node.content %>
<.link
phx-click={JS.push("delete", value: %{uuid: node.uuid}) |> hide("##{dom_id}")}
data-confirm="Delete?"
>
<.icon name="hero-x-circle" class="w-5 h-5" />
<div class="sr-only">Delete</div>
</.link>
</div>
<div class="pl-4 text-xs italic">
Creator: <%= node.creator_id %> | Last Update: <%= node.updated_at %>
</div>
</li>
</ul>
</section>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Radiator.Repo.Migrations.AddCreatorToOutlineNodes do
use Ecto.Migration

def change do
alter table(:outline_nodes) do
add :creator_id, :integer
end
end
end
9 changes: 9 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ defmodule Radiator.OutlineTest do
assert node.content == "some content"
end

test "create_node/1 can have a creator" do
user = %{id: 2}
valid_attrs = %{content: "some content"}

assert {:ok, %Node{} = node} = Outline.create_node(valid_attrs, user)
assert node.content == "some content"
assert node.creator_id == user.id
end

test "create_node/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Outline.create_node(@invalid_attrs)
end
Expand Down

0 comments on commit 4afe2a1

Please sign in to comment.