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

Commit 5b2f45e

Browse files
committed
refactor(article-emotions): mutation for post/job
1 parent 842657a commit 5b2f45e

File tree

16 files changed

+261
-33
lines changed

16 files changed

+261
-33
lines changed

lib/groupher_server/cms/delegates/article_comment_emotion.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
5656
user_id: user.id
5757
}
5858

59-
{:ok, article_comment_user_emotion} = ORM.find_by(ArticleCommentUserEmotion, target)
60-
args = Map.put(target, :"#{emotion}", false)
61-
article_comment_user_emotion |> ORM.update(args)
59+
case ORM.find_by(ArticleCommentUserEmotion, target) do
60+
{:ok, article_comment_user_emotion} ->
61+
args = Map.put(target, :"#{emotion}", false)
62+
article_comment_user_emotion |> ORM.update(args)
63+
64+
{:error, _} ->
65+
ORM.create(ArticleCommentUserEmotion, target)
66+
end
6267
end)
6368
|> Multi.run(:query_emotion_states, fn _, _ ->
6469
query_emotion_states(comment, emotion)

lib/groupher_server/cms/delegates/article_emotion.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
5555
%{recived_user_id: article.author.user_id, user_id: user.id}
5656
|> Map.put(info.foreign_key, article_id)
5757

58-
{:ok, article_user_emotion} = ORM.find_by(ArticleUserEmotion, target)
59-
args = Map.put(target, :"#{emotion}", false)
60-
article_user_emotion |> ORM.update(args)
58+
case ORM.find_by(ArticleUserEmotion, target) do
59+
{:ok, article_user_emotion} ->
60+
args = Map.put(target, :"#{emotion}", false)
61+
article_user_emotion |> ORM.update(args)
62+
63+
{:error, _} ->
64+
# args = Map.put(target, :"#{emotion}", false)
65+
args = target
66+
ORM.create(ArticleUserEmotion, args)
67+
end
6168
end)
6269
|> Multi.run(:query_emotion_status, fn _, _ ->
6370
query_emotion_status(thread, article.id, emotion)

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
128128
CMS.collected_users(thread, id, filter)
129129
end
130130

131+
def emotion_to_article(_root, ~m(id thread emotion)a, %{context: %{cur_user: user}}) do
132+
CMS.emotion_to_article(thread, id, emotion, user)
133+
end
134+
135+
def undo_emotion_to_article(_root, ~m(id thread emotion)a, %{context: %{cur_user: user}}) do
136+
CMS.undo_emotion_to_article(thread, id, emotion, user)
137+
end
138+
131139
# #######################
132140
# category ..
133141
# #######################

lib/groupher_server_web/schema/Helper/fields.ex

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
77
alias GroupherServer.{Accounts, CMS}
88

99
@page_size get_config(:general, :page_size)
10-
@supported_emotions get_config(:article, :comment_supported_emotions)
10+
@supported_emotions get_config(:article, :supported_emotions)
11+
@supported_comment_emotions get_config(:article, :comment_supported_emotions)
1112
@supported_collect_folder_threads Accounts.CollectFolder.supported_threads()
1213

1314
defmacro timestamp_fields do
@@ -192,7 +193,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
192193
end
193194

194195
@doc """
195-
general emotion enum for comments
196+
general emotion enum for articles
196197
#NOTE: xxx_user_logins field is not support for gq-endpoint
197198
"""
198199
defmacro emotion_enum() do
@@ -205,7 +206,20 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
205206
end
206207

207208
@doc """
208-
general emotions for comments
209+
general emotion enum for comments
210+
#NOTE: xxx_user_logins field is not support for gq-endpoint
211+
"""
212+
defmacro comment_emotion_enum() do
213+
@supported_comment_emotions
214+
|> Enum.map(fn emotion ->
215+
quote do
216+
value(unquote(:"#{emotion}"))
217+
end
218+
end)
219+
end
220+
221+
@doc """
222+
general emotions for articles
209223
#NOTE: xxx_user_logins field is not support for gq-endpoint
210224
"""
211225
defmacro emotion_fields() do
@@ -219,6 +233,21 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
219233
end)
220234
end
221235

236+
@doc """
237+
general emotions for comments
238+
#NOTE: xxx_user_logins field is not support for gq-endpoint
239+
"""
240+
defmacro comment_emotion_fields() do
241+
@supported_comment_emotions
242+
|> Enum.map(fn emotion ->
243+
quote do
244+
field(unquote(:"#{emotion}_count"), :integer)
245+
field(unquote(:"viewer_has_#{emotion}ed"), :boolean)
246+
field(unquote(:"latest_#{emotion}_users"), list_of(:simple_user))
247+
end
248+
end)
249+
end
250+
222251
@doc """
223252
general collect folder meta info
224253
"""

