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

Commit 00299b3

Browse files
committed
refactor(article-tags): wip
1 parent eedabd7 commit 00299b3

File tree

13 files changed

+211
-161
lines changed

13 files changed

+211
-161
lines changed

lib/groupher_server/cms/article_tag.ex

+1-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.ArticleTag do
66
import Ecto.Changeset
77

88
alias GroupherServer.CMS
9-
alias CMS.{Author, Community, Job, Post}
9+
alias CMS.{Author, Community}
1010

1111
@required_fields ~w(thread title color author_id community_id)a
1212
@updatable_fields ~w(thread title color community_id)a
@@ -19,21 +19,6 @@ defmodule GroupherServer.CMS.ArticleTag do
1919
belongs_to(:community, Community)
2020
belongs_to(:author, Author)
2121

22-
# many_to_many(
23-
# :posts,
24-
# Post,
25-
# join_through: "posts_tags",
26-
# join_keys: [post_id: :id, tag_id: :id],
27-
# on_delete: :delete_all
28-
# )
29-
30-
# many_to_many(
31-
# :jobs,
32-
# Job,
33-
# join_through: "jobs_tags",
34-
# join_keys: [job_id: :id, tag_id: :id]
35-
# )
36-
3722
timestamps(type: :utc_datetime)
3823
end
3924

lib/groupher_server/cms/delegates/article_tag.ex

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
55
import Ecto.Query, warn: false
66
import GroupherServer.CMS.Helper.Matcher2
77
import Helper.Validator.Guards, only: [g_is_id: 1]
8-
import Helper.Utils, only: [done: 1]
8+
import Helper.Utils, only: [done: 1, camelize_map_key: 2, map_atom_values_to_upcase_str: 1]
99
import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
1010
import ShortMaps
1111
import Helper.ErrorCode
@@ -23,10 +23,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
2323
def create_article_tag(%Community{id: community_id}, thread, attrs, %User{id: user_id}) do
2424
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
2525
{:ok, community} <- ORM.find(Community, community_id) do
26-
thread = thread |> to_string |> String.upcase()
27-
2826
attrs =
29-
attrs |> Map.merge(%{author_id: author.id, community_id: community.id, thread: thread})
27+
attrs
28+
|> Map.merge(%{author_id: author.id, community_id: community.id, thread: thread})
29+
|> map_atom_values_to_upcase_str
3030

