@@ -15,20 +15,30 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
15
15
alias GroupherServer . { Accounts , Repo }
16
16
17
17
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
19
23
20
24
@ doc """
21
25
create a article tag
22
26
"""
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
24
28
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
30
34
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 ( )
32
42
end
33
43
end
34
44
@@ -46,8 +56,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
46
56
delete an article tag
47
57
"""
48
58
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 ( )
51
70
end
52
71
end
53
72
@@ -142,4 +161,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
142
161
|> ORM . paginater ( % { page: 1 , size: 100 } )
143
162
|> done ( )
144
163
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 }
145
168
end