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

Commit 3ca670b

Browse files
authored
refactor: merge pr #173 from coderplanets/clean-code
Clean code
2 parents 63d20bb + f0d2555 commit 3ca670b

File tree

23 files changed

+155
-166
lines changed

23 files changed

+155
-166
lines changed

lib/groupher_server/accounts/delegates/fans.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
4040
false ->
4141
{:error, [message: "can't follow yourself", code: ecode(:self_conflict)]}
4242

43-
{:error, error} ->
44-
{:error, [message: error, code: ecode(:not_exsit)]}
43+
{:error, reason} ->
44+
{:error, [message: reason, code: ecode(:not_exsit)]}
4545
end
4646
end
4747

@@ -89,8 +89,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
8989
false ->
9090
{:error, [message: "can't undo follow yourself", code: ecode(:self_conflict)]}
9191

92-
{:error, error} ->
93-
{:error, [message: error, code: ecode(:not_exsit)]}
92+
{:error, reason} ->
93+
{:error, [message: reason, code: ecode(:not_exsit)]}
9494
end
9595
end
9696

lib/groupher_server/accounts/delegates/favorite_category.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ defmodule GroupherServer.Accounts.Delegate.FavoriteCategory do
149149
{:error, _} ->
150150
case CMS.reaction(thread, :favorite, content_id, user) do
151151
{:ok, _} -> find_content_favorite(thread, content_id, user.id)
152-
{:error, error} -> {:error, error}
152+
{:error, reason} -> {:error, reason}
153153
end
154154
end
155155
end)
@@ -160,7 +160,7 @@ defmodule GroupherServer.Accounts.Delegate.FavoriteCategory do
160160
|> ORM.update(%{total_count: max(old_category.total_count - 1, 0)})
161161
else
162162
true -> {:ok, ""}
163-
error -> {:error, error}
163+
reason -> {:error, reason}
164164
end
165165
end)
166166
|> Multi.run(:update_content_category_id, fn _, %{favorite_content: content_favorite} ->

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ defmodule GroupherServer.CMS do
8484
defdelegate favorited_category(thread, content_id, user), to: FavoritedContents
8585
# ArticleOperation
8686
# >> set flag on article, like: pin / unpin article
87-
defdelegate set_community_flags(queryable, community_id, attrs), to: ArticleOperation
87+
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
8888
defdelegate pin_content(queryable, community_id, topic), to: ArticleOperation
8989
defdelegate undo_pin_content(queryable, community_id, topic), to: ArticleOperation
9090
defdelegate pin_content(queryable, community_id), to: ArticleOperation

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 93 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -63,60 +63,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6363
{:error, %Ecto.Changeset{}}
6464
6565
"""
66-
def create_content(
67-
%Community{id: community_id},
68-
thread,
69-
attrs,
70-
%User{id: user_id}
71-
) do
72-
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
66+
def create_content(%Community{id: cid}, thread, attrs, %User{id: uid}) do
67+
with {:ok, author} <- ensure_author_exists(%User{id: uid}),
7368
{:ok, action} <- match_action(thread, :community),
74-
{:ok, community} <- ORM.find(Community, community_id) do
69+
{:ok, community} <- ORM.find(Community, cid) do
7570
Multi.new()
7671
|> Multi.run(:create_content, fn _, _ ->
77-
action.target
78-
|> struct()
79-
|> action.target.changeset(attrs)
80-
|> Ecto.Changeset.put_change(:author_id, author.id)
81-
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(community_id))
82-
|> Repo.insert()
72+
exec_create_content(action.target, attrs, author, community)
8373
end)
8474
|> Multi.run(:set_community, fn _, %{create_content: content} ->
8575
ArticleOperation.set_community(community, thread, content.id)
8676
end)
8777
|> Multi.run(:set_topic, fn _, %{create_content: content} ->
88-
topic_title =
89-
case attrs |> Map.has_key?(:topic) do
90-
true -> attrs.topic
91-
false -> "posts"
92-
end
93-
94-
ArticleOperation.set_topic(%Topic{title: topic_title}, thread, content.id)
78+
exec_set_topic(thread, content.id, attrs)
9579
end)
9680
|> Multi.run(:set_community_flag, fn _, %{create_content: content} ->
97-
# TODO: remove this judge, as content should have a flag
98-
case action |> Map.has_key?(:flag) do
99-
true ->
100-
ArticleOperation.set_community_flags(content, community.id, %{
101-
trash: false
102-
})
103-
104-
false ->
105-
{:ok, :pass}
106-
end
81+
exec_set_community_flag(community, content, action)
10782
end)
10883
|> Multi.run(:set_tag, fn _, %{create_content: content} ->
109-
case attrs |> Map.has_key?(:tags) do
110-
true -> set_tags(thread, content.id, attrs.tags)
111-
false -> {:ok, :pass}
112-
end
84+
exec_set_tag(thread, content.id, attrs)
11385
end)
11486
|> Multi.run(:mention_users, fn _, %{create_content: content} ->
115-
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: user_id})
87+
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: uid})
11688
{:ok, :pass}
11789
end)
11890
|> Multi.run(:log_action, fn _, _ ->
119-
Statistics.log_publish_action(%User{id: user_id})
91+
Statistics.log_publish_action(%User{id: uid})
12092
end)
12193
|> Repo.transaction()
12294
|> create_content_result()
@@ -132,7 +104,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
132104
ORM.update(content, args)
133105
end)
134106
|> Multi.run(:update_tag, fn _, _ ->
135-
update_tags(content, args.tags)
107+
exec_update_tags(content, args.tags)
136108
end)
137109
|> Repo.transaction()
138110
|> update_content_result()
@@ -368,37 +340,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
368340

369341
defp should_add_pin?(_filter), do: {:error, :pass}
370342

343+
defp concat_contents(%{total_count: 0}, normal_contents), do: {:ok, normal_contents}
344+
371345
defp concat_contents(pined_content, normal_contents) do
372-
case pined_content |> Map.get(:total_count) do
373-
0 ->
374-
{:ok, normal_contents}
375-
376-
_ ->
377-
pind_entries =
378-
pined_content
379-
|> Map.get(:entries)
380-
|> Enum.map(&struct(&1, %{pin: true}))
381-
382-
normal_entries = normal_contents |> Map.get(:entries)
383-
384-
# pind_count = pined_content |> Map.get(:total_count)
385-
normal_count = normal_contents |> Map.get(:total_count)
386-
387-
# remote the pined content from normal_entries (if have)
388-
pind_ids = pick_by(pind_entries, :id)
389-
normal_entries = Enum.reject(normal_entries, &(&1.id in pind_ids))
390-
391-
normal_contents
392-
|> Map.put(:entries, pind_entries ++ normal_entries)
393-
# those two are equals
394-
# |> Map.put(:total_count, pind_count + normal_count - pind_count)
395-
|> Map.put(:total_count, normal_count)
396-
|> done
397-
end
346+
pind_entries =
347+
pined_content
348+
|> Map.get(:entries)
349+
|> Enum.map(&struct(&1, %{pin: true}))
350+
351+
normal_entries = normal_contents |> Map.get(:entries)
352+
353+
# pind_count = pined_content |> Map.get(:total_count)
354+
normal_count = normal_contents |> Map.get(:total_count)
355+
356+
# remote the pined content from normal_entries (if have)
357+
pind_ids = pick_by(pind_entries, :id)
358+
normal_entries = Enum.reject(normal_entries, &(&1.id in pind_ids))
359+
360+
normal_contents
361+
|> Map.put(:entries, pind_entries ++ normal_entries)
362+
# those two are equals
363+
# |> Map.put(:total_count, pind_count + normal_count - pind_count)
364+
|> Map.put(:total_count, normal_count)
365+
|> done
398366
end
399367

400368
defp create_content_result({:ok, %{create_content: result}}) do
401-
Later.exec({__MODULE__, :nofify_admin_new_content, [result]})
369+
Later.exec({__MODULE__, :notify_admin_new_content, [result]})
402370
{:ok, result}
403371
end
404372

@@ -430,10 +398,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
430398
{:error, [message: "log action", code: ecode(:create_fails)]}
431399
end
432400

433-
defp set_tags(thread, content_id, tags) do
401+
# except Job, other content will just pass, should use set_tag function instead
402+
# defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}
403+
404+
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
405+
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
406+
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
407+
408+
defp content_id(:post, id), do: %{post_id: id}
409+
defp content_id(:job, id), do: %{job_id: id}
410+
defp content_id(:repo, id), do: %{repo_id: id}
411+
defp content_id(:video, id), do: %{video_id: id}
412+
413+
# for create content step in Multi.new
414+
defp exec_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
415+
target
416+
|> struct()
417+
|> target.changeset(attrs)
418+
|> Ecto.Changeset.put_change(:author_id, aid)
419+
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
420+
|> Repo.insert()
421+
end
422+
423+
defp exec_set_topic(thread, id, %{topic: topic}) do
424+
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
425+
end
426+
427+
# if topic is not provide, use posts as default
428+
defp exec_set_topic(thread, id, _attrs) do
429+
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
430+
end
431+
432+
defp exec_set_tag(thread, id, %{tags: tags}) do
434433
try do
435434
Enum.each(tags, fn tag ->
436-
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, content_id)
435+
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, id)
437436
end)
438437

439438
{:ok, "psss"}
@@ -442,62 +441,44 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
442441
end
443442
end
444443

445-
defp update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
444+
defp exec_set_tag(_thread, _id, _attrs), do: {:ok, :pass}
446445

447-
# Job is special, the tags in job only represent city, so everytime update
448-
# tags on job content, should be override the old ones, in this way, every
449-
# communiies contains this job will have the same city info
450-
defp update_tags(%CMS.Job{} = content, tags_ids) do
451-
with {:ok, content} <- ORM.find(CMS.Job, content.id, preload: :tags) do
452-
concat_tags(content, tags_ids)
453-
end
446+
# TODO: flag 逻辑似乎有问题
447+
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
448+
ArticleOperation.set_community_flags(community, content, %{
449+
trash: false
450+
})
454451
end
455452

456-
defp update_tags(%CMS.Post{} = content, tags_ids) do
457-
with {:ok, content} <- ORM.find(CMS.Post, content.id, preload: :tags) do
458-
concat_tags(content, tags_ids)
459-
end
453+
defp exec_set_community_flag(_community, _content, _action) do
454+
{:ok, :pass}
460455
end
461456

462-
defp update_tags(%CMS.Video{} = content, tags_ids) do
463-
with {:ok, content} <- ORM.find(CMS.Video, content.id, preload: :tags) do
464-
concat_tags(content, tags_ids)
465-
end
466-
end
467-
468-
# except Job, other content will just pass, should use set_tag function instead
469-
defp update_tags(_, _tags_ids), do: {:ok, :pass}
457+
defp exec_update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
470458

471-
defp concat_tags(content, tags_ids) do
472-
tags =
473-
Enum.reduce(tags_ids, [], fn t, acc ->
474-
{:ok, tag} = ORM.find(Tag, t.id)
459+
defp exec_update_tags(content, tags_ids) do
460+
with {:ok, content} <- ORM.find(content.__struct__, content.id, preload: :tags) do
461+
tags =
462+
Enum.reduce(tags_ids, [], fn t, acc ->
463+
{:ok, tag} = ORM.find(Tag, t.id)
475464

476-
case tag.title == "refined" do
477-
true ->
478-
acc
465+
case tag.title == "refined" do
466+
true ->
467+
acc
479468

480-
false ->
481-
acc ++ [tag]
482-
end
483-
end)
469+
false ->
470+
acc ++ [tag]
471+
end
472+
end)
484473

485-
content
486-
|> Ecto.Changeset.change()
487-
|> Ecto.Changeset.put_assoc(:tags, tags)
488-
|> Repo.update()
474+
content
475+
|> Ecto.Changeset.change()
476+
|> Ecto.Changeset.put_assoc(:tags, tags)
477+
|> Repo.update()
478+
end
489479
end
490480

491-
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
492-
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
493-
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
494-
495-
defp content_id(:post, id), do: %{post_id: id}
496-
defp content_id(:job, id), do: %{job_id: id}
497-
defp content_id(:repo, id), do: %{repo_id: id}
498-
defp content_id(:video, id), do: %{video_id: id}
499-
500-
defp nofify_admin_new_content(%{id: id} = result) do
481+
defp notify_admin_new_content(%{id: id} = result) do
501482
target = result.__struct__
502483
preload = [:origial_community, author: :user]
503484

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
9797
@doc """
9898
trash / untrash articles
9999
"""
100-
def set_community_flags(content, community_id, attrs) do
100+
def set_community_flags(%Community{id: cid}, content, attrs) do
101+
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102+
{:ok, record} <- insert_flag_record(content, cid, attrs) do
103+
{:ok, struct(content, %{trash: record.trash})}
104+
end
105+
end
106+
107+
def set_community_flags(community_id, content, attrs) do
101108
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102109
{:ok, community} <- ORM.find(Community, community_id),
103110
{:ok, record} <- insert_flag_record(content, community.id, attrs) do

lib/groupher_server/cms/delegates/comment_curd.ex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
9292
ORM.delete(comment)
9393
end)
9494
|> Multi.run(:update_floor, fn _, _ ->
95-
Repo.update_all(
96-
from(p in action.reactor, where: p.id > ^comment.id),
97-
inc: [floor: -1]
98-
)
99-
|> done()
100-
|> case do
101-
{:ok, _} -> {:ok, comment}
102-
{:error, _} -> {:error, ""}
103-
end
95+
exec_update_floor(action.reactor, comment)
10496
end)
10597
|> Repo.transaction()
10698
|> delete_comment_result()
@@ -185,6 +177,18 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
185177
{:error, [message: "update follor fails", code: ecode(:delete_fails)]}
186178
end
187179

180+
defp exec_update_floor(queryable, comment) do
181+
Repo.update_all(
182+
from(p in queryable, where: p.id > ^comment.id),
183+
inc: [floor: -1]
184+
)
185+
|> done()
186+
|> case do
187+
{:ok, _} -> {:ok, comment}
188+
{:error, _} -> {:error, ""}
189+
end
190+
end
191+
188192
# simulate a join connection
189193
# TODO: use Multi task to refactor
190194
# TODO: refactor boilerplate code

lib/groupher_server/cms/delegates/community_curd.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
174174
{:ok, ret} ->
175175
{:ok, ret}
176176

177-
{:error, error} ->
178-
{:error, error}
177+
{:error, reason} ->
178+
{:error, reason}
179179
end
180180
end
181181

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
167167

168168
defp set_community_flags(community_id, thread, id, flag) do
169169
with {:ok, content} <- match_action(thread, :self) do
170-
content.target
171-
|> struct(%{id: id})
172-
|> CMS.set_community_flags(community_id, flag)
170+
queryable = content.target |> struct(%{id: id})
171+
172+
CMS.set_community_flags(community_id, queryable, flag)
173173
end
174174
end
175175

0 commit comments

Comments
 (0)