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

Commit 37e9581

Browse files
committed
refactor(article-comment): wip
1 parent f80bec2 commit 37e9581

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

lib/groupher_server/cms/delegates/article_comment_emotion.ex

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
44
"""
55
import Ecto.Query, warn: false
66

7-
# alias Helper.Types, as: T
87
alias Helper.ORM
98
alias GroupherServer.{Accounts, CMS, Repo}
109

@@ -23,10 +22,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
2322
with {:ok, comment} <- ORM.find(ArticleComment, comment_id, preload: :author) do
2423
Multi.new()
2524
|> Multi.run(:create_user_emotion, fn _, _ ->
26-
# dTODO:
27-
# 分两步
28-
# 当前如果有记录,那么直接 emotion 相应置位 boolean 就好
29-
# 如果没有找到记录,那么才就要创建
3025
target = %{
3126
article_comment_id: comment.id,
3227
recived_user_id: comment.author.id,
@@ -35,7 +30,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
3530

3631
args = Map.put(target, :"#{emotion}", true)
3732

38-
ArticleCommentUserEmotion |> ORM.create(args)
33+
case ORM.find_by(ArticleCommentUserEmotion, target) do
34+
{:ok, article_comment_user_emotion} -> article_comment_user_emotion |> ORM.update(args)
35+
{:error, _} -> ArticleCommentUserEmotion |> ORM.create(args)
36+
end
3937
end)
4038
|> Multi.run(:query_emotion_status, fn _, _ ->
4139
query_emotion_status(comment, emotion)
@@ -44,7 +42,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
4442
update_comment_emotion(comment, emotion, status, user)
4543
end)
4644
|> Repo.transaction()
47-
|> upsert_comment_result
45+
|> update_emotion_result
4846
end
4947
end
5048

@@ -69,7 +67,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
6967
update_comment_emotion(comment, emotion, status, user)
7068
end)
7169
|> Repo.transaction()
72-
|> upsert_comment_result
70+
|> update_emotion_result
7371
end
7472
end
7573

@@ -125,9 +123,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
125123
{:ok, Map.merge(comment, %{emotion: emotions})}
126124
end
127125

128-
defp upsert_comment_result({:ok, %{update_comment_emotion: result}}), do: {:ok, result}
126+
defp update_emotion_result({:ok, %{update_comment_emotion: result}}), do: {:ok, result}
129127

130-
defp upsert_comment_result({:error, _, result, _steps}) do
128+
defp update_emotion_result({:error, _, result, _steps}) do
131129
{:error, result}
132130
end
133131
end

test/groupher_server/cms/article_comment_emotions_test.exs

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

9-
alias CMS.{ArticleComment, Embeds}
9+
alias CMS.{ArticleComment, Embeds, ArticleCommentUserEmotion}
1010

1111
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
1212

@@ -115,8 +115,8 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do
115115
assert not user_exist_in?(user2, emotions.latest_downvote_users)
116116
end
117117

118-
@tag :wip3
119-
test "same user make same emotion to same comment", ~m(post user)a do
118+
@tag :wip2
119+
test "same user make same emotion to same comment.", ~m(post user)a do
120120
parent_content = "parent comment"
121121
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user)
122122

@@ -129,7 +129,31 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do
129129
assert user_exist_in?(user, parent_comment.emotions.latest_downvote_users)
130130
end
131131

132-
@tag :wip
132+
@tag :wip2
133+
test "same user same emotion to same comment only have one user_emotion record",
134+
~m(post user)a do
135+
parent_content = "parent comment"
136+
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user)
137+
138+
{:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user)
139+
{:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user)
140+
141+
{:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id)
142+
143+
{:ok, records} = ORM.find_all(ArticleCommentUserEmotion, %{page: 1, size: 10})
144+
assert records.total_count == 1
145+
146+
{:ok, record} =
147+
ORM.find_by(ArticleCommentUserEmotion, %{
148+
article_comment_id: parent_comment.id,
149+
user_id: user.id
150+
})
151+
152+
assert record.downvote
153+
assert record.heart
154+
end
155+
156+
@tag :wip2
133157
test "different user can make same emotions on same comment", ~m(post user user2 user3)a do
134158
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)
135159

0 commit comments

Comments
 (0)