diff --git a/lib/memex/pipelines.ex b/lib/memex/pipelines.ex new file mode 100644 index 0000000..7300755 --- /dev/null +++ b/lib/memex/pipelines.ex @@ -0,0 +1,104 @@ +defmodule Memex.Pipelines do + @moduledoc """ + The Pipelines context. + """ + + import Ecto.Query, warn: false + alias Memex.Repo + + alias Memex.Pipelines.Pipeline + + @doc """ + Returns the list of pipelines. + + ## Examples + + iex> list_pipelines() + [%Pipeline{}, ...] + + """ + def list_pipelines do + Repo.all(Pipeline) + end + + @doc """ + Gets a single pipeline. + + Raises `Ecto.NoResultsError` if the Pipeline does not exist. + + ## Examples + + iex> get_pipeline!(123) + %Pipeline{} + + iex> get_pipeline!(456) + ** (Ecto.NoResultsError) + + """ + def get_pipeline!(id), do: Repo.get!(Pipeline, id) + + @doc """ + Creates a pipeline. + + ## Examples + + iex> create_pipeline(%{field: value}) + {:ok, %Pipeline{}} + + iex> create_pipeline(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_pipeline(attrs \\ %{}) do + %Pipeline{} + |> Pipeline.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a pipeline. + + ## Examples + + iex> update_pipeline(pipeline, %{field: new_value}) + {:ok, %Pipeline{}} + + iex> update_pipeline(pipeline, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_pipeline(%Pipeline{} = pipeline, attrs) do + pipeline + |> Pipeline.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a pipeline. + + ## Examples + + iex> delete_pipeline(pipeline) + {:ok, %Pipeline{}} + + iex> delete_pipeline(pipeline) + {:error, %Ecto.Changeset{}} + + """ + def delete_pipeline(%Pipeline{} = pipeline) do + Repo.delete(pipeline) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking pipeline changes. + + ## Examples + + iex> change_pipeline(pipeline) + %Ecto.Changeset{data: %Pipeline{}} + + """ + def change_pipeline(%Pipeline{} = pipeline, attrs \\ %{}) do + Pipeline.changeset(pipeline, attrs) + end +end diff --git a/lib/memex/pipelines/pipeline.ex b/lib/memex/pipelines/pipeline.ex new file mode 100644 index 0000000..37bae09 --- /dev/null +++ b/lib/memex/pipelines/pipeline.ex @@ -0,0 +1,21 @@ +defmodule Memex.Pipelines.Pipeline do + use Ecto.Schema + import Ecto.Changeset + + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + schema "pipelines" do + field :description, :string + field :title, :string + field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] + + timestamps() + end + + @doc false + def changeset(pipeline, attrs) do + pipeline + |> cast(attrs, [:title, :description, :visibility]) + |> validate_required([:title, :description, :visibility]) + end +end diff --git a/lib/memex_web/live/pipeline_live/form_component.ex b/lib/memex_web/live/pipeline_live/form_component.ex new file mode 100644 index 0000000..addcd0b --- /dev/null +++ b/lib/memex_web/live/pipeline_live/form_component.ex @@ -0,0 +1,55 @@ +defmodule MemexWeb.PipelineLive.FormComponent do + use MemexWeb, :live_component + + alias Memex.Pipelines + + @impl true + def update(%{pipeline: pipeline} = assigns, socket) do + changeset = Pipelines.change_pipeline(pipeline) + + {:ok, + socket + |> assign(assigns) + |> assign(:changeset, changeset)} + end + + @impl true + def handle_event("validate", %{"pipeline" => pipeline_params}, socket) do + changeset = + socket.assigns.pipeline + |> Pipelines.change_pipeline(pipeline_params) + |> Map.put(:action, :validate) + + {:noreply, assign(socket, :changeset, changeset)} + end + + def handle_event("save", %{"pipeline" => pipeline_params}, socket) do + save_pipeline(socket, socket.assigns.action, pipeline_params) + end + + defp save_pipeline(socket, :edit, pipeline_params) do + case Pipelines.update_pipeline(socket.assigns.pipeline, pipeline_params) do + {:ok, _pipeline} -> + {:noreply, + socket + |> put_flash(:info, "Pipeline updated successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, :changeset, changeset)} + end + end + + defp save_pipeline(socket, :new, pipeline_params) do + case Pipelines.create_pipeline(pipeline_params) do + {:ok, _pipeline} -> + {:noreply, + socket + |> put_flash(:info, "Pipeline created successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, changeset: changeset)} + end + end +end diff --git a/lib/memex_web/live/pipeline_live/form_component.html.heex b/lib/memex_web/live/pipeline_live/form_component.html.heex new file mode 100644 index 0000000..077c3e9 --- /dev/null +++ b/lib/memex_web/live/pipeline_live/form_component.html.heex @@ -0,0 +1,28 @@ +
+

