Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 2b0295a

Browse files
committed
feat(seeds): home community & makefile cmd
1 parent 6ed6bb6 commit 2b0295a

File tree

5 files changed

+129
-15
lines changed

5 files changed

+129
-15
lines changed

Makefile

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ deploy.prod:
114114
@echo "deploy to docker done!"
115115
@echo "todo: restart docker container"
116116

117+
reset:
118+
$(call reset.help)
119+
@echo "\n"
120+
reset.help:
121+
$(call reset.help)
122+
@echo "\n"
123+
reset.test:
124+
env MIX_ENV=test mix ecto.drop
125+
env MIX_ENV=test mix ecto.create
126+
reset.mock:
127+
env MIX_ENV=mock mix ecto.reset
128+
reset.prod:
129+
env MIX_ENV=prod mix ecto.reset
130+
131+
seeds:
132+
$(call reset.help)
133+
@echo "\n"
134+
seeds.help:
135+
$(call reset.help)
136+
@echo "\n"
137+
seeds.mock:
138+
@echo "------------------------------"
139+
@echo "seeds the mock database"
140+
env MIX_ENV=mock mix cps.seeds
141+
142+
seeds.prod:
143+
@echo "------------------------------"
144+
@echo "seeds the prod database"
145+
env MIX_ENV=prod mix cps.seeds
146+
117147
test.help:
118148
$(call test.help)
119149
@echo "\n"
@@ -127,9 +157,6 @@ test.watch.wip2:
127157
mix test.watch --only wip2
128158
test.watch.bug:
129159
mix test.watch --only bug
130-
test.db_reset:
131-
env MIX_ENV=test mix ecto.drop
132-
env MIX_ENV=test mix ecto.create
133160
test.report:
134161
MIX_ENV=mix test.coverage
135162
$(call browse,"./cover/excoveralls.html")

Makefile.include.mk

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ define console.help
120120
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
121121
endef
122122

123+
define reset.help
124+
@echo "\n"
125+
@echo " [WARNING: reset db commands]"
126+
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
127+
@echo " reset.test : reset test db"
128+
@echo " ..................................."
129+
@echo " reset.mock : reset mock db"
130+
@echo " ..................................."
131+
@echo " reset.prod : reset prod db"
132+
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
133+
endef
134+
135+
define seeds.help
136+
@echo "\n"
137+
@echo " [seeds db commands]"
138+
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
139+
@echo " seeds.mock : insert default communities etc .. to mock db"
140+
@echo " .........................................................."
141+
@echo " seeds.prod : insert default communities etc .. to prod db"
142+
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
143+
endef
144+
123145
define test.help
124146
@echo "\n"
125147
@echo " [valid test commands]"

lib/mastani_server/cms/delegates/seeds.ex

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ->

priv/mock/cps_seeds.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
alias MastaniServer.CMS
22

3-
# seed communities with default threads and categories
3+
# NOTE: the order is important
44
CMS.seed_communities(:pl)
5+
CMS.seed_communities(:home)

test/mastani_server/seeds/communities_test.exs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ defmodule MastaniServer.Test.Seeds.Communities do
1919

2020
# {:ok, results} = ORM.find_all(CMS.Thread, %{page: 1, size: 20})
2121
{:ok, results} = ORM.find_all(CMS.Community, %{page: 1, size: 20})
22-
# IO.inspect results.entries |> Enum.random(), label: "threads results"
2322
radom_community = results.entries |> Enum.random()
24-
# IO.inspect radom_community, label: "hello radom_community"
2523

2624
{:ok, found} = ORM.find(CMS.Community, radom_community.id, preload: :threads)
27-
assert length(found.threads) !== 0
25+
assert length(found.threads) == 7
2826

2927
{:ok, found} = ORM.find(CMS.Community, radom_community.id, preload: :categories)
3028
assert length(found.categories) !== 0
3129
end
30+
31+
@tag :wip
32+
test "home community seeds works" do
33+
CMS.seed_communities(:home)
34+
35+
# {:ok, results} = ORM.find_all(CMS.Thread, %{page: 1, size: 20})
36+
{:ok, community} = ORM.find_by(CMS.Community, %{raw: "home"})
37+
assert community.title == "coderplanets"
38+
assert community.raw == "home"
39+
40+
{:ok, found} = ORM.find(CMS.Community, community.id, preload: :threads)
41+
assert length(found.threads) == 6
42+
end
3243
end
3344
end

0 commit comments

Comments
 (0)