@@ -5,35 +5,73 @@ 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
8+ import ShortMaps
99
1010 alias Helper.ORM
1111 alias MastaniServer.Accounts.User
12- alias MastaniServer.CMS . { Community , PinState , Post , PostCommunityFlags , Tag }
12+
13+ alias MastaniServer.CMS . {
14+ Community ,
15+ PinState ,
16+ Post ,
17+ PostCommunityFlag ,
18+ Job ,
19+ JobCommunityFlag ,
20+ RepoCommunityFlag ,
21+ Video ,
22+ VideoCommunityFlag ,
23+ Tag
24+ }
25+
26+ alias MastaniServer.CMS.Repo , as: CMSRepo
1327 alias MastaniServer.Repo
1428
1529 @ doc """
1630 pin / unpin, trash / untrash articles
1731 """
18- def set_community_flags ( % Post { id: post_id } , community_id , attrs ) do
19- with { :ok , post } <- ORM . find ( Post , post_id ) ,
32+ def set_community_flags ( % Post { id: _ } = content , community_id , attrs ) ,
33+ do: do_set_flag ( content , community_id , attrs )
34+
35+ def set_community_flags ( % Job { id: _ } = content , community_id , attrs ) ,
36+ do: do_set_flag ( content , community_id , attrs )
37+
38+ def set_community_flags ( % CMSRepo { id: _ } = content , community_id , attrs ) ,
39+ do: do_set_flag ( content , community_id , attrs )
40+
41+ def set_community_flags ( % Video { id: _ } = content , community_id , attrs ) ,
42+ do: do_set_flag ( content , community_id , attrs )
43+
44+ defp do_set_flag ( content , community_id , attrs ) do
45+ with { :ok , content } <- ORM . find ( content . __struct__ , content . id ) ,
2046 { :ok , community } <- ORM . find ( Community , community_id ) ,
21- { :ok , _ } <- insert_flag_record ( post , community_id , attrs ) do
22- ORM . find ( Post , post . id )
47+ { :ok , record } <- insert_flag_record ( content , community_id , attrs ) do
48+ { :ok , struct ( content , % { pin: record . pin , trash: record . trash } ) }
2349 end
2450 end
2551
26- defp insert_flag_record ( % Post { id: id } , community_id , attrs ) do
27- clauses = % {
28- post_id: id ,
29- community_id: community_id
30- }
52+ defp insert_flag_record ( % Post { id: post_id } , community_id , attrs ) do
53+ clauses = ~m( post_id community_id) a
54+ PostCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
55+ end
56+
57+ defp insert_flag_record ( % Job { id: job_id } , community_id , attrs ) do
58+ clauses = ~m( job_id community_id) a
59+ JobCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
60+ end
3161
32- attrs = attrs |> Map . merge ( clauses )
62+ defp insert_flag_record ( % CMSRepo { id: repo_id } , community_id , attrs ) do
63+ clauses = ~m( repo_id community_id) a
64+ RepoCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
65+ end
3366
34- PostCommunityFlags |> ORM . upsert_by ( clauses , attrs )
67+ defp insert_flag_record ( % Video { id: video_id } , community_id , attrs ) do
68+ clauses = ~m( video_id community_id) a
69+ VideoCommunityFlag |> ORM . upsert_by ( clauses , Map . merge ( attrs , clauses ) )
3570 end
3671
72+ @ doc """
73+ set content to diffent community
74+ """
3775 def set_community ( % Community { id: community_id } , thread , content_id ) when valid_thread ( thread ) do
3876 with { :ok , action } <- match_action ( thread , :community ) ,
3977 { :ok , content } <- ORM . find ( action . target , content_id , preload: :communities ) ,
0 commit comments