Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 1d08118

Browse files
authored
refactor: enhance delete article (#410)
* refactor: enhance delete article * refactor: ignore delete document result
1 parent 22f2b6f commit 1d08118

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ defmodule GroupherServer.CMS do
9292

9393
defdelegate mark_delete_article(thread, id), to: ArticleCURD
9494
defdelegate undo_mark_delete_article(thread, id), to: ArticleCURD
95-
defdelegate remove_article(thread, id), to: ArticleCURD
96-
defdelegate remove_article(thread, id, reason), to: ArticleCURD
95+
defdelegate delete_article(article), to: ArticleCURD
96+
defdelegate delete_article(article, reason), to: ArticleCURD
9797

9898
defdelegate update_active_timestamp(thread, article), to: ArticleCURD
9999
defdelegate sink_article(thread, id), to: ArticleCURD

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
77
import GroupherServer.CMS.Helper.Matcher
88

99
import Helper.Utils,
10-
only: [done: 1, pick_by: 2, module_to_atom: 1, get_config: 2, ensure: 2, module_to_upcase: 1]
10+
only: [
11+
done: 1,
12+
pick_by: 2,
13+
module_to_atom: 1,
14+
get_config: 2,
15+
ensure: 2,
16+
module_to_upcase: 1,
17+
thread_of_article: 1
18+
]
1119

1220
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
1321
import Helper.ErrorCode
@@ -328,26 +336,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
328336
@doc """
329337
remove article forever
330338
"""
331-
def remove_article(thread, id, reason \\ @remove_article_hint) do
332-
with {:ok, info} <- match(thread),
333-
{:ok, article} <- ORM.find(info.model, id, preload: [:communities, [author: :user]]) do
334-
Multi.new()
335-
|> Multi.run(:remove_article, fn _, _ ->
336-
article |> ORM.delete()
337-
end)
338-
|> Multi.run(:update_community_article_count, fn _, _ ->
339-
CommunityCURD.update_community_count_field(article.communities, thread)
340-
end)
341-
|> Multi.run(:update_user_published_meta, fn _, _ ->
342-
Accounts.update_published_states(article.author.user.id, thread)
343-
end)
344-
|> Multi.run(:delete_document, fn _, _ ->
345-
Document.remove(thread, id)
346-
end)
347-
# TODO: notify author
348-
|> Repo.transaction()
349-
|> result()
350-
end
339+
def delete_article(article, reason \\ @remove_article_hint) do
340+
article = Repo.preload(article, [:communities, [author: :user]])
341+
{:ok, thread} = thread_of_article(article)
342+
343+
Multi.new()
344+
|> Multi.run(:delete_article, fn _, _ ->
345+
article |> ORM.delete()
346+
end)
347+
|> Multi.run(:update_community_article_count, fn _, _ ->
348+
CommunityCURD.update_community_count_field(article.communities, thread)
349+
end)
350+
|> Multi.run(:update_user_published_meta, fn _, _ ->
351+
Accounts.update_published_states(article.author.user.id, thread)
352+
end)
353+
|> Multi.run(:delete_document, fn _, _ ->
354+
Document.remove(thread, article.id)
355+
# for those history & test setup case
356+
{:ok, :pass}
357+
end)
358+
# TODO: notify author
359+
|> Repo.transaction()
360+
|> result()
351361
end
352362

353363
@spec ensure_author_exists(User.t()) :: {:ok, User.t()}
@@ -497,7 +507,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
497507

498508
defp result({:ok, %{update_edit_status: result}}), do: {:ok, result}
499509
defp result({:ok, %{update_article: result}}), do: {:ok, result}
500-
defp result({:ok, %{remove_article: result}}), do: {:ok, result}
510+
defp result({:ok, %{delete_article: result}}), do: {:ok, result}
501511
# NOTE: for read article, order is import
502512
defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done()
503513
defp result({:ok, %{update_article_meta: result}}), do: {:ok, result}

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
7373
CMS.update_article(article, args)
7474
end
7575

76-
def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content)
76+
def delete_article(_root, %{passport_source: article}, _info) do
77+
CMS.delete_article(article)
78+
end
7779

7880
# #######################
7981
# article actions

test/groupher_server/cms/articles/blog_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ defmodule GroupherServer.Test.Articles.Blog do
173173
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: blog.id, thread: "BLOG"})
174174
{:ok, _blog_doc} = ORM.find_by(BlogDocument, %{blog_id: blog.id})
175175

176-
CMS.remove_article(:blog, blog.id)
176+
{:ok, _} = CMS.delete_article(blog)
177177

178178
{:error, _} = ORM.find(Blog, blog.id)
179179
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: blog.id, thread: "BLOG"})

test/groupher_server/cms/articles/job_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ defmodule GroupherServer.Test.Articles.Job do
174174
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: job.id, thread: "JOB"})
175175
{:ok, _job_doc} = ORM.find_by(JobDocument, %{job_id: job.id})
176176

177-
CMS.remove_article(:job, job.id)
177+
{:ok, _} = CMS.delete_article(job)
178178

179179
{:error, _} = ORM.find(Job, job.id)
180180
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: job.id, thread: "JOB"})

test/groupher_server/cms/articles/post_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ defmodule GroupherServer.Test.CMS.Articles.Post do
208208
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"})
209209
{:ok, _post_doc} = ORM.find_by(PostDocument, %{post_id: post.id})
210210

211-
CMS.remove_article(:post, post.id)
211+
{:ok, _} = CMS.delete_article(post)
212212

213213
{:error, _} = ORM.find(Post, post.id)
214214
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"})

0 commit comments

Comments
 (0)