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

Commit 1ba431b

Browse files
authored
refactor(community): article_tags_count in field (#368)
1 parent 2fb73d6 commit 1ba431b

File tree

8 files changed

+80
-18
lines changed

8 files changed

+80
-18
lines changed

lib/groupher_server/cms/community.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ defmodule GroupherServer.CMS.Community do
4848
field(:articles_count, :integer, default: 0)
4949
field(:editors_count, :integer, default: 0)
5050
field(:subscribers_count, :integer, default: 0)
51+
field(:article_tags_count, :integer, default: 0)
52+
field(:threads_count, :integer, default: 0)
5153

5254
field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
5355
field(:viewer_is_editor, :boolean, default: false, virtual: true)

lib/groupher_server/cms/delegates/article_tag.ex

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,30 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
1515
alias GroupherServer.{Accounts, Repo}
1616

1717
alias Accounts.User
18-
alias GroupherServer.CMS.{Community, ArticleTag}
18+
alias GroupherServer.CMS.{ArticleTag, Community, Delegate}
19+
20+
alias Delegate.CommunityCURD
21+
22+
alias Ecto.Multi
1923

2024
@doc """
2125
create a article tag
2226
"""
23-
def create_article_tag(%Community{id: community_id}, thread, attrs, %User{id: user_id}) do
27+
def create_article_tag(%Community{} = community, thread, attrs, %User{id: user_id}) do
2428
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
25-
{:ok, community} <- ORM.find(Community, community_id) do
26-
attrs =
27-
attrs
28-
|> Map.merge(%{author_id: author.id, community_id: community.id, thread: thread})
29-
|> map_atom_values_to_upcase_str
29+
{:ok, community} <- ORM.find(Community, community.id) do
30+
Multi.new()
31+
|> Multi.run(:create_article_tag, fn _, _ ->
32+
update_attrs = %{author_id: author.id, community_id: community.id, thread: thread}
33+
attrs = attrs |> Map.merge(update_attrs) |> map_atom_values_to_upcase_str
3034

31-
ArticleTag |> ORM.create(attrs)
35+
ORM.create(ArticleTag, attrs)
36+
end)
37+
|> Multi.run(:update_community_count, fn _, _ ->
38+
CommunityCURD.update_community_count_field(community, :article_tags_count)
39+
end)
40+
|> Repo.transaction()
41+
|> result()
3242
end
3343
end
3444

@@ -46,8 +56,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
4656
delete an article tag
4757
"""
4858
def delete_article_tag(id) do
49-
with {:ok, article_tag} <- ORM.find(ArticleTag, id) do
50-
ORM.delete(article_tag)
59+
with {:ok, article_tag} <- ORM.find(ArticleTag, id),
60+
{:ok, community} <- ORM.find(Community, article_tag.community_id) do
61+
Multi.new()
62+
|> Multi.run(:delete_article_tag, fn _, _ ->
63+
ORM.delete(article_tag)
64+
end)
65+
|> Multi.run(:update_community_count, fn _, _ ->
66+
CommunityCURD.update_community_count_field(community, :article_tags_count)
67+
end)
68+
|> Repo.transaction()
69+
|> result()
5170
end
5271
end
5372

@@ -142,4 +161,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
142161
|> ORM.paginater(%{page: 1, size: 100})
143162
|> done()
144163
end
164+
165+
defp result({:ok, %{create_article_tag: result}}), do: {:ok, result}
166+
defp result({:ok, %{delete_article_tag: result}}), do: {:ok, result}
167+
defp result({:error, _, result, _steps}), do: {:error, result}
145168
end

lib/groupher_server/cms/delegates/community_curd.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
7676
|> Repo.update()
7777
end
7878

79+
@doc """
80+
update article_tags_count of a community
81+
"""
82+
def update_community_count_field(%Community{} = community, :article_tags_count) do
83+
count_query = from(t in ArticleTag, where: t.community_id == ^community.id)
84+
article_tags_count = Repo.aggregate(count_query, :count)
85+
86+
community
87+
|> Ecto.Changeset.change(%{article_tags_count: article_tags_count})
88+
|> Repo.update()
89+
end
90+
7991
@doc """
8092
update subscribers_count of a community
8193
"""

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,13 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
266266
field(:articles_count, :integer)
267267
field(:subscribers_count, :integer)
268268
field(:editors_count, :integer)
269+
field(:article_tags_count, :integer)
269270

270271
# TODO: remove
271272
field :threads_count, :integer do
272273
resolve(&R.CMS.threads_count/3)
273274
end
274275

275-
# TODO: remove
276-
field :article_tags_count, :integer do
277-
resolve(&R.CMS.article_tags_count/3)
278-
end
279-
280276
field :contributes_digest, list_of(:integer) do
281277
# TODO add complex here to warning N+1 problem
282278
resolve(&R.Statistics.list_contributes_digest/3)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule GroupherServer.Repo.Migrations.AddArticleTagsCount do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:communities) do
6+
add(:article_tags_count, :integer, default: 0)
7+
add(:threads_count, :integer, default: 0)
8+
end
9+
end
10+
end

test/groupher_server/cms/community/community_test.exs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ defmodule GroupherServer.Test.CMS.Community do
1313
{:ok, user2} = db_insert(:user)
1414
{:ok, community} = db_insert(:community)
1515

16-
{:ok, ~m(user community user2)a}
16+
article_tag_attrs = mock_attrs(:article_tag)
17+
18+
{:ok, ~m(user community article_tag_attrs user2)a}
1719
end
1820

1921
describe "[cms community read]" do
@@ -56,6 +58,25 @@ defmodule GroupherServer.Test.CMS.Community do
5658
end
5759
end
5860

61+
describe "[cms community article_tag]" do
62+
@tag :wip2
63+
test "articleTagsCount should work", ~m(community article_tag_attrs user)a do
64+
{:ok, tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
65+
{:ok, tag2} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
66+
{:ok, tag3} = CMS.create_article_tag(community, :repo, article_tag_attrs, user)
67+
68+
{:ok, community} = ORM.find(Community, community.id)
69+
assert community.article_tags_count == 3
70+
71+
{:ok, _} = CMS.delete_article_tag(tag.id)
72+
{:ok, _} = CMS.delete_article_tag(tag2.id)
73+
{:ok, _} = CMS.delete_article_tag(tag3.id)
74+
75+
{:ok, community} = ORM.find(Community, community.id)
76+
assert community.article_tags_count == 0
77+
end
78+
end
79+
5980
describe "[cms community editor]" do
6081
test "can set editor to a community", ~m(user community)a do
6182
title = "chief editor"

test/groupher_server/cms/search_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ defmodule GroupherServer.Test.CMS.Search do
6868
assert searched.entries |> Enum.at(0) |> Map.get(:title) == "react"
6969
end
7070

71-
@tag :wip2
7271
test "search community blur title should return valid communities" do
7372
{:ok, searched} = CMS.search_communities("reac")
7473
assert searched.entries |> Enum.at(0) |> Map.get(:title) == "react"

test/groupher_server_web/query/cms/article_tags_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ defmodule GroupherServer.Test.Query.CMS.ArticleTags do
3838
}
3939
}
4040
"""
41-
4241
test "guest user can get paged tags without filter",
4342
~m(guest_conn community article_tag_attrs article_tag_attrs2 user)a do
4443
variables = %{}

0 commit comments

Comments
 (0)