philomena/lib/philomena_web/controllers/topic/poll_controller.ex

52 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule PhilomenaWeb.Topic.PollController do
use PhilomenaWeb, :controller
alias Philomena.Forums.Forum
alias Philomena.Polls
alias Philomena.Repo
plug :load_and_authorize_resource,
model: Forum,
id_name: "forum_id",
id_field: "short_name",
persisted: true
plug PhilomenaWeb.LoadTopicPlug
plug PhilomenaWeb.LoadPollPlug
plug :verify_authorized
plug :preload_options
def edit(conn, _params) do
changeset = Polls.change_poll(conn.assigns.poll)
render(conn, "edit.html", title: "Editing Poll", changeset: changeset)
end
def update(conn, %{"poll" => poll_params}) do
case Polls.update_poll(conn.assigns.poll, poll_params) do
{:ok, _poll} ->
conn
|> put_flash(:info, "Poll successfully updated.")
2020-04-30 23:26:34 +02:00
|> redirect(
to: Routes.forum_topic_path(conn, :show, conn.assigns.forum, conn.assigns.topic)
)
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
defp preload_options(conn, _opts) do
poll = Repo.preload(conn.assigns.poll, :options)
assign(conn, :poll, poll)
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :hide, conn.assigns.topic) do
true -> conn
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end