mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
52 lines
1.5 KiB
Elixir
52 lines
1.5 KiB
Elixir
defmodule PhilomenaWeb.Topic.StickController do
|
|
import Plug.Conn
|
|
use PhilomenaWeb, :controller
|
|
|
|
alias Philomena.Topics.Topic
|
|
alias Philomena.Topics
|
|
alias Philomena.Repo
|
|
import Ecto.Query
|
|
|
|
plug :load_topic
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :stick, delete: :stick
|
|
plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true
|
|
|
|
def create(conn, _opts) do
|
|
topic = conn.assigns.topic
|
|
|
|
case Topics.stick_topic(topic) do
|
|
{:ok, topic} ->
|
|
conn
|
|
|> put_flash(:info, "Topic successfully stickied!")
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
{:error, _changeset} ->
|
|
conn
|
|
|> put_flash(:error, "Unable to stick the topic!")
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
end
|
|
end
|
|
|
|
def delete(conn, _opts) do
|
|
topic = conn.assigns.topic
|
|
|
|
case Topics.unstick_topic(topic) do
|
|
{:ok, topic} ->
|
|
conn
|
|
|> put_flash(:info, "Topic successfully unstickied!")
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
{:error, _changeset} ->
|
|
conn
|
|
|> put_flash(:error, "Unable to unstick the topic!")
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
end
|
|
end
|
|
|
|
defp load_topic(conn, _opts) do
|
|
topic = Topic
|
|
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
|
|> preload([:forum])
|
|
|> Repo.one()
|
|
|
|
Plug.Conn.assign(conn, :topic, topic)
|
|
end
|
|
end
|