Thanks to visit codestin.com
Credit goes to github.com

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

Commit 46e52c5

Browse files
committed
refactor: make set_community_flags readable
1 parent f386cb0 commit 46e52c5

File tree

14 files changed

+32
-41
lines changed

14 files changed

+32
-41
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ defmodule GroupherServer.CMS do
8484
defdelegate favorited_category(thread, content_id, user), to: FavoritedContents
8585
# ArticleOperation
8686
# >> set flag on article, like: pin / unpin article
87-
defdelegate set_community_flags(queryable, community_id, attrs), to: ArticleOperation
87+
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
8888
defdelegate pin_content(queryable, community_id, topic), to: ArticleOperation
8989
defdelegate undo_pin_content(queryable, community_id, topic), to: ArticleOperation
9090
defdelegate pin_content(queryable, community_id), to: ArticleOperation

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
129129
defp exec_set_tag(_thread, _id, _attrs), do: {:ok, :pass}
130130

131131
# TODO: flag 逻辑似乎有问题
132-
defp exec_set_community_flag(%Community{id: cid}, content, %{flag: _flag}) do
133-
# TODO: 1. 参数改变一下顺序,把 community 放在前面
134-
# TODO: 2. 直接传 community 下去,免去 set_community_flags 函数再在内部查 community
135-
# TODO: 3. 该函数似乎逻辑似乎有点问题, 没有区分 action trash|..
136-
# 要考虑第二条是否符合现实?
137-
ArticleOperation.set_community_flags(content, cid, %{
132+
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
133+
ArticleOperation.set_community_flags(community, content, %{
138134
trash: false
139135
})
140-
141-
# TODO: remove this judge, as content should have a flag
142-
# case action |> Map.has_key?(:flag) do
143-
# true ->
144-
# ArticleOperation.set_community_flags(content, community.id, %{
145-
# trash: false
146-
# })
147-
148-
# false ->
149-
# {:ok, :pass}
150-
# end
151136
end
152137

153138
defp exec_set_community_flag(_community, _content, _action) do

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
9797
@doc """
9898
trash / untrash articles
9999
"""
100-
def set_community_flags(content, community_id, attrs) do
100+
def set_community_flags(%Community{id: cid}, content, attrs) do
101+
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102+
{:ok, record} <- insert_flag_record(content, cid, attrs) do
103+
{:ok, struct(content, %{trash: record.trash})}
104+
end
105+
end
106+
107+
def set_community_flags(community_id, content, attrs) do
101108
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102109
{:ok, community} <- ORM.find(Community, community_id),
103110
{:ok, record} <- insert_flag_record(content, community.id, attrs) do

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
167167

168168
defp set_community_flags(community_id, thread, id, flag) do
169169
with {:ok, content} <- match_action(thread, :self) do
170-
content.target
171-
|> struct(%{id: id})
172-
|> CMS.set_community_flags(community_id, flag)
170+
queryable = content.target |> struct(%{id: id})
171+
172+
CMS.set_community_flags(community_id, queryable, flag)
173173
end
174174
end
175175

test/groupher_server/cms/content_flag_test.exs

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

46
alias GroupherServer.CMS
57

68
alias CMS.{
7-
Post,
89
PostCommunityFlag,
9-
Repo,
1010
RepoCommunityFlag,
11-
Job,
1211
JobCommunityFlag,
13-
Video,
1412
VideoCommunityFlag
1513
}
1614

@@ -35,7 +33,7 @@ defmodule GroupherServer.Test.CMS.ContentFlags do
3533
{:ok, found} = PostCommunityFlag |> ORM.find_by(~m(post_id community_id)a)
3634
assert found.trash == false
3735

38-
CMS.set_community_flags(%Post{id: post.id}, community.id, %{trash: true})
36+
CMS.set_community_flags(community, post, %{trash: true})
3937

4038
{:ok, found} = PostCommunityFlag |> ORM.find_by(~m(post_id community_id)a)
4139

@@ -55,7 +53,7 @@ defmodule GroupherServer.Test.CMS.ContentFlags do
5553
{:ok, found} = JobCommunityFlag |> ORM.find_by(~m(job_id community_id)a)
5654
assert found.trash == false
5755

58-
CMS.set_community_flags(%Job{id: job.id}, community.id, %{trash: true})
56+
CMS.set_community_flags(community, job, %{trash: true})
5957

6058
{:ok, found} = JobCommunityFlag |> ORM.find_by(~m(job_id community_id)a)
6159

@@ -73,7 +71,7 @@ defmodule GroupherServer.Test.CMS.ContentFlags do
7371
{:ok, found} = VideoCommunityFlag |> ORM.find_by(~m(video_id community_id)a)
7472
assert found.trash == false
7573

76-
CMS.set_community_flags(%Video{id: video.id}, community.id, %{trash: true})
74+
CMS.set_community_flags(community, video, %{trash: true})
7775

7876
{:ok, found} = VideoCommunityFlag |> ORM.find_by(~m(video_id community_id)a)
7977

@@ -91,7 +89,7 @@ defmodule GroupherServer.Test.CMS.ContentFlags do
9189
{:ok, found} = RepoCommunityFlag |> ORM.find_by(~m(repo_id community_id)a)
9290
assert found.trash == false
9391

94-
CMS.set_community_flags(%Repo{id: repo.id}, community.id, %{trash: true})
92+
CMS.set_community_flags(community, repo, %{trash: true})
9593

9694
{:ok, found} = RepoCommunityFlag |> ORM.find_by(~m(repo_id community_id)a)
9795

test/groupher_server_web/mutation/cms/cms_manager_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ defmodule GroupherServer.Test.Mutation.CMS.Manager do
2828
}
2929
}
3030
"""
31+
@tag :wip2
3132
test "root can trash a post", ~m(community user)a do
3233
post_attrs = mock_attrs(:post, %{community_id: community.id})
3334
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)

test/groupher_server_web/mutation/cms/job_flag_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
5757
test "auth user can undo trash job", ~m(community job)a do
5858
variables = %{id: job.id, communityId: community.id}
5959

60-
{:ok, _} = CMS.set_community_flags(job, community.id, %{trash: true})
60+
{:ok, _} = CMS.set_community_flags(community, job, %{trash: true})
6161

6262
passport_rules = %{community.raw => %{"job.undo_trash" => true}}
6363
rule_conn = simu_conn(:user, cms: passport_rules)

test/groupher_server_web/mutation/cms/post_flag_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do
5757
test "auth user can undo trash post", ~m(community post)a do
5858
variables = %{id: post.id, communityId: community.id}
5959

60-
{:ok, _} = CMS.set_community_flags(post, community.id, %{trash: true})
60+
{:ok, _} = CMS.set_community_flags(community, post, %{trash: true})
6161

6262
passport_rules = %{community.raw => %{"post.undo_trash" => true}}
6363
rule_conn = simu_conn(:user, cms: passport_rules)

test/groupher_server_web/mutation/cms/repo_flag_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do
5757
test "auth user can undo trash repo", ~m(community repo)a do
5858
variables = %{id: repo.id, communityId: community.id}
5959

60-
{:ok, _} = CMS.set_community_flags(repo, community.id, %{trash: true})
60+
{:ok, _} = CMS.set_community_flags(community, repo, %{trash: true})
6161

6262
passport_rules = %{community.raw => %{"repo.undo_trash" => true}}
6363
rule_conn = simu_conn(:user, cms: passport_rules)

test/groupher_server_web/mutation/cms/video_flag_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule GroupherServer.Test.Mutation.VideoFlag do
5757
test "auth user can undo trash video", ~m(community video)a do
5858
variables = %{id: video.id, communityId: community.id}
5959

60-
{:ok, _} = CMS.set_community_flags(video, community.id, %{trash: true})
60+
{:ok, _} = CMS.set_community_flags(community, video, %{trash: true})
6161

6262
passport_rules = %{community.raw => %{"video.undo_trash" => true}}
6363
rule_conn = simu_conn(:user, cms: passport_rules)

0 commit comments

Comments
 (0)