<%= @title %>

+ + <.form + let={f} + for={@changeset} + id="pipeline-form" + phx-target={@myself} + phx-change="validate" + phx-submit="save"> + + <%= label f, :title %> + <%= text_input f, :title %> + <%= error_tag f, :title %> + + <%= label f, :description %> + <%= textarea f, :description %> + <%= error_tag f, :description %> + + <%= label f, :visibility %> + <%= select f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility), prompt: "Choose a value" %> + <%= error_tag f, :visibility %> + +
+ <%= submit "Save", phx_disable_with: "Saving..." %> +
+ +
diff --git a/lib/memex_web/live/pipeline_live/index.ex b/lib/memex_web/live/pipeline_live/index.ex new file mode 100644 index 0000000..f9b6b4f --- /dev/null +++ b/lib/memex_web/live/pipeline_live/index.ex @@ -0,0 +1,46 @@ +defmodule MemexWeb.PipelineLive.Index do + use MemexWeb, :live_view + + alias Memex.Pipelines + alias Memex.Pipelines.Pipeline + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, :pipelines, list_pipelines())} + end + + @impl true + def handle_params(params, _url, socket) do + {:noreply, apply_action(socket, socket.assigns.live_action, params)} + end + + defp apply_action(socket, :edit, %{"id" => id}) do + socket + |> assign(:page_title, "Edit Pipeline") + |> assign(:pipeline, Pipelines.get_pipeline!(id)) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Pipeline") + |> assign(:pipeline, %Pipeline{}) + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Listing Pipelines") + |> assign(:pipeline, nil) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + pipeline = Pipelines.get_pipeline!(id) + {:ok, _} = Pipelines.delete_pipeline(pipeline) + + {:noreply, assign(socket, :pipelines, list_pipelines())} + end + + defp list_pipelines do + Pipelines.list_pipelines() + end +end diff --git a/lib/memex_web/live/pipeline_live/index.html.heex b/lib/memex_web/live/pipeline_live/index.html.heex new file mode 100644 index 0000000..55559e3 --- /dev/null +++ b/lib/memex_web/live/pipeline_live/index.html.heex @@ -0,0 +1,43 @@ +

Listing Pipelines

+ +<%= if @live_action in [:new, :edit] do %> + <.modal return_to={Routes.pipeline_index_path(@socket, :index)}> + <.live_component + module={MemexWeb.PipelineLive.FormComponent} + id={@pipeline.id || :new} + title={@page_title} + action={@live_action} + pipeline={@pipeline} + return_to={Routes.pipeline_index_path(@socket, :index)} + /> + +<% end %> + + + + + + + + + + + + + <%= for pipeline <- @pipelines do %> + + + + + + + + <% end %> + +
TitleDescriptionVisibility
<%= pipeline.title %><%= pipeline.description %><%= pipeline.visibility %> + <%= live_redirect "Show", to: Routes.pipeline_show_path(@socket, :show, pipeline) %> + <%= live_patch "Edit", to: Routes.pipeline_index_path(@socket, :edit, pipeline) %> + <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: pipeline.id, data: [confirm: "Are you sure?"] %> +
+ +<%= live_patch "New Pipeline", to: Routes.pipeline_index_path(@socket, :new) %> diff --git a/lib/memex_web/live/pipeline_live/show.ex b/lib/memex_web/live/pipeline_live/show.ex new file mode 100644 index 0000000..794403a --- /dev/null +++ b/lib/memex_web/live/pipeline_live/show.ex @@ -0,0 +1,21 @@ +defmodule MemexWeb.PipelineLive.Show do + use MemexWeb, :live_view + + alias Memex.Pipelines + + @impl true + def mount(_params, _session, socket) do + {:ok, socket} + end + + @impl true + def handle_params(%{"id" => id}, _, socket) do + {:noreply, + socket + |> assign(:page_title, page_title(socket.assigns.live_action)) + |> assign(:pipeline, Pipelines.get_pipeline!(id))} + end + + defp page_title(:show), do: "Show Pipeline" + defp page_title(:edit), do: "Edit Pipeline" +end diff --git a/lib/memex_web/live/pipeline_live/show.html.heex b/lib/memex_web/live/pipeline_live/show.html.heex new file mode 100644 index 0000000..daee582 --- /dev/null +++ b/lib/memex_web/live/pipeline_live/show.html.heex @@ -0,0 +1,36 @@ +

