@@ -58,8 +58,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5858 update editors_count of a community
5959 """
6060 def update_community_count_field ( % Community { } = community , user_id , :editors_count , opt ) do
61- count_query = from ( s in CommunityEditor , where: s . community_id == ^ community . id )
62- editors_count = Repo . aggregate ( count_query , :count )
61+ { :ok , editors_count } =
62+ from ( s in CommunityEditor , where: s . community_id == ^ community . id )
63+ |> ORM . count ( )
64+
6365 community_meta = if is_nil ( community . meta ) , do: @ default_meta , else: community . meta
6466
6567 editors_ids =
@@ -80,8 +82,9 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
8082 update article_tags_count of a community
8183 """
8284 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+ { :ok , article_tags_count } =
86+ from ( t in ArticleTag , where: t . community_id == ^ community . id )
87+ |> ORM . count ( )
8588
8689 community
8790 |> Ecto.Changeset . change ( % { article_tags_count: article_tags_count } )
@@ -92,8 +95,9 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
9295 update subscribers_count of a community
9396 """
9497 def update_community_count_field ( % Community { } = community , user_id , :subscribers_count , opt ) do
95- count_query = from ( s in CommunitySubscriber , where: s . community_id == ^ community . id )
96- subscribers_count = Repo . aggregate ( count_query , :count )
98+ { :ok , subscribers_count } =
99+ from ( s in CommunitySubscriber , where: s . community_id == ^ community . id ) |> ORM . count ( )
100+
97101 community_meta = if is_nil ( community . meta ) , do: @ default_meta , else: community . meta
98102
99103 subscribed_user_ids =
@@ -122,22 +126,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
122126 """
123127 def update_community_count_field ( % Community { } = community , thread ) do
124128 with { :ok , info } <- match ( thread ) do
125- count_query =
129+ { :ok , thread_article_count } =
126130 from ( a in info . model ,
127131 join: c in assoc ( a , :communities ) ,
128- where: a . mark_delete == false ,
129- where: c . id == ^ community . id
132+ where: a . mark_delete == false and c . id == ^ community . id
130133 )
134+ |> ORM . count ( )
131135
132- thread_article_count = Repo . aggregate ( count_query , :count )
133136 community_meta = if is_nil ( community . meta ) , do: @ default_meta , else: community . meta
134-
135- meta = community_meta |> Map . put ( :"#{ thread } s_count" , thread_article_count ) |> strip_struct
137+ meta = Map . put ( community_meta , :"#{ thread } s_count" , thread_article_count )
136138
137139 community
138- |> Ecto.Changeset . change ( % { articles_count: recount_articles_count ( meta ) } )
139- |> Ecto.Changeset . put_embed ( :meta , meta )
140- |> Repo . update ( )
140+ |> ORM . update_meta ( meta , changes: % { articles_count: recount_articles_count ( meta ) } )
141141 end
142142 end
143143
0 commit comments