add pipelines

This commit is contained in:
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/: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