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 @@