frenzy/lib/frenzy_web/controllers/pipeline_controller.ex

115 lines
3.0 KiB
Elixir
Raw Normal View History

2019-07-20 23:08:56 +00:00
defmodule FrenzyWeb.PipelineController do
use FrenzyWeb, :controller
alias Frenzy.{Repo, Feed, PipelineStage}
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
import Ecto.Query
plug :user_owns_feed
plug :user_owns_stage
defp user_owns_feed(%Plug.Conn{path_params: %{"feed_id" => feed_id}} = conn, _opts) do
user = conn.assigns[:user]
feed = Repo.get(Feed, feed_id) |> Repo.preload(:pipeline_stages)
if Enum.any?(user.groups, fn g -> g.id == feed.group_id end) do
conn
|> assign(:feed, feed)
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_feed(conn, _opts), do: conn
defp user_owns_stage(%Plug.Conn{path_params: %{"stage_id" => stage_id}} = conn, _opts) do
feed = conn.assigns[:feed]
stage = Repo.get(PipelineStage, stage_id)
if stage.feed_id == feed.id do
conn
|> assign(:stage, stage)
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_stage(conn, _opts), do: conn
def edit(conn, %{"stage_id" => stage_id}) do
feed = conn.assigns[:feed]
stage = conn.assigns[:stage]
{:ok, options_json} = Jason.encode(stage.options, pretty: true)
render(conn, "edit.html", %{
feed: feed,
stage: stage,
options: options_json
})
end
def update(conn, %{"options" => options_json}) do
feed = conn.assigns[:feed]
stage = conn.assigns[:stage]
{:ok, options} = Jason.decode(options_json)
changeset = PipelineStage.changeset(stage, %{options: options})
{:ok, _stage} = Repo.update(changeset)
conn
|> put_flash(:info, "Pipeline Stage updated")
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
end
def new(conn, _params) do
changeset = PipelineStage.changeset(%PipelineStage{}, %{})
render(conn, "new.html", %{
feed: conn.assigns[:feed],
changeset: changeset
})
end
def create(conn, %{
"pipeline_stage" => %{
"index" => index,
"module_name" => module_name,
"options" => options_json
}
}) do
feed = conn.assigns[:feed]
{index, _} = Integer.parse(index)
{:ok, options} = Jason.decode(options_json)
changeset =
Ecto.build_assoc(feed, :pipeline_stages, %{
index: index,
module_name: module_name,
options: options
})
{:ok, _stage} = Repo.insert(changeset)
conn
|> put_flash(:info, "Pipeline Stage created")
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
end
def delete(conn, _params) do
feed = conn.assigns[:feed]
stage = conn.assigns[:stage]
{:ok, _stage} = Repo.delete(stage)
conn
|> put_flash(:info, "Pipeline Stage deleted")
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
end
end