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

Commit a85b191

Browse files
committed
refactor(pinned-article): undo pin article
1 parent 6736313 commit a85b191

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ defmodule GroupherServer.CMS do
8787
# >> set flag on article, like: pin / unpin article
8888
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
8989
defdelegate pin_article(thread, id, community_id), to: ArticleOperation
90+
defdelegate undo_pin_article(thread, id, community_id), to: ArticleOperation
9091
defdelegate pin_content(queryable, community_id), to: ArticleOperation
9192
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation
9293

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,39 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
3030
alias GroupherServer.CMS.Repo, as: CMSRepo
3131
alias GroupherServer.Repo
3232

33-
alias Ecto.Multi
34-
3533
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()
3634

3735
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
3836
def pin_article(thread, article_id, community_id) do
3937
with {:ok, info} <- match(thread),
4038
{:ok, community} <- ORM.find(Community, community_id),
41-
{:ok, _} <- check_pinned_article_count(community_id, thread) do
42-
Multi.new()
43-
|> Multi.run(:update_article_pinned_flag, fn _, _ ->
44-
# ORM.update(comment, %{is_pinned: true})
45-
{:ok, :pass}
46-
end)
47-
|> Multi.run(:create_pinned_article, fn _, _ ->
48-
thread_upcase = thread |> to_string |> String.upcase()
49-
50-
args =
51-
Map.put(
52-
%{community_id: community.id, thread: thread_upcase},
53-
info.foreign_key,
54-
article_id
55-
)
56-
57-
PinnedArticle |> ORM.create(args)
58-
end)
59-
|> Repo.transaction()
60-
|> create_pinned_article_result()
39+
{:ok, _} <- check_pinned_article_count(community.id, thread) do
40+
thread_upcase = thread |> to_string |> String.upcase()
41+
42+
args =
43+
Map.put(
44+
%{community_id: community.id, thread: thread_upcase},
45+
info.foreign_key,
46+
article_id
47+
)
48+
49+
PinnedArticle |> ORM.create(args)
50+
end
51+
end
52+
53+
@spec undo_pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
54+
def undo_pin_article(thread, article_id, community_id) do
55+
with {:ok, info} <- match(thread) do
56+
thread_upcase = thread |> to_string |> String.upcase()
57+
58+
args =
59+
Map.put(
60+
%{community_id: community_id, thread: thread_upcase},
61+
info.foreign_key,
62+
article_id
63+
)
64+
65+
ORM.findby_delete(PinnedArticle, args)
6166
end
6267
end
6368

@@ -306,10 +311,4 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
306311
_ -> {:ok, :pass}
307312
end
308313
end
309-
310-
defp create_pinned_article_result({:ok, %{create_pinned_article: result}}), do: {:ok, result}
311-
312-
defp create_pinned_article_result({:error, _, result, _steps}) do
313-
{:error, result}
314-
end
315314
end

test/groupher_server/cms/article_pin_test.exs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule GroupherServer.Test.CMS.ArticlePin do
2+
@moduledoc false
3+
24
use GroupherServer.TestTools
35

46
alias Helper.ORM
@@ -52,13 +54,15 @@ defmodule GroupherServer.Test.CMS.ArticlePin do
5254
assert {:error, _} = CMS.pin_article(:post, 8848, community.id)
5355
end
5456

55-
# test "can undo pin to a post", ~m(community post)a do
56-
# {:ok, pined_post} = CMS.pin_content(post, community)
57-
# assert pined_post.id == post.id
57+
@tag :wip2
58+
test "can undo pin to a post", ~m(community post)a do
59+
{:ok, _} = CMS.pin_article(:post, post.id, community.id)
5860

59-
# assert {:ok, unpined} = CMS.undo_pin_content(post, community)
60-
# assert unpined.id == post.id
61-
# end
61+
assert {:ok, unpinned} = CMS.undo_pin_article(:post, post.id, community.id)
62+
assert unpinned.post_id == post.id
63+
64+
assert {:error, _} = ORM.find_by(PinnedArticle, %{post_id: post.id})
65+
end
6266
end
6367

6468
# describe "[cms job pin]" do

0 commit comments

Comments
 (0)