From 37be32f2b2c29d89b38557a5a1d60230418ebdac Mon Sep 17 00:00:00 2001 From: Luna D Date: Sat, 14 Dec 2019 11:43:26 -0500 Subject: [PATCH] finish topic moving --- lib/philomena/topics.ex | 26 ++++++++++++++++--- lib/philomena/topics/topic.ex | 4 +-- .../controllers/topic/lock_controller.ex | 9 ++++--- .../controllers/topic/move_controller.ex | 13 +++++++--- .../topic/post/delete_controller.ex | 6 ++--- .../controllers/topic/post/hide_controller.ex | 10 +++---- .../controllers/topic/stick_controller.ex | 9 ++++--- .../templates/topic/show.html.slime | 7 ----- 8 files changed, 52 insertions(+), 32 deletions(-) diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index c18cf37c..35576a59 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -210,9 +210,29 @@ defmodule Philomena.Topics do |> Repo.update() end - def move_topic(%Topic{} = topic, attrs) do - Topic.move_changeset(topic, attrs) - |> Repo.update() + def move_topic(topic, new_forum_id) do + old_forum_id = topic.forum_id + topic_changes = Topic.move_changeset(topic, new_forum_id) + + Multi.new + |> Multi.update(:topic, topic_changes) + |> Multi.run(:update_old_forum, fn repo, %{topic: topic} -> + {count, nil} = + Forum + |> where(id: ^old_forum_id) + |> repo.update_all(inc: [post_count: -topic.post_count, topic_count: -1]) + + {:ok, count} + end) + |> Multi.run(:update_new_forum, fn repo, %{topic: topic} -> + {count, nil} = + Forum + |> where(id: ^topic.forum_id) + |> repo.update_all(inc: [post_count: topic.post_count, topic_count: 1]) + + {:ok, count} + end) + |> Repo.isolated_transaction(:serializable) end def clear_notification(nil, _user), do: nil diff --git a/lib/philomena/topics/topic.ex b/lib/philomena/topics/topic.ex index 6ad54980..d2403c7b 100644 --- a/lib/philomena/topics/topic.ex +++ b/lib/philomena/topics/topic.ex @@ -91,9 +91,9 @@ defmodule Philomena.Topics.Topic do |> put_change(:lock_reason, "") end - def move_changeset(topic, attrs) do + def move_changeset(topic, new_forum_id) do change(topic) - |> put_change(:forum_id, attrs["target_forum_id"] |> String.to_integer()) + |> put_change(:forum_id, new_forum_id) end def put_slug(changeset) do diff --git a/lib/philomena_web/controllers/topic/lock_controller.ex b/lib/philomena_web/controllers/topic/lock_controller.ex index 6d983045..6a50ddcc 100644 --- a/lib/philomena_web/controllers/topic/lock_controller.ex +++ b/lib/philomena_web/controllers/topic/lock_controller.ex @@ -19,11 +19,11 @@ defmodule PhilomenaWeb.Topic.LockController do {:ok, topic} -> conn |> put_flash(:info, "Topic successfully locked!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) {:error, _changeset} -> conn |> put_flash(:error, "Unable to lock the topic!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) end end @@ -34,17 +34,18 @@ defmodule PhilomenaWeb.Topic.LockController do {:ok, topic} -> conn |> put_flash(:info, "Topic successfully unlocked!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) {:error, _changeset} -> conn |> put_flash(:error, "Unable to unlock the topic!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> 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) diff --git a/lib/philomena_web/controllers/topic/move_controller.ex b/lib/philomena_web/controllers/topic/move_controller.ex index 759c8f68..86767640 100644 --- a/lib/philomena_web/controllers/topic/move_controller.ex +++ b/lib/philomena_web/controllers/topic/move_controller.ex @@ -15,16 +15,21 @@ defmodule PhilomenaWeb.Topic.MoveController do # todo: moving def create(conn, %{"topic" => topic_params}) do topic = conn.assigns.topic + target_forum_id = String.to_integer(topic_params["target_forum_id"]) + + case Topics.move_topic(topic, target_forum_id) do + {:ok, %{topic: topic}} -> + topic = Repo.preload(topic, :forum) - case Topics.move_topic(topic, topic_params) do - {:ok, topic} -> conn |> put_flash(:info, "Topic successfully moved!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic_params["target_forum_id"] |> String.to_integer(), topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) {:error, _changeset} -> + topic = Repo.preload(topic, :forum) + conn |> put_flash(:error, "Unable to move the topic!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) end end diff --git a/lib/philomena_web/controllers/topic/post/delete_controller.ex b/lib/philomena_web/controllers/topic/post/delete_controller.ex index 90e9942e..a59d726b 100644 --- a/lib/philomena_web/controllers/topic/post/delete_controller.ex +++ b/lib/philomena_web/controllers/topic/post/delete_controller.ex @@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do alias Philomena.Posts plug PhilomenaWeb.CanaryMapPlug, create: :hide - plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic] + 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 @@ -16,12 +16,12 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do conn |> put_flash(:info, "Post successfully destroyed!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> 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!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}") end end end diff --git a/lib/philomena_web/controllers/topic/post/hide_controller.ex b/lib/philomena_web/controllers/topic/post/hide_controller.ex index 11b9b193..c659b91b 100644 --- a/lib/philomena_web/controllers/topic/post/hide_controller.ex +++ b/lib/philomena_web/controllers/topic/post/hide_controller.ex @@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do alias Philomena.Posts plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide - plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic] + plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic, topic: :forum] def create(conn, %{"post" => post_params}) do post = conn.assigns.post @@ -17,11 +17,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do conn |> put_flash(:info, "Post successfully hidden!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}") {:error, _changeset} -> conn |> put_flash(:error, "Unable to hide post!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}") end end @@ -34,11 +34,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do conn |> put_flash(:info, "Post successfully unhidden!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}") {:error, _changeset} -> conn |> put_flash(:error, "Unable to unhide post!") - |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}") + |> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}") end end end diff --git a/lib/philomena_web/controllers/topic/stick_controller.ex b/lib/philomena_web/controllers/topic/stick_controller.ex index ed63af04..e6d4ecc0 100644 --- a/lib/philomena_web/controllers/topic/stick_controller.ex +++ b/lib/philomena_web/controllers/topic/stick_controller.ex @@ -18,11 +18,11 @@ defmodule PhilomenaWeb.Topic.StickController do {:ok, topic} -> conn |> put_flash(:info, "Topic successfully stickied!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> 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_id, topic.id)) + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) end end @@ -33,17 +33,18 @@ defmodule PhilomenaWeb.Topic.StickController do {:ok, topic} -> conn |> put_flash(:info, "Topic successfully unstickied!") - |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + |> 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_id, topic.id)) + |> 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) diff --git a/lib/philomena_web/templates/topic/show.html.slime b/lib/philomena_web/templates/topic/show.html.slime index cf905ab3..b0ce6a61 100644 --- a/lib/philomena_web/templates/topic/show.html.slime +++ b/lib/philomena_web/templates/topic/show.html.slime @@ -118,13 +118,6 @@ h1 = @topic.title / .toggle-box-container / .toggle-box-container__content / p - / - if can? :move, @topic - / = form_tag forum_topic_move_path(@forum, @topic), method: :post, class: 'hform' do - / .field - / => select_tag :target_forum_id, options_from_collection_for_select(Forum.where.not(id: @forum.id).all, :short_name, :name), class: 'input hform__text' - / => button_tag class: 'hform__button button' do - / i.fa.fa-truck> - / | Move / - if can?(:destroy, @topic) && !@topic.hidden_from_users / = form_tag forum_topic_hide_path(@forum, @topic), method: :post, class: 'hform' do / .field