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

Commit 76c283b

Browse files
committed
refactor(create_tag): update args to follow conventions
1 parent 165a6ba commit 76c283b

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

lib/mastani_server/cms/cms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule MastaniServer.CMS do
3434
# >> thread
3535
defdelegate create_thread(attrs), to: CommunityCURD
3636
# >> tag
37-
defdelegate create_tag(thread, attrs, user), to: CommunityCURD
37+
defdelegate create_tag(community, thread, attrs, user), to: CommunityCURD
3838
defdelegate update_tag(attrs), to: CommunityCURD
3939
defdelegate get_tags(community, thread), to: CommunityCURD
4040
defdelegate get_tags(community, thread, topic), to: CommunityCURD

lib/mastani_server/cms/delegates/community_curd.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ defmodule MastaniServer.CMS.Delegate.CommunityCURD do
5353
create a Tag base on type: post / tuts / videos ...
5454
"""
5555
# TODO: change to create_tag(community, thread, attrs, ....)
56-
def create_tag(thread, attrs, %Accounts.User{id: user_id}) do
56+
def create_tag(%Community{id: community_id}, thread, attrs, %Accounts.User{id: user_id}) do
5757
with {:ok, action} <- match_action(thread, :tag),
5858
{:ok, author} <- ensure_author_exists(%Accounts.User{id: user_id}),
59-
{:ok, _community} <- ORM.find(Community, attrs.community_id),
59+
{:ok, _community} <- ORM.find(Community, community_id),
6060
{:ok, topic} = find_or_insert_topic(attrs) do
6161
attrs =
6262
attrs
63-
|> Map.merge(%{author_id: author.id, topic_id: topic.id})
63+
|> Map.merge(%{author_id: author.id, topic_id: topic.id, community_id: community_id})
6464
|> map_atom_value(:string)
6565
|> Map.merge(%{thread: attrs.thread |> to_string |> String.downcase()})
6666

lib/mastani_server/cms/delegates/seeds.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
2020
@default_threads ["post", "user", "job", "video", "wiki", "cheatsheet", "repo"]
2121
@home_threads ["post", "user", "news", "city", "share", "job"]
2222

23+
# those thread has tag list
24+
@general_threads ["post", "job", "repo", "video"]
25+
2326
@pl_communities ["javascript", "scala", "haskell", "swift", "typescript", "lua", "racket"]
2427
@default_categories ["pl", "front-end", "back-end", "ai", "design", "mobile", "others"]
2528

@@ -78,6 +81,9 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
7881
{:ok, categories} <- seed_categories(bot, :default),
7982
{:ok, communities} <- seed_for_communities(bot, :pl) do
8083
threadify_communities(communities.entries, threads)
84+
# tagfy_threads(communities.entries, threads)
85+
86+
# TODO: set tags for post, video, job, repo thread
8187
categorify_communities(communities.entries, categories)
8288
end
8389
end
@@ -145,6 +151,18 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
145151
end)
146152
end
147153

154+
# tagfy only post job repo and video
155+
defp tagfy_threads(communities, threads) when is_list(communities) do
156+
Enum.each(communities, fn community ->
157+
Enum.each(threads, fn thread ->
158+
case thread.raw in @general_threads do
159+
true -> IO.inspect(thread.raw, label: "set this thread")
160+
false -> IO.inspect(thread.raw, label: "not target")
161+
end
162+
end)
163+
end)
164+
end
165+
148166
# set categories to given communities
149167
defp categorify_communities(communities, categories) do
150168
Enum.each(communities, fn community ->

lib/mastani_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ defmodule MastaniServerWeb.Resolvers.CMS do
223223
# #######################
224224
# tags ..
225225
# #######################
226-
def create_tag(_root, args, %{context: %{cur_user: user}}) do
227-
CMS.create_tag(args.thread, args, user)
226+
def create_tag(_root, %{thread: thread, community_id: community_id} = args, %{
227+
context: %{cur_user: user}
228+
}) do
229+
CMS.create_tag(%Community{id: community_id}, thread, args, user)
228230
end
229231

230232
def delete_tag(_root, %{id: id}, _info), do: Tag |> ORM.find_delete(id)

test/mastani_server/cms/cms_test.exs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule MastaniServer.Test.CMS do
33

44
alias MastaniServer.Accounts.User
55
alias MastaniServer.CMS
6+
alias CMS.Community
67

78
alias Helper.{Certification, ORM}
89

@@ -16,22 +17,28 @@ defmodule MastaniServer.Test.CMS do
1617

1718
describe "[cms tag]" do
1819
test "create tag with valid data", ~m(community user)a do
19-
valid_attrs = mock_attrs(:tag, %{community_id: community.id})
20+
valid_attrs = mock_attrs(:tag)
2021

21-
{:ok, tag} = CMS.create_tag(:post, valid_attrs, %User{id: user.id})
22+
{:ok, tag} = CMS.create_tag(community, :post, valid_attrs, %User{id: user.id})
2223
assert tag.title == valid_attrs.title
2324
end
2425

2526
test "create tag with non-exsit user fails", ~m(user)a do
26-
invalid_attrs = mock_attrs(:tag, %{community_id: non_exsit_id()})
27+
invalid_attrs = mock_attrs(:tag)
2728

28-
assert {:error, _} = CMS.create_tag(:post, invalid_attrs, %User{id: user.id})
29+
assert {:error, _} =
30+
CMS.create_tag(%Community{id: non_exsit_id()}, :post, invalid_attrs, %User{
31+
id: user.id
32+
})
2933
end
3034

3135
test "create tag with non-exsit community fails", ~m(user)a do
32-
invalid_attrs = mock_attrs(:tag, %{community_id: non_exsit_id()})
36+
invalid_attrs = mock_attrs(:tag)
3337

34-
assert {:error, _} = CMS.create_tag(:post, invalid_attrs, %User{id: user.id})
38+
assert {:error, _} =
39+
CMS.create_tag(%Community{id: non_exsit_id()}, :post, invalid_attrs, %User{
40+
id: user.id
41+
})
3542
end
3643
end
3744

test/mastani_server_web/query/cms/cms_test.exs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ defmodule MastaniServer.Test.Query.CMS.Basic do
237237
test "guest user can get paged tags", ~m(guest_conn community user)a do
238238
variables = %{filter: %{page: 1, size: 10}}
239239

240-
valid_attrs = mock_attrs(:tag, %{user_id: user.id, community_id: community.id})
241-
{:ok, _} = CMS.create_tag(:post, valid_attrs, %User{id: user.id})
240+
valid_attrs = mock_attrs(:tag, %{user_id: user.id})
241+
242+
{:ok, _} =
243+
CMS.create_tag(%Community{id: community.id}, :post, valid_attrs, %User{id: user.id})
242244

243245
results = guest_conn |> query_result(@query, variables, "tags")
244246

@@ -273,8 +275,10 @@ defmodule MastaniServer.Test.Query.CMS.Basic do
273275
end
274276

275277
test "user can get partial tags by default index topic", ~m(guest_conn community user)a do
276-
valid_attrs = mock_attrs(:tag, %{community_id: community.id})
277-
{:ok, _tag} = CMS.create_tag(:post, valid_attrs, %User{id: user.id})
278+
valid_attrs = mock_attrs(:tag)
279+
280+
{:ok, _tag} =
281+
CMS.create_tag(%Community{id: community.id}, :post, valid_attrs, %User{id: user.id})
278282

279283
variables = %{thread: "POST", communityId: community.id, topic: "index"}
280284
results = guest_conn |> query_result(@query, variables, "partialTags")

0 commit comments

Comments
 (0)