Thanks to visit codestin.com
Credit goes to github.com

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

Commit e700c36

Browse files
authored
refactor: works update workflow (#437)
* feat(comments): add freshkey to commentsState * refactor(blog-workflow): wip * refactor(works): add raw to techstack && clean up * feat(search): add category for community search * refactor(works-workflow): works update adjust * refactor(workflow): debug article worksflows * refactor(works-workflow): fix works required fields * refactor(workflow): fix tests * refactor(workflow): clean up
1 parent 3663e56 commit e700c36

37 files changed

+314
-64
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ defmodule GroupherServer.CMS do
108108
defdelegate create_blog_rss(attrs), to: BlogCURD
109109
defdelegate update_blog_rss(attrs), to: BlogCURD
110110
defdelegate blog_rss_info(rss), to: BlogCURD
111+
defdelegate update_rss_author(rss, attrs), to: BlogCURD
111112

112113
defdelegate create_works(attrs, user), to: WorksCURD
113114
defdelegate update_works(attrs, user), to: WorksCURD
@@ -218,6 +219,7 @@ defmodule GroupherServer.CMS do
218219
# search
219220
defdelegate search_articles(thread, args), to: Search
220221
defdelegate search_communities(args), to: Search
222+
defdelegate search_communities(args, category), to: Search
221223

222224
# seeds
223225
defdelegate seed_communities(opt), to: Seeds

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,35 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
408408
# unique_constraint: avoid race conditions, make sure user_id unique
409409
# foreign_key_constraint: check foreign key: user_id exsit or not
410410
# see alos no_assoc_constraint in https://hexdocs.pm/ecto/Ecto.Changeset.html
411-
%Author{user_id: user.id}
412-
|> Ecto.Changeset.change()
413-
|> Ecto.Changeset.unique_constraint(:user_id)
414-
|> Ecto.Changeset.foreign_key_constraint(:user_id)
415-
|> Repo.insert()
416-
|> handle_existing_author()
411+
case ORM.find_by(Author, user_id: user.id) do
412+
{:ok, author} ->
413+
{:ok, author}
414+
415+
{:error, _} ->
416+
%Author{user_id: user.id}
417+
|> Ecto.Changeset.change()
418+
|> Ecto.Changeset.unique_constraint(:user_id)
419+
|> Ecto.Changeset.foreign_key_constraint(:user_id)
420+
|> Repo.insert()
421+
end
422+
423+
# %Author{user_id: user.id}
424+
# |> Ecto.Changeset.change()
425+
# |> Ecto.Changeset.unique_constraint(:user_id)
426+
# |> Ecto.Changeset.foreign_key_constraint(:user_id)
427+
# |> Repo.insert()
428+
# |> handle_existing_author()
417429
end
418430

419-
defp handle_existing_author({:ok, author}), do: {:ok, author}
431+
# defp handle_existing_author({:ok, author}), do: {:ok, author}
420432

421-
defp handle_existing_author({:error, changeset}) do
422-
ORM.find_by(Author, user_id: changeset.data.user_id)
423-
end
433+
# defp handle_existing_author({:error, %Ecto.Changeset{changes: %{user_id: user_id}}}) do
434+
# ORM.find_by(Author, user_id: user_id) |> IO.inspect(label: "after f2")
435+
# end
436+
437+
# defp handle_existing_author({:error, changeset}) do
438+
# ORM.find_by(Author, user_id: changeset.data.user_id)
439+
# end
424440

425441
defp add_pin_articles_ifneed(articles, querable, %{community: community} = filter) do
426442
thread = module_to_atom(querable)

lib/groupher_server/cms/delegates/blog_curd.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
2222
with {:ok, feed} <- ORM.find_by(BlogRSS, %{rss: rss}) do
2323
{:ok, feed}
2424
else
25-
_ -> fetch_fresh_rssinfo_and_cache(rss)
25+
_ ->
26+
fetch_fresh_rssinfo_and_cache(rss)
2627
end
2728
end
2829

@@ -37,6 +38,12 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
3738
end
3839
end
3940

41+
def update_rss_author(rss, attrs) do
42+
with {:ok, feed} <- ORM.find_by(BlogRSS, %{rss: rss}) do
43+
ORM.update_embed(feed, :author, Map.drop(attrs, [:rss]))
44+
end
45+
end
46+
4047
# rss 记录存在, 直接创建 blog
4148
defp do_create_blog(%Community{} = community, attrs, %User{} = user, %{id: _} = feed) do
4249
blog_author = if is_nil(feed.author), do: nil, else: Map.from_struct(feed.author)
@@ -106,6 +113,7 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
106113
defp build_blog_attrs(attrs, blog_author, selected_feed) do
107114
attrs
108115
|> Map.merge(%{
116+
rss: attrs.rss,
109117
link_addr: selected_feed.link_addr,
110118
published: selected_feed.published,
111119
blog_author: blog_author,

lib/groupher_server/cms/delegates/search.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ defmodule GroupherServer.CMS.Delegate.Search do
1616
search community by title
1717
"""
1818
def search_communities(title) do
19-
Community
19+
do_search_communities(Community, title)
20+
end
21+
22+
def search_communities(title, category) do
23+
from(
24+
c in Community,
25+
join: cat in assoc(c, :categories),
26+
where: cat.raw == ^category
27+
)
28+
|> do_search_communities(title)
29+
end
30+
31+
defp do_search_communities(queryable, title) do
32+
queryable
2033
|> where([c], ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%"))
2134
|> ORM.paginator(page: 1, size: @search_items_count)
2235
|> done()

lib/groupher_server/cms/delegates/works_curd.ex

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,33 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
3636
attrs = attrs |> atom_values_to_upcase
3737

3838
Multi.new()
39-
|> Multi.run(:update_works_fields, fn _, _ ->
40-
update_works_fields(works, attrs)
41-
end)
42-
|> Multi.run(:update_works, fn _, %{update_works_fields: works} ->
39+
|> Multi.run(:update_works, fn _, _ ->
4340
update_article(works, attrs)
4441
end)
42+
|> Multi.run(:update_works_fields, fn _, %{update_works: works} ->
43+
update_works_fields(works, attrs)
44+
end)
4545
|> Repo.transaction()
4646
|> result()
4747
end
4848

4949
# update works spec fields
5050
defp update_works_fields(%Works{} = works, attrs) do
51-
techstacks = Map.get(attrs, :techstacks, [])
52-
cities = Map.get(attrs, :cities, [])
53-
social_info = Map.get(attrs, :social_info, [])
54-
app_store = Map.get(attrs, :app_store, [])
51+
works = Repo.preload(works, [:techstacks, :cities])
52+
53+
desc = Map.get(attrs, :desc, works.desc)
54+
home_link = Map.get(attrs, :home_link, works.home_link)
55+
techstacks = Map.get(attrs, :techstacks, works.techstacks)
56+
cities = Map.get(attrs, :cities, works.cities)
57+
social_info = Map.get(attrs, :social_info, works.social_info)
58+
app_store = Map.get(attrs, :app_store, works.app_store)
5559

5660
with {:ok, techstacks} <- get_or_create_techstacks(techstacks),
5761
{:ok, cities} <- get_or_create_cities(cities) do
58-
works = Repo.preload(works, [:techstacks, :cities])
59-
6062
works
61-
|> Ecto.Changeset.change()
62-
|> Ecto.Changeset.put_assoc(:techstacks, works.techstacks ++ techstacks)
63-
|> Ecto.Changeset.put_assoc(:cities, works.cities ++ cities)
63+
|> Ecto.Changeset.change(%{desc: desc, home_link: home_link})
64+
|> Ecto.Changeset.put_assoc(:techstacks, uniq_by_raw(techstacks))
65+
|> Ecto.Changeset.put_assoc(:cities, uniq_by_raw(cities))
6466
|> Ecto.Changeset.put_embed(:social_info, social_info)
6567
|> Ecto.Changeset.put_embed(:app_store, app_store)
6668
|> Repo.update()
@@ -71,6 +73,7 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
7173

7274
defp get_or_create_cities(cities) do
7375
cities
76+
|> Enum.uniq()
7477
|> Enum.map(&String.downcase(&1))
7578
|> Enum.reduce([], fn title, acc ->
7679
with {:ok, city} <- get_city(title) do
@@ -95,7 +98,7 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
9598
title: community.title,
9699
logo: community.logo,
97100
desc: community.desc,
98-
link: "/#{community.raw}"
101+
raw: community.raw
99102
}
100103

101104
{:error, _} ->
@@ -131,19 +134,25 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
131134
{:ok, community} ->
132135
%{
133136
title: community.title,
137+
raw: community.raw,
134138
logo: community.logo,
135139
community_link: "/#{community.raw}",
136140
desc: community.desc
137141
}
138142

139143
{:error, _} ->
140-
%{title: title}
144+
%{title: title, raw: String.downcase(title)}
141145
end
142146

143147
ORM.create(Techstack, attrs)
144148
end
145149

146-
defp result({:ok, %{create_works: result}}), do: {:ok, result}
150+
defp uniq_by_raw(list) do
151+
Enum.uniq_by(list, & &1.raw)
152+
end
153+
154+
# defp result({:ok, %{create_works: result}}), do: {:ok, result}
155+
defp result({:ok, %{update_works_fields: result}}), do: {:ok, result}
147156
defp result({:ok, %{update_works: result}}), do: {:ok, result}
148157

149158
defp result({:error, :create_works, _result, _steps}) do

lib/groupher_server/cms/helper/macros.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
116116
"""
117117
def general_article_cast_fields() do
118118
[
119+
:title,
119120
:digest,
120121
:link_addr,
121122
:original_community_id,

lib/groupher_server/cms/models/author.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule GroupherServer.CMS.Model.Author do
1313
@type t :: %Author{}
1414

1515
schema "cms_authors" do
16-
field(:role, :string)
16+
# field(:role, :string)
1717
# field(:user_id, :id)
1818
# has_many(:posts, Post)
1919
# user_id filed in own-table
@@ -23,11 +23,12 @@ defmodule GroupherServer.CMS.Model.Author do
2323
end
2424

2525
@doc false
26-
def changeset(%Author{} = author, attrs) do
26+
def changeset(%Author{} = author, _attrs) do
2727
# |> foreign_key_constraint(:user_id)
2828
author
29-
|> cast(attrs, [:role])
30-
|> validate_required([:role])
29+
# |> cast(attrs, [:role])
30+
# |> validate_required([:role])
3131
|> unique_constraint(:user_id)
32+
|> foreign_key_constraint(:user_id)
3233
end
3334
end

lib/groupher_server/cms/models/blog.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ defmodule GroupherServer.CMS.Model.Blog do
1515

1616
@required_fields ~w(title digest)a
1717
@article_cast_fields general_article_cast_fields()
18-
@optional_fields ~w(digest feed_digest feed_content published)a ++ @article_cast_fields
18+
@optional_fields ~w(digest feed_digest feed_content published rss)a ++ @article_cast_fields
1919

2020
@type t :: %Blog{}
2121
schema "cms_blogs" do
2222
# for frontend constant
2323
field(:copy_right, :string, default: "", virtual: true)
24+
field(:rss, :string)
2425

2526
field(:feed_digest, :string)
2627
field(:feed_content, :string)

lib/groupher_server/cms/models/city.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ defmodule GroupherServer.CMS.Model.City do
1212
@timestamps_opts [type: :utc_datetime_usec]
1313

1414
@required_fields ~w(title)a
15-
@optional_fields ~w(logo desc link)a
15+
@optional_fields ~w(logo desc raw)a
1616

1717
@type t :: %City{}
1818
schema "cms_cities" do
1919
## mailstone
2020
field(:title, :string)
2121
field(:logo, :string)
2222
field(:desc, :string)
23-
field(:link, :string)
23+
field(:raw, :string)
2424

2525
timestamps()
2626
end

lib/groupher_server/cms/models/techstack.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ defmodule GroupherServer.CMS.Model.Techstack do
1111

1212
@timestamps_opts [type: :utc_datetime_usec]
1313

14-
@required_fields ~w(title)a
14+
@required_fields ~w(title raw)a
1515
@optional_fields ~w(logo desc home_link community_link category)a
1616

1717
@type t :: %Techstack{}
1818
schema "cms_techstacks" do
1919
## mailstone
2020
field(:title, :string)
21+
field(:raw, :string)
2122
field(:logo, :string)
2223
field(:desc, :string)
2324

0 commit comments

Comments
 (0)