@@ -3,7 +3,10 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
33 seeds data for init, should be called ONLY in new database, like migration
44 """
55
6- # import Ecto.Query, warn: false
6+ import Helper.Utils , only: [ done: 1 ]
7+ import Ecto.Query , warn: false
8+
9+ @ oss_endpoint "https://coderplanets.oss-cn-beijing.aliyuncs.com"
710 # import MastaniServer.CMS.Utils.Matcher
811 # import Helper.Utils, only: [done: 1, map_atom_value: 2]
912 # import MastaniServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
@@ -15,7 +18,7 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
1518 alias MastaniServer.CMS . { Community , Thread , Category }
1619
1720 @ default_threads [ "post" , "user" , "job" , "video" , "wiki" , "cheatsheet" , "repo" ]
18- @ home_addon_threads [ "city" , "share" , "news " ]
21+ @ home_threads [ "post" , "user" , "news" , " city", "share" , "job " ]
1922
2023 @ pl_communities [ "javascript" , "scala" , "haskell" , "swift" , "typescript" , "lua" , "racket" ]
2124 @ default_categories [ "pl" , "front-end" , "back-end" , "ai" , "design" , "mobile" , "others" ]
@@ -28,8 +31,30 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
2831 { :ok , _thread } = CMS . create_thread ( ~m( title raw) a )
2932 end )
3033
31- ORM . find_all ( CMS.Thread , % { page: 1 , size: 20 } )
34+ CMS.Thread
35+ |> where ( [ t ] , t . raw in @ default_threads )
36+ |> ORM . paginater ( page: 1 , size: 10 )
37+ |> done ( )
38+
39+ # ORM.find_all(CMS.Thread, %{page: 1, size: 20})
40+ end
41+ end
42+
43+ # NOTE: the home threads should be insert after default threads
44+ def seed_threads ( :home ) do
45+ case ORM . find_by ( CMS.Thread , % { raw: "post" } ) do
46+ { :ok , _ } -> { :ok , "ass" }
47+ { :error , _ } -> seed_threads ( :default )
3248 end
49+
50+ { :ok , _thread } = CMS . create_thread ( % { title: "news" , raw: "news" , index: 1 } )
51+ { :ok , _thread } = CMS . create_thread ( % { title: "share" , raw: "share" , index: 2 } )
52+ { :ok , _thread } = CMS . create_thread ( % { title: "city" , raw: "city" , index: 4 } )
53+
54+ CMS.Thread
55+ |> where ( [ t ] , t . raw in @ home_threads )
56+ |> ORM . paginater ( page: 1 , size: 10 )
57+ |> done ( )
3358 end
3459
3560 def seed_categories ( bot , :default ) do
@@ -44,14 +69,36 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
4469 end
4570 end
4671
72+ @ doc """
73+ seed communities pragraming languages
74+ """
4775 def seed_communities ( :pl ) do
4876 with { :ok , threads } <- seed_threads ( :default ) ,
4977 { :ok , bot } <- seed_bot ( ) ,
5078 { :ok , categories } <- seed_categories ( bot , :default ) ,
5179 { :ok , communities } <- seed_for_communities ( bot , :pl ) do
52- threadify_communities ( communities , threads )
53- categorify_communities ( communities , categories )
54- # {:ok, _} = CMS.set_category(%Community{id: community.id}, %Category{id: category.id})
80+ threadify_communities ( communities . entries , threads )
81+ categorify_communities ( communities . entries , categories )
82+ end
83+ end
84+
85+ @ doc """
86+ seed community for home
87+ """
88+ def seed_communities ( :home ) do
89+ with { :error , _ } <- ORM . find_by ( CMS.Community , % { raw: "home" } ) ,
90+ { :ok , threads } <- seed_threads ( :home ) ,
91+ { :ok , bot } <- seed_bot ( ) do
92+ args = % {
93+ title: "coderplanets" ,
94+ desc: "the most sexy community for developers, ever." ,
95+ logo: "#{ @ oss_endpoint } /icons/cmd/keyboard_logo.svg" ,
96+ raw: "home" ,
97+ user_id: bot . id
98+ }
99+
100+ { :ok , community } = Community |> ORM . create ( args )
101+ threadify_communities ( community , threads )
55102 end
56103 end
57104
@@ -71,7 +118,7 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
71118 args = % {
72119 title: c ,
73120 desc: "yes, #{ c } is awesome!" ,
74- logo: "https://coderplanets.oss-cn-beijing.aliyuncs.com /icons/pl/#{ c } .svg" ,
121+ logo: "#{ @ oss_endpoint } /icons/pl/#{ c } .svg" ,
75122 raw: c ,
76123 user_id: bot . id
77124 }
@@ -84,14 +131,20 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
84131 end
85132
86133 # set threads to given communities
87- defp threadify_communities ( communities , threads ) do
134+ defp threadify_communities ( communities , threads ) when is_list ( communities ) do
88135 Enum . each ( communities , fn community ->
89136 Enum . each ( threads , fn thread ->
90137 { :ok , _ } = CMS . set_thread ( % Community { id: community . id } , % Thread { id: thread . id } )
91138 end )
92139 end )
93140 end
94141
142+ defp threadify_communities ( community , threads ) do
143+ Enum . each ( threads , fn thread ->
144+ { :ok , _ } = CMS . set_thread ( % Community { id: community . id } , % Thread { id: thread . id } )
145+ end )
146+ end
147+
95148 # set categories to given communities
96149 defp categorify_communities ( communities , categories ) do
97150 Enum . each ( communities , fn community ->
0 commit comments