3131
ArticleTag |> ORM.create(attrs)
3232
end
@@ -37,6 +37,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
3737
"""
3838
def update_article_tag(id, attrs) do
3939
with {:ok, article_tag} <- ORM.find(ArticleTag, id) do
40+
attrs = attrs |> map_atom_values_to_upcase_str
4041
ORM.update(article_tag, attrs)
4142
end
4243
end

lib/groupher_server_web/resolvers/cms_resolver.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
206206
CMS.create_article_tag(%Community{id: community_id}, thread, args, user)
207207
end
208208

209-
def delete_article_tag(_root, %{id: id}, _info) do
210-
CMS.delete_article_tag(id)
211-
end
212-
213209
def update_article_tag(_root, %{id: id} = args, _info) do
214210
CMS.update_article_tag(id, args)
215211
end
216212

213+
def delete_article_tag(_root, %{id: id}, _info) do
214+
CMS.delete_article_tag(id)
215+
end
216+
217217
def set_article_tag(_root, ~m(id thread article_tag_id)a, _info) do
218218
CMS.set_article_tag(thread, id, article_tag_id)
219219
end

lib/groupher_server_web/schema/cms/cms_misc.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
108108
value(:least_words)
109109
end
110110

111-
enum :rainbow_color_enum do
111+
enum :rainbow_color do
112112
value(:red)
113113
value(:orange)
114114
value(:yellow)

lib/groupher_server_web/schema/cms/mutations/community.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do
129129
@desc "create a tag"
130130
field :create_article_tag, :article_tag do
131131
arg(:title, non_null(:string))
132-
arg(:color, non_null(:rainbow_color_enum))
132+
arg(:color, non_null(:rainbow_color))
133133
arg(:community_id, non_null(:id))
134134
arg(:thread, :thread, default_value: :post)
135135

@@ -143,9 +143,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do
143143
@desc "update a tag"
144144
field :update_article_tag, :article_tag do
145145
arg(:id, non_null(:id))
146-
arg(:title, non_null(:string))
147-
arg(:color, non_null(:rainbow_color_enum))
148146
arg(:community_id, non_null(:id))
147+
arg(:title, :string)
148+
arg(:color, :rainbow_color)
149149
arg(:thread, :thread, default_value: :post)
150150

151151
middleware(M.Authorize, :login)

lib/helper/utils/map.ex

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
defmodule Helper.Utils.Map do
22
@moduledoc """
3-
unitil functions
3+
utils functions for map structure
44
"""
5+
@doc """
6+
map atom value to upcase string
7+
8+
e.g:
9+
%{hello: :world} # -> %{hello: "WORLD"}
10+
"""
11+
def map_atom_values_to_upcase_str(map) when is_map(map) do
12+
map
13+
|> Enum.reduce(%{}, fn {key, val}, acc ->
14+
case is_atom(val) do
15+
true -> Map.put(acc, key, val |> to_string |> String.upcase())
16+
false -> Map.put(acc, key, val)
17+
end
18+
end)
19+
end
20+
21+
def map_atom_values_to_upcase_str(value), do: value
22+
523
def map_key_stringify(%{__struct__: _} = map) when is_map(map) do
624
map = Map.from_struct(map)
725
map |> Enum.reduce(%{}, fn {key, val}, acc -> Map.put(acc, to_string(key), val) end)

lib/helper/utils/utils.ex

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Helper.Utils do
1111
alias Helper.{Cache, Utils}
1212

1313
# Map utils
14+
defdelegate map_atom_values_to_upcase_str(map), to: Utils.Map
1415
defdelegate map_key_stringify(map), to: Utils.Map
1516
defdelegate keys_to_atoms(map), to: Utils.Map
1617
defdelegate keys_to_strings(map), to: Utils.Map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
defmodule GroupherServer.Test.Mutation.CMS.ArticleArticleTags.CURD do
2+
@moduledoc false
3+
4+
use GroupherServer.TestTools
5+
6+
alias GroupherServer.CMS
7+
alias CMS.ArticleTag
8+
9+
alias Helper.ORM
10+
11+
setup do
12+
{:ok, community} = db_insert(:community)
13+
{:ok, thread} = db_insert(:thread)
14+
{:ok, user} = db_insert(:user)
15+
16+
tag_attrs = mock_attrs(:tag)
17+
18+
user_conn = simu_conn(:user)
19+
guest_conn = simu_conn(:guest)
20+
21+
{:ok, ~m(user_conn guest_conn community thread user tag_attrs)a}
22+
end
23+
24+
describe "[mutation cms tag]" do
25+
@create_tag_query """
26+
mutation($thread: Thread!, $title: String!, $color: RainbowColor!, $communityId: ID!) {
27+
createArticleTag(thread: $thread, title: $title, color: $color, communityId: $communityId) {
28+
id
29+
title
30+
color
31+
thread
32+
community {
33+
id
34+
logo
35+
title
36+
}
37+
}
38+
}
39+
"""
40+
@tag :wip2
41+
test "create tag with valid attrs, has default POST thread and default posts",
42+
~m(community)a do
43+
variables = %{
44+
title: "tag title",
45+
communityId: community.id,
46+
thread: "POST",
47+
color: "GREEN"
48+
}
49+
50+
passport_rules = %{community.title => %{"post.article_tag.create" => true}}
51+
rule_conn = simu_conn(:user, cms: passport_rules)
52+
53+
created = rule_conn |> mutation_result(@create_tag_query, variables, "createArticleTag")
54+
55+
belong_community = created["community"]
56+
57+
{:ok, found} = ArticleTag |> ORM.find(created["id"])
58+
59+
assert created["id"] == to_string(found.id)
60+
assert found.thread == "POST"
61+
assert belong_community["id"] == to_string(community.id)
62+
end
63+
64+
@tag :wip2
65+
test "unauth user create tag fails", ~m(community user_conn guest_conn)a do
66+
variables = %{
67+
title: "tag title",
68+
communityId: community.id,
69+
thread: "POST",
70+
color: "GREEN"
71+
}
72+
73+
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
74+
75+
assert user_conn |> mutation_get_error?(@create_tag_query, variables, ecode(:passport))
76+
77+
assert guest_conn
78+
|> mutation_get_error?(@create_tag_query, variables, ecode(:account_login))
79+
80+
assert rule_conn |> mutation_get_error?(@create_tag_query, variables, ecode(:passport))
81+
end
82+
83+
@update_tag_query """
84+
mutation($id: ID!, $color: RainbowColor, $title: String, $communityId: ID!) {
85+
updateArticleTag(id: $id, color: $color, title: $title, communityId: $communityId) {
86+
id
87+
title
88+
color
89+
}
90+
}
91+
"""
92+
@tag :wip2
93+
test "auth user can update a tag", ~m(tag_attrs community user)a do
94+
{:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user)
95+
96+
variables = %{
97+
id: article_tag.id,
98+
color: "YELLOW",
99+
title: "new title",
100+
communityId: community.id
101+
}
102+
103+
passport_rules = %{community.title => %{"post.article_tag.update" => true}}
104+
rule_conn = simu_conn(:user, cms: passport_rules)
105+
106+
updated = rule_conn |> mutation_result(@update_tag_query, variables, "updateArticleTag")
107+
108+
assert updated["color"] == "YELLOW"
109+
assert updated["title"] == "new title"
110+
end
111+
112+
@delete_tag_query """
113+
mutation($id: ID!, $communityId: ID!){
114+
deleteArticleTag(id: $id, communityId: $communityId) {
115+
id
116+
}
117+
}
118+
"""
119+
@tag :wip2
120+
test "auth user can delete tag", ~m(tag_attrs community user)a do
121+
{:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user)
122+
123+
variables = %{id: article_tag.id, communityId: community.id}
124+
125+
rule_conn =
126+
simu_conn(:user,
127+
cms: %{community.title => %{"post.article_tag.delete" => true}}
128+
)
129+
130+
deleted = rule_conn |> mutation_result(@delete_tag_query, variables, "deleteArticleTag")
131+
132+
assert deleted["id"] == to_string(article_tag.id)
133+
end
134+
135+
@tag :wip2
136+
test "unauth user delete tag fails", ~m(tag_attrs community user_conn guest_conn user)a do
137+
{:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user)
138+
139+
variables = %{id: article_tag.id, communityId: community.id}
140+
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
141+
142+
assert user_conn |> mutation_get_error?(@delete_tag_query, variables, ecode(:passport))
143+
144+
assert guest_conn
145+
|> mutation_get_error?(@delete_tag_query, variables, ecode(:account_login))
146+
147+
assert rule_conn |> mutation_get_error?(@delete_tag_query, variables, ecode(:passport))
148+
end
149+
end
150+
end

test/groupher_server_web/mutation/cms/article_tags/job_tag_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.JobTag do
2929
}
3030
}
3131
"""
32-
@tag :wip2
32+
3333
test "auth user can set a valid tag to job", ~m(community job tag_attrs user)a do
3434
{:ok, article_tag} = CMS.create_article_tag(community, :job, tag_attrs, user)
3535

