@@ -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 }
145168end
0 commit comments