Add error messages for creating/editing pipeline stages
This commit is contained in:
parent
7f3ff17925
commit
d0c6831d45
|
@ -49,57 +49,93 @@ defmodule FrenzyWeb.PipelineController do
|
|||
stage = conn.assigns[:stage]
|
||||
{:ok, options_json} = Jason.encode(stage.options, pretty: true)
|
||||
|
||||
changeset =
|
||||
PipelineStage.changeset(stage, %{
|
||||
options: options_json
|
||||
})
|
||||
|
||||
render(conn, "edit.html", %{
|
||||
feed: feed,
|
||||
stage: stage,
|
||||
options: options_json
|
||||
changeset: changeset
|
||||
})
|
||||
end
|
||||
|
||||
def update(conn, %{"options" => options_json}) do
|
||||
def update(conn, %{"pipeline_stage" => %{"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))
|
||||
with {:ok, options} <- Jason.decode(options_json) do
|
||||
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))
|
||||
else
|
||||
result ->
|
||||
error_changeset = PipelineStage.changeset(stage, %{options: options_json})
|
||||
|
||||
conn
|
||||
|> put_flash(:error, "Unable to update pipeline stage: #{inspect(result)}")
|
||||
|> render("edit.html", %{
|
||||
feed: feed,
|
||||
stage: stage,
|
||||
changeset: error_changeset
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = PipelineStage.changeset(%PipelineStage{}, %{})
|
||||
feed = conn.assigns[:feed]
|
||||
|
||||
changeset =
|
||||
PipelineStage.changeset(%PipelineStage{}, %{
|
||||
index: feed.pipeline_stages |> Enum.count(),
|
||||
options: "{}"
|
||||
})
|
||||
|
||||
render(conn, "new.html", %{
|
||||
feed: conn.assigns[:feed],
|
||||
feed: feed,
|
||||
changeset: changeset
|
||||
})
|
||||
end
|
||||
|
||||
def create(conn, %{
|
||||
"pipeline_stage" => %{
|
||||
"index" => index,
|
||||
"module_name" => module_name,
|
||||
"options" => options_json
|
||||
}
|
||||
"pipeline_stage" =>
|
||||
%{
|
||||
"index" => index,
|
||||
"module_name" => module_name,
|
||||
"options" => options_json
|
||||
} = params
|
||||
}) 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
|
||||
})
|
||||
with {index, _} <- Integer.parse(index),
|
||||
{:ok, options} <- Jason.decode(options_json) do
|
||||
changeset =
|
||||
Ecto.build_assoc(feed, :pipeline_stages, %{
|
||||
index: index,
|
||||
module_name: module_name,
|
||||
options: options
|
||||
})
|
||||
|
||||
{:ok, _stage} = Repo.insert(changeset)
|
||||
{:ok, _stage} = Repo.insert(changeset)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Pipeline Stage created")
|
||||
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
|
||||
conn
|
||||
|> put_flash(:info, "Pipeline Stage created")
|
||||
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
|
||||
else
|
||||
result ->
|
||||
error_changeset = PipelineStage.changeset(%PipelineStage{}, params)
|
||||
|
||||
conn
|
||||
|> put_flash(:error, "Unable to create pipeline stage: #{inspect(result)}")
|
||||
|> render("new.html", %{
|
||||
feed: feed,
|
||||
changeset: error_changeset
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, _params) do
|
||||
|
|
|
@ -53,7 +53,7 @@ defmodule FrenzyWeb.Router do
|
|||
# resources "/pipelines", PipelineController, only: [:edit, :update, :new, :create]
|
||||
|
||||
get "/feeds/:feed_id/pipelines/:stage_id/edit", PipelineController, :edit
|
||||
post "/feeds/:feed_id/pipelines/:stage_id/update", PipelineController, :update
|
||||
put "/feeds/:feed_id/pipelines/:stage_id/update", PipelineController, :update
|
||||
delete "/feeds/:feed_id/pipelines/:stage_id/delete", PipelineController, :delete
|
||||
get "/feeds/:feed_id/pipelines/new", PipelineController, :new
|
||||
post "/feeds/:feed_id/pipelines/create", PipelineController, :create
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
<h3>Feed: <%= @feed.title %></h3>
|
||||
<h3>Module: <code><%= @stage.module_name %></code></h3>
|
||||
|
||||
<%= form_for @conn, Routes.pipeline_path(@conn, :update, @feed.id, @stage.id), fn f -> %>
|
||||
<%= textarea f, :options, value: @options, class: "form-control", rows: 15, style: "font-family: monospace;" %>
|
||||
<%= form_for @changeset, Routes.pipeline_path(@conn, :update, @feed.id, @stage.id), fn f -> %>
|
||||
<%= textarea f, :options, class: "form-control", rows: 15, style: "font-family: monospace;" %>
|
||||
<%= submit "Update", class: "btn btn-primary mt-2" %>
|
||||
<% end %>
|
||||
|
||||
|
|
|
@ -10,13 +10,13 @@
|
|||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label" for="index">Index</label>
|
||||
<div class="col-sm-10">
|
||||
<%= number_input f, :index, value: "#{@feed.pipeline_stages |> Enum.count()}", class: "form-control" %>
|
||||
<%= number_input f, :index, class: "form-control" %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label" for="options">Options</label>
|
||||
<div class="col-sm-10">
|
||||
<%= textarea f, :options, value: "{}", class: "form-control", rows: 15, style: "font-family: monospace;" %>
|
||||
<%= textarea f, :options, class: "form-control", rows: 15, style: "font-family: monospace;" %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group row">
|
||||
|
|
Loading…
Reference in New Issue