Show Pipeline

+ +<%= if @live_action in [:edit] do %> + <.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}> + <.live_component + module={MemexWeb.PipelineLive.FormComponent} + id={@pipeline.id} + title={@page_title} + action={@live_action} + pipeline={@pipeline} + return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)} + /> + +<% end %> + + + +<%= live_patch "Edit", to: Routes.pipeline_show_path(@socket, :edit, @pipeline), class: "button" %> | +<%= live_redirect "Back", to: Routes.pipeline_index_path(@socket, :index) %> diff --git a/lib/memex_web/router.ex b/lib/memex_web/router.ex index b34c5d8..3ddf5de 100644 --- a/lib/memex_web/router.ex +++ b/lib/memex_web/router.ex @@ -63,6 +63,8 @@ defmodule MemexWeb.Router do live "/contexts", ContextLive.Index, :index live "/contexts/:id", ContextLive.Show, :show + live "/pipelines", PipelineLive.Index, :index + live "/pipelines/:id", PipelineLive.Show, :show end scope "/", MemexWeb do @@ -76,6 +78,10 @@ defmodule MemexWeb.Router do live "/contexts/:id/edit", ContextLive.Index, :edit live "/contexts/:id/show/edit", ContextLive.Show, :edit + live "/pipelines/new", PipelineLive.Index, :new + live "/pipelines/:id/edit", PipelineLive.Index, :edit + live "/pipelines/:id/show/edit", PipelineLive.Show, :edit + get "/users/settings", UserSettingsController, :edit put "/users/settings", UserSettingsController, :update delete "/users/settings/:id", UserSettingsController, :delete diff --git a/priv/repo/migrations/20220726001146_create_pipelines.exs b/priv/repo/migrations/20220726001146_create_pipelines.exs new file mode 100644 index 0000000..ca21f31 --- /dev/null +++ b/priv/repo/migrations/20220726001146_create_pipelines.exs @@ -0,0 +1,14 @@ +defmodule Memex.Repo.Migrations.CreatePipelines do + use Ecto.Migration + + def change do + create table(:pipelines, primary_key: false) do + add :id, :binary_id, primary_key: true + add :title, :string + add :description, :text + add :visibility, :string + + timestamps() + end + end +end diff --git a/test/memex/pipelines_test.exs b/test/memex/pipelines_test.exs new file mode 100644 index 0000000..18a4932 --- /dev/null +++ b/test/memex/pipelines_test.exs @@ -0,0 +1,63 @@ +defmodule Memex.PipelinesTest do + use Memex.DataCase + + alias Memex.Pipelines + + describe "pipelines" do + alias Memex.Pipelines.Pipeline + + import Memex.PipelinesFixtures + + @invalid_attrs %{description: nil, title: nil, visibility: nil} + + test "list_pipelines/0 returns all pipelines" do + pipeline = pipeline_fixture() + assert Pipelines.list_pipelines() == [pipeline] + end + + test "get_pipeline!/1 returns the pipeline with given id" do + pipeline = pipeline_fixture() + assert Pipelines.get_pipeline!(pipeline.id) == pipeline + end + + test "create_pipeline/1 with valid data creates a pipeline" do + valid_attrs = %{description: "some description", title: "some title", visibility: :public} + + assert {:ok, %Pipeline{} = pipeline} = Pipelines.create_pipeline(valid_attrs) + assert pipeline.description == "some description" + assert pipeline.title == "some title" + assert pipeline.visibility == :public + end + + test "create_pipeline/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Pipelines.create_pipeline(@invalid_attrs) + end + + test "update_pipeline/2 with valid data updates the pipeline" do + pipeline = pipeline_fixture() + update_attrs = %{description: "some updated description", title: "some updated title", visibility: :private} + + assert {:ok, %Pipeline{} = pipeline} = Pipelines.update_pipeline(pipeline, update_attrs) + assert pipeline.description == "some updated description" + assert pipeline.title == "some updated title" + assert pipeline.visibility == :private + end + + test "update_pipeline/2 with invalid data returns error changeset" do + pipeline = pipeline_fixture() + assert {:error, %Ecto.Changeset{}} = Pipelines.update_pipeline(pipeline, @invalid_attrs) + assert pipeline == Pipelines.get_pipeline!(pipeline.id) + end + + test "delete_pipeline/1 deletes the pipeline" do + pipeline = pipeline_fixture() + assert {:ok, %Pipeline{}} = Pipelines.delete_pipeline(pipeline) + assert_raise Ecto.NoResultsError, fn -> Pipelines.get_pipeline!(pipeline.id) end + end + + test "change_pipeline/1 returns a pipeline changeset" do + pipeline = pipeline_fixture() + assert %Ecto.Changeset{} = Pipelines.change_pipeline(pipeline) + end + end +end diff --git a/test/memex_web/live/pipeline_live_test.exs b/test/memex_web/live/pipeline_live_test.exs new file mode 100644 index 0000000..b996b9b --- /dev/null +++ b/test/memex_web/live/pipeline_live_test.exs @@ -0,0 +1,110 @@ +defmodule MemexWeb.PipelineLiveTest do + use MemexWeb.ConnCase + + import Phoenix.LiveViewTest + import Memex.PipelinesFixtures + + @create_attrs %{description: "some description", title: "some title", visibility: :public} + @update_attrs %{description: "some updated description", title: "some updated title", visibility: :private} + @invalid_attrs %{description: nil, title: nil, visibility: nil} + + defp create_pipeline(_) do + pipeline = pipeline_fixture() + %{pipeline: pipeline} + end + + describe "Index" do + setup [:create_pipeline] + + test "lists all pipelines", %{conn: conn, pipeline: pipeline} do + {:ok, _index_live, html} = live(conn, Routes.pipeline_index_path(conn, :index)) + + assert html =~ "Listing Pipelines" + assert html =~ pipeline.description + end + + test "saves new pipeline", %{conn: conn} do + {:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index)) + + assert index_live |> element("a", "New Pipeline") |> render_click() =~ + "New Pipeline" + + assert_patch(index_live, Routes.pipeline_index_path(conn, :new)) + + assert index_live + |> form("#pipeline-form", pipeline: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#pipeline-form", pipeline: @create_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.pipeline_index_path(conn, :index)) + + assert html =~ "Pipeline created successfully" + assert html =~ "some description" + end + + test "updates pipeline in listing", %{conn: conn, pipeline: pipeline} do + {:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index)) + + assert index_live |> element("#pipeline-#{pipeline.id} a", "Edit") |> render_click() =~ + "Edit Pipeline" + + assert_patch(index_live, Routes.pipeline_index_path(conn, :edit, pipeline)) + + assert index_live + |> form("#pipeline-form", pipeline: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#pipeline-form", pipeline: @update_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.pipeline_index_path(conn, :index)) + + assert html =~ "Pipeline updated successfully" + assert html =~ "some updated description" + end + + test "deletes pipeline in listing", %{conn: conn, pipeline: pipeline} do + {:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index)) + + assert index_live |> element("#pipeline-#{pipeline.id} a", "Delete") |> render_click() + refute has_element?(index_live, "#pipeline-#{pipeline.id}") + end + end + + describe "Show" do + setup [:create_pipeline] + + test "displays pipeline", %{conn: conn, pipeline: pipeline} do + {:ok, _show_live, html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline)) + + assert html =~ "Show Pipeline" + assert html =~ pipeline.description + end + + test "updates pipeline within modal", %{conn: conn, pipeline: pipeline} do + {:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline)) + + assert show_live |> element("a", "Edit") |> render_click() =~ + "Edit Pipeline" + + assert_patch(show_live, Routes.pipeline_show_path(conn, :edit, pipeline)) + + assert show_live + |> form("#pipeline-form", pipeline: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + show_live + |> form("#pipeline-form", pipeline: @update_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.pipeline_show_path(conn, :show, pipeline)) + + assert html =~ "Pipeline updated successfully" + assert html =~ "some updated description" + end + end +end diff --git a/test/support/fixtures/pipelines_fixtures.ex b/test/support/fixtures/pipelines_fixtures.ex new file mode 100644 index 0000000..f85628b --- /dev/null +++ b/test/support/fixtures/pipelines_fixtures.ex @@ -0,0 +1,22 @@ +defmodule Memex.PipelinesFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Memex.Pipelines` context. + """ + + @doc """ + Generate a pipeline. + """ + def pipeline_fixture(attrs \\ %{}) do + {:ok, pipeline} = + attrs + |> Enum.into(%{ + description: "some description", + title: "some title", + visibility: :public + }) + |> Memex.Pipelines.create_pipeline() + + pipeline + end +end