@@ -5,23 +5,72 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
55 import MastaniServer.CMS.Utils.Matcher
66 import Ecto.Query , warn: false
77 import Helper.ErrorCode
8+ import ShortMaps
89
910 alias Helper.ORM
1011 alias MastaniServer.Accounts.User
11- alias MastaniServer.CMS . { Community , Tag }
12+
13+ alias MastaniServer.CMS . {
14+ Community ,
15+ Post ,
16+ PostCommunityFlag ,
17+ Job ,
18+ JobCommunityFlag ,
19+ RepoCommunityFlag ,
20+ Video ,
21+ VideoCommunityFlag ,
22+ Tag
23+ }
24+
25+ alias MastaniServer.CMS.Repo , as: CMSRepo
1226 alias MastaniServer.Repo
1327
1428 @ doc """
1529 pin / unpin, trash / untrash articles
1630 """
17- def set_flag ( queryable , id , % { pin: _ } = attrs , % User { } = _user ) do
18- queryable |> ORM . find_update ( id , attrs )
31+ def set_community_flags ( % Post { id: _ } = content , community_id , attrs ) ,
32+ do: do_set_flag ( content , community_id , attrs )
33+
34+ def set_community_flags ( % Job { id: _ } = content , community_id , attrs ) ,
35+ do: do_set_flag ( content , community_id , attrs )
36+
37+ def set_community_flags ( % CMSRepo { id: _ } = content , community_id , attrs ) ,
38+ do: do_set_flag ( content , community_id , attrs )
39+
40+ def set_community_flags ( % Video { id: _ } = content , community_id , attrs ) ,
41+ do: do_set_flag ( content , community_id , attrs )
42+
43+ defp do_set_flag ( content , community_id , attrs ) do
44+ with { :ok , content } <- ORM . find ( content . __struct__ , content . id ) ,
45+ { :ok , community } <- ORM . find ( Community , community_id ) ,
46+ { :ok , record } <- insert_flag_record ( content , community_id , attrs ) do
47+ { :ok , struct ( content , % { pin: record . pin , trash: record . trash } ) }
48+ end
1949 end
2050
21- def set_flag ( queryable , id , % { trash: _ } = attrs , % User { } = _user ) do
22- queryable |> ORM . find_update ( id , attrs )
51+ defp insert_flag_record ( % Post { id: post_id } , community_id , attrs ) do
52+ clauses = ~m( post_id community_id) a
53+ PostCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
2354 end
2455
56+ defp insert_flag_record ( % Job { id: job_id } , community_id , attrs ) do
57+ clauses = ~m( job_id community_id) a
58+ JobCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
59+ end
60+
61+ defp insert_flag_record ( % CMSRepo { id: repo_id } , community_id , attrs ) do
62+ clauses = ~m( repo_id community_id) a
63+ RepoCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
64+ end
65+
66+ defp insert_flag_record ( % Video { id: video_id } , community_id , attrs ) do
67+ clauses = ~m( video_id community_id) a
68+ VideoCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
69+ end
70+
71+ @ doc """
72+ set content to diffent community
73+ """
2574 def set_community ( % Community { id: community_id } , thread , content_id ) when valid_thread ( thread ) do
2675 with { :ok , action } <- match_action ( thread , :community ) ,
2776 { :ok , content } <- ORM . find ( action . target , content_id , preload: :communities ) ,
@@ -49,7 +98,7 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
4998 set tag for post / tuts / videos ...
5099 """
51100 # check community first
52- def set_tag ( % Community { id: communitId } , thread , % Tag { id: tag_id } , content_id ) do
101+ def set_tag ( % Community { id: _communitId } , thread , % Tag { id: tag_id } , content_id ) do
53102 with { :ok , action } <- match_action ( thread , :tag ) ,
54103 { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
55104 { :ok , tag } <- ORM . find ( action . reactor , tag_id ) do
0 commit comments