diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index 6f6e6ffe..da7d6e4c 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -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 """ @@ -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. @@ -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. diff --git a/lib/radiator/outline/node.ex b/lib/radiator/outline/node.ex index 590ce8e0..5e9f2ea2 100644 --- a/lib/radiator/outline/node.ex +++ b/lib/radiator/outline/node.ex @@ -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) diff --git a/lib/radiator_web/live/outline_live/index.ex b/lib/radiator_web/live/outline_live/index.ex index 012c6b2c..afc1b4a5 100644 --- a/lib/radiator_web/live/outline_live/index.ex +++ b/lib/radiator_web/live/outline_live/index.ex @@ -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) diff --git a/lib/radiator_web/live/outline_live/index.html.heex b/lib/radiator_web/live/outline_live/index.html.heex index 51752780..b685652e 100644 --- a/lib/radiator_web/live/outline_live/index.html.heex +++ b/lib/radiator_web/live/outline_live/index.html.heex @@ -15,15 +15,20 @@ diff --git a/priv/repo/migrations/20231125210004_add_creator_to_outline_nodes.exs b/priv/repo/migrations/20231125210004_add_creator_to_outline_nodes.exs new file mode 100644 index 00000000..c6db9699 --- /dev/null +++ b/priv/repo/migrations/20231125210004_add_creator_to_outline_nodes.exs @@ -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 diff --git a/test/radiator/outline_test.exs b/test/radiator/outline_test.exs index 80d1c2a1..3b09125c 100644 --- a/test/radiator/outline_test.exs +++ b/test/radiator/outline_test.exs @@ -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