@@ -58,7 +58,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.JobTag do
5858
}
5959
}
6060
"""
61-
@tag :wip2
61+
6262
test "can unset tag to a job", ~m(community job tag_attrs tag_attrs2 user)a do
6363
{:ok, article_tag} = CMS.create_article_tag(community, :job, tag_attrs, user)
6464
{:ok, article_tag2} = CMS.create_article_tag(community, :job, tag_attrs2, user)

test/groupher_server_web/mutation/cms/article_tags/post_tag_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.PostTag do
2929
}
3030
}
3131
"""
32-
@tag :wip2
32+
3333
test "auth user can set a valid tag to post", ~m(community post tag_attrs user)a do
3434
{:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user)
3535

@@ -58,7 +58,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.PostTag do
5858
}
5959
}
6060
"""
61-
@tag :wip2
61+
6262
test "can unset tag to a post", ~m(community post tag_attrs tag_attrs2 user)a do
6363
{:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user)
6464
{:ok, article_tag2} = CMS.create_article_tag(community, :post, tag_attrs2, user)

test/groupher_server_web/mutation/cms/article_tags/repo_tag_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.RepoTag do
2929
}
3030
}
3131
"""
32-
@tag :wip2
32+
3333
test "auth user can set a valid tag to repo", ~m(community repo tag_attrs user)a do
3434
{:ok, article_tag} = CMS.create_article_tag(community, :repo, tag_attrs, user)
3535

@@ -58,7 +58,7 @@ defmodule GroupherServer.Test.Mutation.ArticleTags.RepoTag do
5858
}
5959
}
6060
"""
61-
@tag :wip2
61+
6262
test "can unset tag to a repo", ~m(community repo tag_attrs tag_attrs2 user)a do
6363
{:ok, article_tag} = CMS.create_article_tag(community, :repo, tag_attrs, user)
6464
{:ok, article_tag2} = CMS.create_article_tag(community, :repo, tag_attrs2, user)

0 commit comments

Comments
 (0)