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

Commit 445881c

Browse files
committed
refactor(warnings): rm warnings about update_all & Multi task
1 parent fe1c22d commit 445881c

File tree

10 files changed

+36
-37
lines changed

10 files changed

+36
-37
lines changed

lib/helper/orm.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ defmodule Helper.ORM do
8787
defp inc_views_count(content, queryable) do
8888
{1, [result]} =
8989
Repo.update_all(
90-
from(p in queryable, where: p.id == ^content.id),
91-
[inc: [views: 1]],
92-
returning: [:views]
90+
from(p in queryable, where: p.id == ^content.id, select: p.views),
91+
inc: [views: 1]
9392
)
9493

95-
put_in(content.views, result.views)
94+
put_in(content.views, result)
9695
end
9796

9897
@doc """

lib/mastani_server/accounts/delegates/fans.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule MastaniServer.Accounts.Delegate.Fans do
3131
:create_following,
3232
UserFollowing.changeset(%UserFollowing{}, %{user_id: user_id, following_id: follower_id})
3333
)
34-
|> Multi.run(:add_achievement, fn _ ->
34+
|> Multi.run(:add_achievement, fn _, _ ->
3535
Accounts.achieve(%User{id: follower_id}, :add, :follow)
3636
end)
3737
|> Repo.transaction()
@@ -70,13 +70,13 @@ defmodule MastaniServer.Accounts.Delegate.Fans do
7070
with true <- to_string(user_id) !== to_string(follower_id),
7171
{:ok, _follow_user} <- ORM.find(User, follower_id) do
7272
Multi.new()
73-
|> Multi.run(:delete_follower, fn _ ->
73+
|> Multi.run(:delete_follower, fn _, _ ->
7474
ORM.findby_delete(UserFollower, %{user_id: follower_id, follower_id: user_id})
7575
end)
76-
|> Multi.run(:delete_following, fn _ ->
76+
|> Multi.run(:delete_following, fn _, _ ->
7777
ORM.findby_delete(UserFollowing, %{user_id: user_id, following_id: follower_id})
7878
end)
79-
|> Multi.run(:minus_achievement, fn _ ->
79+
|> Multi.run(:minus_achievement, fn _, _ ->
8080
Accounts.achieve(%User{id: follower_id}, :minus, :follow)
8181
end)
8282
|> Repo.transaction()

lib/mastani_server/accounts/delegates/favorite_category.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
4040
def delete_favorite_category(%User{id: user_id}, id) do
4141
with {:ok, category} <- FavoriteCategory |> ORM.find_by(~m(id user_id)a) do
4242
Multi.new()
43-
|> Multi.run(:downgrade_achievement, fn _ ->
43+
|> Multi.run(:downgrade_achievement, fn _, _ ->
4444
# find user favvoried-contents(posts & jobs & videos) 's author,
4545
# and downgrade their's acieveents
4646
# NOTE: this is too fucking violent and should be refactor later
@@ -67,7 +67,7 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
6767

6868
{:ok, %{done: true}}
6969
end)
70-
|> Multi.run(:delete_category, fn _ ->
70+
|> Multi.run(:delete_category, fn _, _ ->
7171
category |> ORM.delete()
7272
end)
7373
|> Repo.transaction()
@@ -138,7 +138,7 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
138138
with {:ok, favorite_category} <-
139139
FavoriteCategory |> ORM.find_by(%{user_id: user.id, id: category_id}) do
140140
Multi.new()
141-
|> Multi.run(:favorite_content, fn _ ->
141+
|> Multi.run(:favorite_content, fn _, _ ->
142142
with {:ok, content_favorite} <- find_content_favorite(thread, content_id, user.id) do
143143
check_dup_category(content_favorite, favorite_category)
144144
else
@@ -149,7 +149,7 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
149149
end
150150
end
151151
end)
152-
|> Multi.run(:dec_old_category_count, fn %{favorite_content: content_favorite} ->
152+
|> Multi.run(:dec_old_category_count, fn _, %{favorite_content: content_favorite} ->
153153
with false <- is_nil(content_favorite.category_id),
154154
{:ok, old_category} <- FavoriteCategory |> ORM.find(content_favorite.category_id) do
155155
old_category
@@ -159,10 +159,10 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
159159
error -> {:error, error}
160160
end
161161
end)
162-
|> Multi.run(:update_content_category_id, fn %{favorite_content: content_favorite} ->
162+
|> Multi.run(:update_content_category_id, fn _, %{favorite_content: content_favorite} ->
163163
content_favorite |> ORM.update(%{category_id: favorite_category.id})
164164
end)
165-
|> Multi.run(:update_category_info, fn _ ->
165+
|> Multi.run(:update_category_info, fn _, _ ->
166166
last_updated = Timex.today() |> Timex.to_datetime()
167167

168168
favorite_category
@@ -199,10 +199,10 @@ defmodule MastaniServer.Accounts.Delegate.FavoriteCategory do
199199
with {:ok, favorite_category} <-
200200
FavoriteCategory |> ORM.find_by(%{user_id: user.id, id: category_id}) do
201201
Multi.new()
202-
|> Multi.run(:undo_favorite_action, fn _ ->
202+
|> Multi.run(:undo_favorite_action, fn _, _ ->
203203
CMS.undo_reaction(thread, :favorite, content_id, user)
204204
end)
205-
|> Multi.run(:update_category_info, fn _ ->
205+
|> Multi.run(:update_category_info, fn _, _ ->
206206
last_updated = Timex.today() |> Timex.to_datetime()
207207

208208
favorite_category

lib/mastani_server/accounts/delegates/profile.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
126126

127127
defp register_github_user(github_profile) do
128128
Multi.new()
129-
|> Multi.run(:create_user, fn _ ->
129+
|> Multi.run(:create_user, fn _, _ ->
130130
create_user(github_profile, :github)
131131
end)
132-
|> Multi.run(:create_profile, fn %{create_user: user} ->
132+
|> Multi.run(:create_profile, fn _, %{create_user: user} ->
133133
create_profile(user, github_profile, :github)
134134
end)
135-
|> Multi.run(:init_achievement, fn %{create_user: user} ->
135+
|> Multi.run(:init_achievement, fn _, %{create_user: user} ->
136136
Achievement |> ORM.upsert_by([user_id: user.id], %{user_id: user.id})
137137
end)
138138
|> Repo.transaction()

lib/mastani_server/cms/delegates/article_curd.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
6868
{:ok, action} <- match_action(thread, :community),
6969
{:ok, community} <- ORM.find(Community, community_id) do
7070
Multi.new()
71-
|> Multi.run(:add_content_author, fn _ ->
71+
|> Multi.run(:add_content_author, fn _, _ ->
7272
action.target
7373
|> struct()
7474
|> action.target.changeset(attrs)
7575
|> Ecto.Changeset.put_change(:author_id, author.id)
7676
|> Repo.insert()
7777
end)
78-
|> Multi.run(:set_community, fn %{add_content_author: content} ->
78+
|> Multi.run(:set_community, fn _, %{add_content_author: content} ->
7979
ArticleOperation.set_community(community, thread, content.id)
8080
end)
81-
|> Multi.run(:set_topic, fn %{add_content_author: content} ->
81+
|> Multi.run(:set_topic, fn _, %{add_content_author: content} ->
8282
topic_title =
8383
case attrs |> Map.has_key?(:topic) do
8484
true -> attrs.topic
@@ -87,7 +87,7 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
8787

8888
ArticleOperation.set_topic(%Topic{title: topic_title}, thread, content.id)
8989
end)
90-
|> Multi.run(:set_community_flag, fn %{add_content_author: content} ->
90+
|> Multi.run(:set_community_flag, fn _, %{add_content_author: content} ->
9191
# TODO: remove this judge, as content should have a flag
9292
case action |> Map.has_key?(:flag) do
9393
true ->
@@ -99,17 +99,17 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
9999
{:ok, "pass"}
100100
end
101101
end)
102-
|> Multi.run(:set_tag, fn %{add_content_author: content} ->
102+
|> Multi.run(:set_tag, fn _, %{add_content_author: content} ->
103103
case attrs |> Map.has_key?(:tags) do
104104
true -> set_tags(community, thread, content.id, attrs.tags)
105105
false -> {:ok, "pass"}
106106
end
107107
end)
108-
|> Multi.run(:mention_users, fn %{add_content_author: content} ->
108+
|> Multi.run(:mention_users, fn _, %{add_content_author: content} ->
109109
Delivery.mention_from_content(thread, content, attrs, %User{id: user_id})
110110
{:ok, "pass"}
111111
end)
112-
|> Multi.run(:log_action, fn _ ->
112+
|> Multi.run(:log_action, fn _, _ ->
113113
Statistics.log_publish_action(%User{id: user_id})
114114
end)
115115
|> Repo.transaction()

lib/mastani_server/cms/delegates/article_reaction.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ defmodule MastaniServer.CMS.Delegate.ArticleReaction do
2121
{:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]),
2222
{:ok, user} <- ORM.find(Accounts.User, user_id) do
2323
Multi.new()
24-
|> Multi.run(:create_reaction_record, fn _ ->
24+
|> Multi.run(:create_reaction_record, fn _, _ ->
2525
create_reaction_record(action, user, thread, content)
2626
end)
27-
|> Multi.run(:add_achievement, fn _ ->
27+
|> Multi.run(:add_achievement, fn _, _ ->
2828
achiever_id = content.author.user_id
2929
Accounts.achieve(%User{id: achiever_id}, :add, react)
3030
end)
@@ -59,10 +59,10 @@ defmodule MastaniServer.CMS.Delegate.ArticleReaction do
5959
{:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]),
6060
{:ok, user} <- ORM.find(Accounts.User, user_id) do
6161
Multi.new()
62-
|> Multi.run(:delete_reaction_record, fn _ ->
62+
|> Multi.run(:delete_reaction_record, fn _, _ ->
6363
delete_reaction_record(action, user, thread, content)
6464
end)
65-
|> Multi.run(:minus_achievement, fn _ ->
65+
|> Multi.run(:minus_achievement, fn _, _ ->
6666
achiever_id = content.author.user_id
6767
Accounts.achieve(%User{id: achiever_id}, :minus, react)
6868
end)

lib/mastani_server/cms/delegates/comment_curd.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ defmodule MastaniServer.CMS.Delegate.CommentCURD do
4444
with {:ok, action} <- match_action(thread, :comment),
4545
{:ok, comment} <- ORM.find(action.reactor, content_id) do
4646
Multi.new()
47-
|> Multi.run(:delete_comment, fn _ ->
47+
|> Multi.run(:delete_comment, fn _, _ ->
4848
ORM.delete(comment)
4949
end)
50-
|> Multi.run(:update_floor, fn _ ->
50+
|> Multi.run(:update_floor, fn _, _ ->
5151
Repo.update_all(
5252
from(p in action.reactor, where: p.id > ^comment.id),
5353
inc: [floor: -1]

lib/mastani_server/cms/delegates/community_operation.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule MastaniServer.CMS.Delegate.CommunityOperation do
6868
:insert_editor,
6969
CommunityEditor.changeset(%CommunityEditor{}, ~m(user_id community_id title)a)
7070
)
71-
|> Multi.run(:stamp_passport, fn _ ->
71+
|> Multi.run(:stamp_passport, fn _, _ ->
7272
rules = Certification.passport_rules(cms: title)
7373
PassportCURD.stamp_passport(rules, %User{id: user_id})
7474
end)

lib/mastani_server/statistics/delegates/contribute.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ defmodule MastaniServer.Statistics.Delegate.Contribute do
141141
defp do_inc_count(query, contribute, count \\ 1) do
142142
{1, [result]} =
143143
Repo.update_all(
144-
query,
145-
[inc: [count: count]],
146-
returning: [:count]
144+
from(p in query, select: p.count),
145+
inc: [count: count]
147146
)
148147

149-
put_in(contribute.count, result.count)
148+
put_in(contribute.count, result)
150149
end
151150
end

test/mastani_server_web/query/accounts/account_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defmodule MastaniServer.Test.Query.Account.Basic do
5252
assert results["id"] == to_string(user.id)
5353
end
5454

55+
@tag :wip
5556
test "user's views +1 after visit", ~m(guest_conn user)a do
5657
{:ok, target_user} = ORM.find(Accounts.User, user.id)
5758
assert target_user.views == 0

0 commit comments

Comments
 (0)