@@ -3,7 +3,8 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
3
3
seeds data for init, should be called ONLY in new database, like migration
4
4
"""
5
5
6
- import Helper.Utils , only: [ done: 1 ]
6
+ import GroupherServer.Support.Factory
7
+ import Helper.Utils , only: [ done: 1 , get_config: 2 ]
7
8
import Ecto.Query , warn: false
8
9
9
10
import GroupherServer.CMS.Delegate.Seeds.Helper ,
@@ -17,18 +18,14 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
17
18
insert_community: 3
18
19
]
19
20
20
- @ oss_endpoint "https://cps-oss.oss-cn-shanghai.aliyuncs.com"
21
- # import Helper.Utils, only: [done: 1, map_atom_value: 2]
22
- # import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
23
- # import ShortMaps
24
-
25
21
alias Helper.ORM
26
22
alias GroupherServer.CMS
27
23
28
- alias CMS.Model . { Community , Category }
24
+ alias CMS.Model . { Community , Category , Post }
29
25
alias CMS.Delegate.Seeds
30
26
alias Seeds.Domain
31
27
28
+ @ article_threads get_config ( :article , :threads )
32
29
# categories
33
30
@ community_types [ :pl , :framework , :editor , :database , :devops , :city ]
34
31
@@ -76,4 +73,69 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
76
73
{ :ok , _ } = CMS . set_category ( % Community { id: community . id } , % Category { id: category . id } )
77
74
end )
78
75
end
76
+
77
+ def seed_articles ( % Community { } = community , thread , count \\ 3 )
78
+ when thread in @ article_threads do
79
+ #
80
+ thread_upcase = thread |> to_string |> String . upcase ( )
81
+ tags_filter = % { community_id: community . id , thread: thread_upcase }
82
+
83
+ with { :ok , community } <- ORM . find ( Community , community . id ) ,
84
+ { :ok , tags } <- CMS . paged_article_tags ( tags_filter ) ,
85
+ { :ok , user } <- db_insert ( :user ) do
86
+ 1 .. count
87
+ |> Enum . each ( fn _ ->
88
+ attrs = mock_attrs ( thread , % { community_id: community . id } )
89
+ { :ok , article } = CMS . create_article ( community , thread , attrs , user )
90
+ seed_tags ( tags , thread , article . id )
91
+ seed_upvotes ( thread , article . id )
92
+ end )
93
+ end
94
+ end
95
+
96
+ defp seed_upvotes ( thread , article_id ) do
97
+ with { :ok , users } <- db_insert_multi ( :user , Enum . random ( 1 .. 10 ) ) do
98
+ users
99
+ |> Enum . each ( fn user ->
100
+ { :ok , _article } = CMS . upvote_article ( thread , article_id , user )
101
+ end )
102
+ end
103
+ end
104
+
105
+ defp seed_tags ( tags , thread , article_id ) do
106
+ get_tag_ids ( tags , thread )
107
+ |> Enum . each ( fn tag_id ->
108
+ { :ok , _ } = CMS . set_article_tag ( thread , article_id , tag_id )
109
+ end )
110
+ end
111
+
112
+ defp get_tag_ids ( tags , :job ) do
113
+ tags . entries |> Enum . map ( & & 1 . id ) |> Enum . shuffle ( ) |> Enum . take ( 3 )
114
+ end
115
+
116
+ defp get_tag_ids ( tags , _ ) do
117
+ tags . entries |> Enum . map ( & & 1 . id ) |> Enum . shuffle ( ) |> Enum . take ( 1 )
118
+ end
119
+
120
+ # clean up
121
+
122
+ def clean_up ( :all ) do
123
+ #
124
+ end
125
+
126
+ def clean_up_community ( raw ) do
127
+ with { :ok , community } <- ORM . findby_delete ( Community , % { raw: to_string ( raw ) } ) do
128
+ clean_up_articles ( community , :post )
129
+ end
130
+ end
131
+
132
+ def clean_up_articles ( % Community { } = community , :post ) do
133
+ Post
134
+ |> join ( :inner , [ p ] , c in assoc ( p , :original_community ) )
135
+ |> where ( [ p , c ] , c . id == ^ community . id )
136
+ |> ORM . delete_all ( :if_exist )
137
+ |> done
138
+ end
139
+
140
+ def clean_up_articles ( _ , _ ) , do: { :ok , :pass }
79
141
end