Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit1fd6000

Browse files
committed
refactor(article-comments): undo_upvote_article_comment
1 parentc0bbf97 commit1fd6000

File tree

4 files changed

+93
-45
lines changed

4 files changed

+93
-45
lines changed

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ defmodule GroupherServer.CMS do
124124

125125
defdelegatecreate_article_comment(thread,article_id,args,user),to:ArticleComment
126126
defdelegateupvote_article_comment(comment_id,user),to:ArticleComment
127+
defdelegateundo_upvote_article_comment(comment_id,user),to:ArticleComment
127128
defdelegatedelete_article_comment(comment_id,user),to:ArticleComment
128129
defdelegatereply_article_comment(comment_id,args,user),to:ArticleComment
129130

‎lib/groupher_server/cms/delegates/article_comment.ex‎

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -334,67 +334,55 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
334334
ORM.update(comment,%{upvotes_count:upvotes_count})
335335
end)
336336
|>Multi.run(:check_article_author_upvoted,fn_,_->
337-
with{:ok,article}=get_full_comment(comment_id)do
338-
is_article_author_upvoted=article.author.id==user_id
339-
340-
caseis_article_author_upvoteddo
341-
true->
342-
new_meta=
343-
comment.meta
344-
|>Map.from_struct()
345-
|>Map.merge(%{is_article_author_upvoted:true})
346-
347-
comment|>ORM.update(%{meta:new_meta})
348-
349-
false->
350-
{:ok,:pass}
351-
end
352-
end
337+
update_article_author_upvoted_info(comment,user_id)
353338
end)
354339
|>Repo.transaction()
355340
|>upsert_comment_result()
356341
end
357342
end
358343

359-
@specget_full_comment(String.t())::{:ok,T.article_info()}|{:error,nil}
360-
defpget_full_comment(comment_id)do
361-
query=from(cinArticleComment,where:c.id==^comment_id,preload::post,preload::job)
344+
@doc"upvote a comment"
345+
defundo_upvote_article_comment(comment_id,%User{id:user_id})do
346+
with{:ok,comment}<-ORM.find(ArticleComment,comment_id),
347+
false<-comment.is_deleteddo
348+
Multi.new()
349+
|>Multi.run(:delete_comment_upvote,fn_,_->
350+
ORM.findby_delete(ArticleCommentUpvote,%{
351+
article_comment_id:comment.id,
352+
user_id:user_id
353+
})
354+
end)
355+
|>Multi.run(:desc_upvotes_count,fn_,_->
356+
count_query=from(cinArticleCommentUpvote,where:c.article_comment_id==^comment_id)
357+
upvotes_count=Repo.aggregate(count_query,:count)
362358

363-
with{:ok,comment}<-Repo.one(query)|>done()do
364-
extract_article_info(comment)
359+
ORM.update(comment,%{upvotes_count:Enum.max([upvotes_count-1,0])})
360+
end)
361+
|>Multi.run(:check_article_author_upvoted,fn_,%{desc_upvotes_count:updated_comment}->
362+
update_article_author_upvoted_info(updated_comment,user_id)
363+
end)
364+
|>Repo.transaction()
365+
|>upsert_comment_result()
365366
end
366367
end
367368

368-
defpextract_article_info(%ArticleComment{post:%Post{}=post})whennotis_nil(post)do
369-
do_extract_article_info(:post,post)
370-
end
371-
372-
defpextract_article_info(%ArticleComment{job:%Job{}=job})whennotis_nil(job)do
373-
do_extract_article_info(:job,job)
374-
end
369+
defpupdate_article_author_upvoted_info(%ArticleComment{}=comment,user_id)do
370+
with{:ok,article}=get_full_comment(comment.id)do
371+
is_article_author_upvoted=article.author.id==user_id
375372

376-
@specdo_extract_article_info(T.article_thread(),T.article_common())::{:ok,T.article_info()}
377-
defpdo_extract_article_info(thread,article)do
378-
with{:ok,article_with_author}<-Repo.preload(article,author::user)|>done(),
379-
article_author<-get_in(article_with_author,[:author,:user])do
380-
#
381-
article_info=%{title:article.title}
382-
383-
author_info=%{
384-
id:article_author.id,
385-
login:article_author.login,
386-
nickname:article_author.nickname
387-
}
373+
new_meta=
374+
comment.meta
375+
|>Map.from_struct()
376+
|>Map.merge(%{is_article_author_upvoted:is_article_author_upvoted})
388377