lib/groupher_server_web/schema/Helper/mutations.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,28 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
100100
end
101101
end
102102
end
103+
104+
defmacro article_emotion_mutation(thread) do
105+
quote do
106+
@desc unquote("emotion to #{thread}")
107+
field unquote(:"emotion_to_#{thread}"), unquote(thread) do
108+
arg(:id, non_null(:id))
109+
arg(:emotion, non_null(:article_emotion))
110+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
111+
112+
middleware(M.Authorize, :login)
113+
resolve(&R.CMS.emotion_to_article/3)
114+
end
115+
116+
@desc unquote("undo emotion to #{thread}")
117+
field unquote(:"undo_emotion_to_#{thread}"), unquote(thread) do
118+
arg(:id, non_null(:id))
119+
arg(:emotion, non_null(:article_emotion))
120+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
121+
122+
middleware(M.Authorize, :login)
123+
resolve(&R.CMS.undo_emotion_to_article/3)
124+
end
125+
end
126+
end
103127
end

lib/groupher_server_web/schema/cms/cms_misc.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
153153
value(:grey)
154154
end
155155

156+
@desc "emotion options of article"
157+
enum(:article_emotion, do: emotion_enum())
158+
156159
@desc "emotion options of comment"
157-
enum :article_comment_emotion do
158-
emotion_enum()
159-
end
160+
enum(:article_comment_emotion, do: comment_emotion_enum())
160161

161162
@desc "the filter mode for list comments"
162163
enum :article_comments_mode do

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
4444
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
4545

4646
field(:meta, :article_meta)
47+
field(:emotions, :article_emotions)
4748

4849
field :comments, list_of(:comment) do
4950
arg(:filter, :members_filter)
@@ -99,6 +100,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
99100
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
100101

101102
field(:meta, :article_meta)
103+
field(:emotions, :article_emotions)
102104

103105
field(:salary, :string)
104106
field(:exp, :string)
@@ -316,6 +318,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
316318
end
317319

318320
object :article_comment_emotions do
321+
comment_emotion_fields()
322+
end
323+
324+
object :article_emotions do
319325
emotion_fields()
320326
end
321327

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
7575
article_pin_mutation(:job)
7676
article_trash_mutation(:job)
7777
article_delete_mutation(:job)
78+
article_emotion_mutation(:job)
7879
#############
7980
end
8081
end

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
4949
article_pin_mutation(:post)
5050
article_trash_mutation(:post)
5151
article_delete_mutation(:post)
52+
article_emotion_mutation(:post)
5253
#############
5354
end
5455
end

test/groupher_server/cms/emotions/job_emotions_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
66
alias Helper.ORM
77
alias GroupherServer.CMS
88

9-
alias CMS.{Embeds, ArticlerEmotion, ArticleUserEmotion}
9+
alias CMS.{Embeds, ArticleUserEmotion}
1010

1111
@default_emotions Embeds.ArticleEmotion.default_emotions()
1212

@@ -22,7 +22,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
2222
end
2323

2424
describe "[emotion in paged jobs]" do
25-
@tag :wip2
25+
@tag :wip3
2626
test "login user should got viewer has emotioned status",
2727
~m(community job_attrs user)a do
2828
total_count = 10
@@ -61,15 +61,15 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
6161
end
6262

6363
describe "[basic article emotion]" do
64-
@tag :wip2
64+
@tag :wip3
6565
test "job has default emotions after created", ~m(community job_attrs user)a do
6666
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
6767

6868
emotions = job.emotions |> Map.from_struct() |> Map.delete(:id)
6969
assert @default_emotions == emotions
7070
end
7171

72-
@tag :wip2
72+
@tag :wip3
7373
test "can make emotion to job", ~m(community job_attrs user user2)a do
7474
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
7575

@@ -83,7 +83,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
8383
assert user_exist_in?(user2, emotions.latest_downvote_users)
8484
end
8585

86-
@tag :wip2
86+
@tag :wip3
8787
test "can undo emotion to job", ~m(community job_attrs user user2)a do
8888
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
8989

@@ -100,7 +100,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
100100
assert not user_exist_in?(user2, emotions.latest_downvote_users)
101101
end
102102

103-
@tag :wip2
103+
@tag :wip3
104104
test "same user make same emotion to same job.", ~m(community job_attrs user)a do
105105
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
106106

@@ -113,7 +113,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
113113
assert user_exist_in?(user, job.emotions.latest_downvote_users)
114114
end
115115

116-
@tag :wip2
116+
@tag :wip3
117117
test "same user same emotion to same job only have one user_emotion record",
118118
~m(community job_attrs user)a do
119119
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
@@ -131,7 +131,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
131131
assert record.heart
132132
end
133133

134-
@tag :wip2
134+
@tag :wip3
135135
test "different user can make same emotions on same job",
136136
~m(community job_attrs user user2 user3)a do
137137
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
@@ -148,7 +148,7 @@ defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
148148
assert user_exist_in?(user3, emotions.latest_beer_users)
149149
end
150150

151-
@tag :wip2
151+
@tag :wip3
152152
test "same user can make differcent emotions on same job", ~m(community job_attrs user)a do
153153
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
154154

0 commit comments

Comments
 (0)