add pipelines

This commit is contained in:
shibao 2022-07-25 20:12:11 -04:00
parent 3347b26256
commit 0fca0ead12
13 changed files with 569 additions and 0 deletions

104
lib/memex/pipelines.ex Normal file
View File

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

View File

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

View File

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

View File

@ -0,0 +1,28 @@
<div>
<h2><%= @title %></h2>
<.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 %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

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

View File

@ -0,0 +1,43 @@
<h1>Listing Pipelines</h1>
<%= 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)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="pipelines">
<%= for pipeline <- @pipelines do %>
<tr id={"pipeline-#{pipeline.id}"}>
<td><%= pipeline.title %></td>
<td><%= pipeline.description %></td>
<td><%= pipeline.visibility %></td>
<td>
<span><%= live_redirect "Show", to: Routes.pipeline_show_path(@socket, :show, pipeline) %></span>
<span><%= live_patch "Edit", to: Routes.pipeline_index_path(@socket, :edit, pipeline) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: pipeline.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Pipeline", to: Routes.pipeline_index_path(@socket, :new) %></span>

View File

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

View File

@ -0,0 +1,36 @@
<h1>Show Pipeline</h1>
<%= 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)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @pipeline.title %>
</li>
<li>
<strong>Description:</strong>
<%= @pipeline.description %>
</li>
<li>
<strong>Visibility:</strong>
<%= @pipeline.visibility %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.pipeline_show_path(@socket, :edit, @pipeline), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.pipeline_index_path(@socket, :index) %></span>

View File

@ -63,6 +63,8 @@ defmodule MemexWeb.Router do
live "/contexts", ContextLive.Index, :index live "/contexts", ContextLive.Index, :index
live "/contexts/:id", ContextLive.Show, :show live "/contexts/:id", ContextLive.Show, :show
live "/pipelines", PipelineLive.Index, :index
live "/pipelines/:id", PipelineLive.Show, :show
end end
scope "/", MemexWeb do scope "/", MemexWeb do
@ -76,6 +78,10 @@ defmodule MemexWeb.Router do
live "/contexts/:id/edit", ContextLive.Index, :edit live "/contexts/:id/edit", ContextLive.Index, :edit
live "/contexts/:id/show/edit", ContextLive.Show, :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 get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete delete "/users/settings/:id", UserSettingsController, :delete

View File

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

View File

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

View File

@ -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&#39;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&#39;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&#39;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

View File

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