philomena/lib/philomena_web/controllers/topic/post/delete_controller.ex

28 lines
883 B
Elixir
Raw Normal View History

defmodule PhilomenaWeb.Topic.Post.DeleteController do
use PhilomenaWeb, :controller
alias Philomena.Posts.Post
alias Philomena.Posts
plug PhilomenaWeb.CanaryMapPlug, create: :hide
2019-12-14 17:43:26 +01:00
plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic, topic: :forum]
def create(conn, _params) do
post = conn.assigns.post
case Posts.destroy_post(post) do
{:ok, post} ->
Posts.reindex_post(post)
conn
|> put_flash(:info, "Post successfully destroyed!")
2019-12-14 17:43:26 +01:00
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to destroy post!")
2019-12-14 17:43:26 +01:00
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
end
end
end