389-
{:ok,%{thread:thread,article:article_info,author:author_info}}
378+
comment|>ORM.update(%{meta:new_meta})
390379
end
391380
end
392381

393382
# creat article comment for parent or reply
394383
# set floor
395384
# TODO: parse editor-json
396385
# set default emotions
397-
# TODO: meta
398386
defpdo_create_comment(
399387
content,
400388
foreign_key,
@@ -511,9 +499,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
511499
%{paged_comments|entries:new_entries}
512500
end
513501

502+
@specget_full_comment(String.t())::{:ok,T.article_info()}|{:error,nil}
503+
defpget_full_comment(comment_id)do
504+
query=from(cinArticleComment,where:c.id==^comment_id,preload::post,preload::job)
505+
506+
with{:ok,comment}<-Repo.one(query)|>done()do
507+
extract_article_info(comment)
508+
end
509+
end
510+
511+
defpextract_article_info(%ArticleComment{post:%Post{}=post})whennotis_nil(post)do
512+
do_extract_article_info(:post,post)
513+
end
514+
515+
defpextract_article_info(%ArticleComment{job:%Job{}=job})whennotis_nil(job)do
516+
do_extract_article_info(:job,job)
517+
end
518+
519+
@specdo_extract_article_info(T.article_thread(),T.article_common())::{:ok,T.article_info()}
520+
defpdo_extract_article_info(thread,article)do
521+
with{:ok,article_with_author}<-Repo.preload(article,author::user)|>done(),
522+
article_author<-get_in(article_with_author,[:author,:user])do
523+
#
524+
article_info=%{title:article.title}
525+
526+
author_info=%{
527+
id:article_author.id,
528+
login:article_author.login,
529+
nickname:article_author.nickname
530+
}
531+
532+
{:ok,%{thread:thread,article:article_info,author:author_info}}
533+
end
534+
end
535+
514536
defpupsert_comment_result({:ok,%{create_article_comment:result}}),do:{:ok,result}
515537
defpupsert_comment_result({:ok,%{add_reply_to:result}}),do:{:ok,result}
516-
defpupsert_comment_result({:ok,%{inc_upvotes_count:result}}),do:{:ok,result}
538+
defpupsert_comment_result({:ok,%{check_article_author_upvoted:result}}),do:{:ok,result}
517539
defpupsert_comment_result({:ok,%{update_report_flag:result}}),do:{:ok,result}
518540
defpupsert_comment_result({:ok,%{update_comment_emotion:result}}),do:{:ok,result}
519541

‎lib/helper/orm.ex‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ defmodule Helper.ORM do
128128
end
129129

130130
deffindby_delete(queryable,clauses)do
131-
with{:ok,content}<-find_by(queryable,clauses)do
132-
delete(content)
131+
casefind_by(queryable,clauses)do
132+
{:ok,content}->delete(content)
133+
{:error,_}->{:ok,:pass}
133134
end
134135
end
135136

‎test/groupher_server/cms/article_comment_test.exs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
165165
{:ok,comment}=ORM.find(ArticleComment,comment.id)
166166
assertcomment.upvotes_count==2
167167
end
168+
169+
@tag:wip2
170+
test"user can undo upvote a post comment",~m(user post)ado
171+
content="post_comment"
172+
{:ok,comment}=CMS.create_article_comment(:post,post.id,content,user)
173+
CMS.upvote_article_comment(comment.id,user)
174+
175+
{:ok,comment}=ORM.find(ArticleComment,comment.id,preload::upvotes)
176+
assert1==length(comment.upvotes)
177+
178+
{:ok,comment}=CMS.undo_upvote_article_comment(comment.id,user)
179+
assert0==comment.upvotes_count
180+
end
181+
182+
@tag:wip2
183+
test"user can undo upvote a post comment with no upvote",~m(user post)ado
184+
content="post_comment"
185+
{:ok,comment}=CMS.create_article_comment(:post,post.id,content,user)
186+
{:ok,comment}=CMS.undo_upvote_article_comment(comment.id,user)
187+
assert0==comment.upvotes_count
188+
189+
{:ok,comment}=CMS.undo_upvote_article_comment(comment.id,user)
190+
assert0==comment.upvotes_count
191+
end
168192
end
169193

170194
describe"[article comment fold/unfold]"do

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp