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

Commit aabff2f

Browse files
committed
refactor(pinned-article): wip
1 parent 36b1d2a commit aabff2f

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,35 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
3232
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
3333
def pin_article(thread, article_id, community_id) do
3434
with {:ok, info} <- match(thread),
35-
{:ok, community} <- ORM.find(Community, community_id),
36-
{:ok, _} <- check_pinned_article_count(community.id, thread) do
37-
thread_upcase = thread |> to_string |> String.upcase()
38-
39-
args =
40-
Map.put(
41-
%{community_id: community.id, thread: thread_upcase},
42-
info.foreign_key,
43-
article_id
44-
)
45-
46-
PinnedArticle |> ORM.create(args)
35+
args <- pack_pin_args(thread, article_id, community_id),
36+
{:ok, _} <- check_pinned_article_count(args.community_id, thread),
37+
{:ok, _} <- ORM.create(PinnedArticle, args) do
38+
ORM.find(info.model, article_id)
4739
end
4840
end
4941

5042
@spec undo_pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
5143
def undo_pin_article(thread, article_id, community_id) do
52-
with {:ok, info} <- match(thread) do
53-
thread_upcase = thread |> to_string |> String.upcase()
54-
55-
args =
56-
Map.put(
57-
%{community_id: community_id, thread: thread_upcase},
58-
info.foreign_key,
59-
article_id
60-
)
61-
44+
with {:ok, info} <- match(thread),
45+
args <- pack_pin_args(thread, article_id, community_id) do
6246
ORM.findby_delete(PinnedArticle, args)
6347
ORM.find(info.model, article_id)
6448
end
6549
end
6650

51+
defp pack_pin_args(thread, article_id, community_id) do
52+
with {:ok, info} <- match(thread),
53+
{:ok, community} <- ORM.find(Community, community_id) do
54+
thread_upcase = thread |> to_string |> String.upcase()
55+
56+
Map.put(
57+
%{community_id: community.id, thread: thread_upcase},
58+
info.foreign_key,
59+
article_id
60+
)
61+
end
62+
end
63+
6764
@doc """
6865
trash / untrash articles
6966
"""

test/groupher_server_web/mutation/cms/post_flag_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do
8484
}
8585
}
8686
"""
87+
@tag :wip2
8788
test "auth user can pin post", ~m(community post)a do
8889
variables = %{id: post.id, communityId: community.id}
8990

0 commit comments

Comments
 (0)