frenzy/lib/frenzy_web/controllers/pipeline_controller.ex

160 lines
4.3 KiB
Elixir
Raw Normal View History

2019-07-20 23:08:56 +00:00
defmodule FrenzyWeb.PipelineController do
use FrenzyWeb, :controller
alias Frenzy.{Repo, Pipeline}
2019-07-20 23:08:56 +00:00
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
import Ecto.Query
plug :user_owns_pipeline
2019-07-20 23:08:56 +00:00
defp user_owns_pipeline(%Plug.Conn{path_params: %{"id" => pipeline_id}} = conn, _opts) do
2019-07-20 23:08:56 +00:00
user = conn.assigns[:user]
pipeline = Repo.get(Pipeline, pipeline_id)
2019-07-20 23:08:56 +00:00
if pipeline.user_id == user.id do
2019-07-20 23:08:56 +00:00
conn
|> assign(:pipeline, pipeline)
2019-07-20 23:08:56 +00:00
else
conn
|> put_flash(:error, "You do not have permission to access that resource.")
|> redirect(to: Routes.group_path(Endpoint, :index))
|> halt()
end
end
defp user_owns_pipeline(conn, _opts), do: conn
2019-07-20 23:08:56 +00:00
def index(conn, _params) do
user = conn.assigns[:user]
2019-07-20 23:08:56 +00:00
pipelines = Repo.all(from p in Pipeline, where: p.user_id == ^user.id, preload: [:feeds])
2019-07-20 23:08:56 +00:00
render(conn, "index.html", %{pipelines: pipelines})
2019-07-20 23:08:56 +00:00
end
def new(conn, _params) do
changeset =
Pipeline.changeset(%Pipeline{}, %{
name: "",
stages: "[\n]"
})
render(conn, "new.html", %{changeset: changeset})
2019-07-20 23:08:56 +00:00
end
def create(conn, %{"pipeline" => %{"name" => name, "stages" => stages_json}}) do
user = conn.assigns[:user]
2019-07-20 23:08:56 +00:00
with {:ok, stages} <- Jason.decode(stages_json),
{:ok, stages} <- validate_pipeline(stages) do
changeset = Pipeline.changeset(%Pipeline{}, %{user_id: user.id, name: name, stages: stages})
{:ok, _pipeline} = Repo.insert(changeset)
conn
|> put_flash(:info, "Pipeline created")
|> redirect(to: Routes.pipeline_path(Endpoint, :index))
else
{:error, reason} ->
error_changeset = Pipeline.changeset(%Pipeline{}, %{name: name, stages: stages_json})
conn
|> put_flash(:error, "Unable to create pipeline: #{reason}")
|> render("new.html", %{changeset: error_changeset})
end
2019-07-20 23:08:56 +00:00
end
def show(conn, _params) do
pipeline = conn.assigns[:pipeline]
render(conn, "show.html", %{pipeline: pipeline})
end
2019-07-20 23:08:56 +00:00
def delete(conn, _params) do
conn.assigns[:pipeline]
|> Repo.delete()
redirect(conn, to: Routes.pipeline_path(Endpoint, :index))
end
def edit(conn, _params) do
pipeline = conn.assigns[:pipeline]
{:ok, stages_json} = Jason.encode(pipeline.stages, pretty: true)
render(conn, "edit.html", %{
pipeline: pipeline,
name: pipeline.name,
stages_json: stages_json
2019-07-20 23:08:56 +00:00
})
end
def update(conn, %{"pipeline" => %{"name" => name, "stages" => stages_json}}) do
pipeline = conn.assigns[:pipeline]
with {:ok, stages} <- Jason.decode(stages_json),
{:ok, stages} <- validate_pipeline(stages) do
2020-01-30 03:20:49 +00:00
changeset = Pipeline.changeset(pipeline, %{name: name, stages: stages})
2019-07-20 23:08:56 +00:00
{:ok, _pipeline} = Repo.update(changeset)
2019-07-20 23:08:56 +00:00
conn
|> put_flash(:info, "Pipeline edited")
|> redirect(to: Routes.pipeline_path(Endpoint, :show, pipeline.id))
else
{:error, reason} ->
conn
2020-05-31 19:56:27 +00:00
|> put_flash(:error, "Unable to edit pipeline: #{reason}")
|> render("edit.html", %{
pipeline: pipeline,
name: name,
stages_json: stages_json
})
end
2019-07-20 23:08:56 +00:00
end
defp validate_pipeline(stages) do
stages
|> Enum.with_index()
|> Enum.reduce_while({:ok, []}, fn {stage, index}, {:ok, new_stages} ->
case validate_stage(stage) do
{:ok, stage} ->
{:cont, {:ok, new_stages ++ [stage]}}
{:error, reason} ->
2020-05-31 19:56:27 +00:00
{:halt, {:error, "invalid stage at index #{index}: #{reason}"}}
end
end)
end
defp validate_stage(%{"module_name" => module_name, "options" => options}) do
with true <- module_exists(module_name),
{:ok, options} <-
apply(String.to_existing_atom("Elixir." <> module_name), :validate_opts, [
options
]) do
{:ok, %{"module_name" => module_name, "options" => options}}
else
false ->
{:error, "module #{module_name} does not exist"}
2019-07-20 23:08:56 +00:00
{:error, reason} ->
{:error, "invalid options for #{module_name}: #{reason}"}
end
end
defp validate_stage(_),
do: {:error, "pipeline stage must be a map with module_name and options"}
defp module_exists(module_name) do
try do
String.to_existing_atom("Elixir." <> module_name)
true
rescue
ArgumentError ->
false
end
2019-07-20 23:08:56 +00:00
end
end