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

Commit 5549752

Browse files
committed
refactor(orm): use ! after-fix to make API clear
1 parent 0023c38 commit 5549752

File tree

9 files changed

+24
-19
lines changed

9 files changed

+24
-19
lines changed

cover/excoveralls.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/groupher_server/accounts/delegates/fans.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
7575
{:ok, _follow_user} <- ORM.find(User, follower_id) do
7676
Multi.new()
7777
|> Multi.run(:delete_follower, fn _, _ ->
78-
ORM.findby_delete(UserFollower, %{user_id: follower_id, follower_id: user_id})
78+
ORM.findby_delete!(UserFollower, %{user_id: follower_id, follower_id: user_id})
7979
end)
8080
|> Multi.run(:delete_following, fn _, _ ->
81-
ORM.findby_delete(UserFollowing, %{user_id: user_id, following_id: follower_id})
81+
ORM.findby_delete!(UserFollowing, %{user_id: user_id, following_id: follower_id})
8282
end)
8383
|> Multi.run(:minus_achievement, fn _, _ ->
8484
Accounts.achieve(%User{id: follower_id}, :minus, :follow)

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
351351
article_comment_id: comment.id,
352352
user_id: user_id
353353
})
354-
355-
{:ok, :pass}
356354
end)
357355
|> Multi.run(:desc_upvotes_count, fn _, _ ->
358356
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)

lib/groupher_server/cms/delegates/comment_reaction.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule GroupherServer.CMS.Delegate.CommentReaction do
3737
with {:ok, action} <- match_action(thread, feeling) do
3838
clause = Map.merge(%{user_id: user_id}, merge_thread_comment_id(thread, comment_id))
3939
# clause = %{post_comment_id: comment_id, user_id: user_id}
40-
ORM.findby_delete(action.reactor, clause)
40+
ORM.findby_delete!(action.reactor, clause)
4141
ORM.find(action.target, comment_id)
4242
end
4343
end

lib/groupher_server/cms/delegates/community_operation.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
3535
"""
3636
def unset_category(%Community{id: community_id}, %Category{id: category_id}) do
3737
with {:ok, community_category} <-
38-
CommunityCategory |> ORM.findby_delete(~m(community_id category_id)a) do
38+
CommunityCategory |> ORM.findby_delete!(~m(community_id category_id)a) do
3939
Community |> ORM.find(community_category.community_id)
4040
end
4141
end
@@ -54,7 +54,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
5454
"""
5555
def unset_thread(%Community{id: community_id}, %Thread{id: thread_id}) do
5656
with {:ok, community_thread} <-
57-
CommunityThread |> ORM.findby_delete(~m(community_id thread_id)a) do
57+
CommunityThread |> ORM.findby_delete!(~m(community_id thread_id)a) do
5858
Community |> ORM.find(community_thread.community_id)
5959
end
6060
end
@@ -80,7 +80,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
8080
unset a community editor
8181
"""
8282
def unset_editor(%Community{id: community_id}, %User{id: user_id}) do
83-
with {:ok, _} <- ORM.findby_delete(CommunityEditor, ~m(user_id community_id)a),
83+
with {:ok, _} <- ORM.findby_delete!(CommunityEditor, ~m(user_id community_id)a),
8484
{:ok, _} <- PassportCURD.delete_passport(%User{id: user_id}) do
8585
User |> ORM.find(user_id)
8686
end
@@ -132,7 +132,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
132132
with {:ok, community} <- ORM.find(Community, community_id),
133133
true <- community.raw !== "home",
134134
{:ok, record} <-
135-
ORM.findby_delete(CommunitySubscriber, community_id: community.id, user_id: user_id) do
135+
ORM.findby_delete!(CommunitySubscriber, community_id: community.id, user_id: user_id) do
136136
Community |> ORM.find(record.community_id)
137137
else
138138
false ->
@@ -151,7 +151,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
151151
with {:ok, community} <- ORM.find(Community, community_id),
152152
true <- community.raw !== "home",
153153
{:ok, record} <-
154-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
154+
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
155155
update_community_geo(community_id, user_id, remote_ip, :dec)
156156
Community |> ORM.find(record.community_id)
157157
else
@@ -171,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
171171
with {:ok, community} <- ORM.find(Community, community_id),
172172
true <- community.raw !== "home",
173173
{:ok, record} <-
174-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
174+
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
175175
update_community_geo_map(community.id, city, :dec)
176176
Community |> ORM.find(record.community_id)
177177
else

lib/groupher_server/cms/delegates/passport_curd.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule GroupherServer.CMS.Delegate.PassportCURD do
6565
end
6666

6767
def delete_passport(%Accounts.User{id: user_id}) do
68-
ORM.findby_delete(UserPasport, ~m(user_id)a)
68+
ORM.findby_delete!(UserPasport, ~m(user_id)a)
6969
end
7070

7171
defp reject_invalid_rules(rules) when is_map(rules) do

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
4141

4242
def update_community(_root, args, _info), do: Community |> ORM.find_update(args)
4343

44-
def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete(id)
44+
def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete!(id)
4545

4646
# #######################
4747
# community thread (post, job), login user should be logged
@@ -178,7 +178,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
178178
CMS.create_category(%{title: title, raw: raw}, user)
179179
end
180180

181-
def delete_category(_root, %{id: id}, _info), do: Category |> ORM.find_delete(id)
181+
def delete_category(_root, %{id: id}, _info), do: Category |> ORM.find_delete!(id)
182182

183183
def update_category(_root, ~m(id title)a, %{context: %{cur_user: _}}) do
184184
CMS.update_category(~m(%Category id title)a)
@@ -243,7 +243,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
243243
CMS.create_tag(%Community{id: community_id}, thread, args, user)
244244
end
245245

246-
def delete_tag(_root, %{id: id}, _info), do: Tag |> ORM.find_delete(id)
246+
def delete_tag(_root, %{id: id}, _info), do: Tag |> ORM.find_delete!(id)
247247

248248
def update_tag(_root, args, _info), do: CMS.update_tag(args)
249249

lib/helper/orm.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,25 @@ defmodule Helper.ORM do
121121
"""
122122
def delete(content), do: Repo.delete(content)
123123

124-
def find_delete(queryable, id) do
124+
def find_delete!(queryable, id) do
125125
with {:ok, content} <- find(queryable, id) do
126126
delete(content)
127127
end
128128
end
129129

130-
def findby_delete(queryable, clauses) do
130+
def findby_delete!(queryable, clauses) do
131131
with {:ok, content} <- find_by(queryable, clauses) do
132132
delete(content)
133133
end
134134
end
135135

136+
def findby_delete(queryable, clauses) do
137+
case find_by(queryable, clauses) do
138+
{:ok, content} -> delete(content)
139+
_ -> {:ok, :pass}
140+
end
141+
end
142+
136143
def findby_or_insert(queryable, clauses, attrs) do
137144
case queryable |> find_by(clauses) do
138145
{:ok, content} ->

test/groupher_server/cms/post_comment_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule GroupherServer.Test.PostComment do
124124
{:ok, reply} =
125125
CMS.reply_comment(:post, comment.id, %{community: community.raw, body: reply_body}, user)
126126

127-
PostComment |> ORM.find_delete(comment.id)
127+
PostComment |> ORM.find_delete!(comment.id)
128128

129129
{:error, _} = ORM.find(PostComment, comment.id)
130130
{:error, _} = ORM.find(PostComment, reply.id)

0 commit comments

Comments
 (0)