Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor: works update workflow#437

Merged
mydearxym merged 9 commits intodevfromblog-update-workflow
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -108,6 +108,7 @@ defmodule GroupherServer.CMS do
defdelegate create_blog_rss(attrs), to: BlogCURD
defdelegate update_blog_rss(attrs), to: BlogCURD
defdelegate blog_rss_info(rss), to: BlogCURD
defdelegate update_rss_author(rss, attrs), to: BlogCURD

defdelegate create_works(attrs, user), to: WorksCURD
defdelegate update_works(attrs, user), to: WorksCURD
Expand DownExpand Up@@ -218,6 +219,7 @@ defmodule GroupherServer.CMS do
# search
defdelegate search_articles(thread, args), to: Search
defdelegate search_communities(args), to: Search
defdelegate search_communities(args, category), to: Search

# seeds
defdelegate seed_communities(opt), to: Seeds
Expand Down
36 changes: 26 additions & 10 deletionslib/groupher_server/cms/delegates/article_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -408,19 +408,35 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
# unique_constraint: avoid race conditions, make sure user_id unique
# foreign_key_constraint: check foreign key: user_id exsit or not
# see alos no_assoc_constraint in https://hexdocs.pm/ecto/Ecto.Changeset.html
%Author{user_id: user.id}
|> Ecto.Changeset.change()
|> Ecto.Changeset.unique_constraint(:user_id)
|> Ecto.Changeset.foreign_key_constraint(:user_id)
|> Repo.insert()
|> handle_existing_author()
case ORM.find_by(Author, user_id: user.id) do
{:ok, author} ->
{:ok, author}

{:error, _} ->
%Author{user_id: user.id}
|> Ecto.Changeset.change()
|> Ecto.Changeset.unique_constraint(:user_id)
|> Ecto.Changeset.foreign_key_constraint(:user_id)
|> Repo.insert()
end

# %Author{user_id: user.id}
# |> Ecto.Changeset.change()
# |> Ecto.Changeset.unique_constraint(:user_id)
# |> Ecto.Changeset.foreign_key_constraint(:user_id)
# |> Repo.insert()
# |> handle_existing_author()
end

defp handle_existing_author({:ok, author}), do: {:ok, author}
#defp handle_existing_author({:ok, author}), do: {:ok, author}

defp handle_existing_author({:error, changeset}) do
ORM.find_by(Author, user_id: changeset.data.user_id)
end
# defp handle_existing_author({:error, %Ecto.Changeset{changes: %{user_id: user_id}}}) do
# ORM.find_by(Author, user_id: user_id) |> IO.inspect(label: "after f2")
# end

# defp handle_existing_author({:error, changeset}) do
# ORM.find_by(Author, user_id: changeset.data.user_id)
# end

defp add_pin_articles_ifneed(articles, querable, %{community: community} = filter) do
thread = module_to_atom(querable)
Expand Down
10 changes: 9 additions & 1 deletionlib/groupher_server/cms/delegates/blog_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,8 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
with {:ok, feed} <- ORM.find_by(BlogRSS, %{rss: rss}) do
{:ok, feed}
else
_ -> fetch_fresh_rssinfo_and_cache(rss)
_ ->
fetch_fresh_rssinfo_and_cache(rss)
end
end

Expand All@@ -37,6 +38,12 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
end
end

def update_rss_author(rss, attrs) do
with {:ok, feed} <- ORM.find_by(BlogRSS, %{rss: rss}) do
ORM.update_embed(feed, :author, Map.drop(attrs, [:rss]))
end
end

# rss 记录存在, 直接创建 blog
defp do_create_blog(%Community{} = community, attrs, %User{} = user, %{id: _} = feed) do
blog_author = if is_nil(feed.author), do: nil, else: Map.from_struct(feed.author)
Expand DownExpand Up@@ -106,6 +113,7 @@ defmodule GroupherServer.CMS.Delegate.BlogCURD do
defp build_blog_attrs(attrs, blog_author, selected_feed) do
attrs
|> Map.merge(%{
rss: attrs.rss,
link_addr: selected_feed.link_addr,
published: selected_feed.published,
blog_author: blog_author,
Expand Down
15 changes: 14 additions & 1 deletionlib/groupher_server/cms/delegates/search.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,20 @@ defmodule GroupherServer.CMS.Delegate.Search do
search community by title
"""
def search_communities(title) do
Community
do_search_communities(Community, title)
end

def search_communities(title, category) do
from(
c in Community,
join: cat in assoc(c, :categories),
where: cat.raw == ^category
)
|> do_search_communities(title)
end

defp do_search_communities(queryable, title) do
queryable
|> where([c], ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%"))
|> ORM.paginator(page: 1, size: @search_items_count)
|> done()
Expand Down
41 changes: 25 additions & 16 deletionslib/groupher_server/cms/delegates/works_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,31 +36,33 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
attrs = attrs |> atom_values_to_upcase

Multi.new()
|> Multi.run(:update_works_fields, fn _, _ ->
update_works_fields(works, attrs)
end)
|> Multi.run(:update_works, fn _, %{update_works_fields: works} ->
|> Multi.run(:update_works, fn _, _ ->
update_article(works, attrs)
end)
|> Multi.run(:update_works_fields, fn _, %{update_works: works} ->
update_works_fields(works, attrs)
end)
|> Repo.transaction()
|> result()
end

# update works spec fields
defp update_works_fields(%Works{} = works, attrs) do
techstacks = Map.get(attrs, :techstacks, [])
cities = Map.get(attrs, :cities, [])
social_info = Map.get(attrs, :social_info, [])
app_store = Map.get(attrs, :app_store, [])
works = Repo.preload(works, [:techstacks, :cities])

desc = Map.get(attrs, :desc, works.desc)
home_link = Map.get(attrs, :home_link, works.home_link)
techstacks = Map.get(attrs, :techstacks, works.techstacks)
cities = Map.get(attrs, :cities, works.cities)
social_info = Map.get(attrs, :social_info, works.social_info)
app_store = Map.get(attrs, :app_store, works.app_store)

with {:ok, techstacks} <- get_or_create_techstacks(techstacks),
{:ok, cities} <- get_or_create_cities(cities) do
works = Repo.preload(works, [:techstacks, :cities])

works
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:techstacks,works.techstacks ++ techstacks)
|> Ecto.Changeset.put_assoc(:cities,works.cities ++ cities)
|> Ecto.Changeset.change(%{desc: desc, home_link: home_link})
|> Ecto.Changeset.put_assoc(:techstacks,uniq_by_raw(techstacks))
|> Ecto.Changeset.put_assoc(:cities,uniq_by_raw(cities))
|> Ecto.Changeset.put_embed(:social_info, social_info)
|> Ecto.Changeset.put_embed(:app_store, app_store)
|> Repo.update()
Expand All@@ -71,6 +73,7 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do

defp get_or_create_cities(cities) do
cities
|> Enum.uniq()
|> Enum.map(&String.downcase(&1))
|> Enum.reduce([], fn title, acc ->
with {:ok, city} <- get_city(title) do
Expand All@@ -95,7 +98,7 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
title: community.title,
logo: community.logo,
desc: community.desc,
link: "/#{community.raw}"
raw:community.raw
}

{:error, _} ->
Expand DownExpand Up@@ -131,19 +134,25 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
{:ok, community} ->
%{
title: community.title,
raw: community.raw,
logo: community.logo,
community_link: "/#{community.raw}",
desc: community.desc
}

{:error, _} ->
%{title: title}
%{title: title, raw: String.downcase(title)}
end

ORM.create(Techstack, attrs)
end

defp result({:ok, %{create_works: result}}), do: {:ok, result}
defp uniq_by_raw(list) do
Enum.uniq_by(list, & &1.raw)
end

# defp result({:ok, %{create_works: result}}), do: {:ok, result}
defp result({:ok, %{update_works_fields: result}}), do: {:ok, result}
defp result({:ok, %{update_works: result}}), do: {:ok, result}

defp result({:error, :create_works, _result, _steps}) do
Expand Down
1 change: 1 addition & 0 deletionslib/groupher_server/cms/helper/macros.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -116,6 +116,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
"""
def general_article_cast_fields() do
[
:title,
:digest,
:link_addr,
:original_community_id,
Expand Down
9 changes: 5 additions & 4 deletionslib/groupher_server/cms/models/author.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,7 @@ defmodule GroupherServer.CMS.Model.Author do
@type t :: %Author{}

schema "cms_authors" do
field(:role, :string)
#field(:role, :string)
# field(:user_id, :id)
# has_many(:posts, Post)
# user_id filed in own-table
Expand All@@ -23,11 +23,12 @@ defmodule GroupherServer.CMS.Model.Author do
end

@doc false
def changeset(%Author{} = author,attrs) do
def changeset(%Author{} = author,_attrs) do
# |> foreign_key_constraint(:user_id)
author
|> cast(attrs, [:role])
|> validate_required([:role])
#|> cast(attrs, [:role])
#|> validate_required([:role])
|> unique_constraint(:user_id)
|> foreign_key_constraint(:user_id)
end
end
3 changes: 2 additions & 1 deletionlib/groupher_server/cms/models/blog.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,12 +15,13 @@ defmodule GroupherServer.CMS.Model.Blog do

@required_fields ~w(title digest)a
@article_cast_fields general_article_cast_fields()
@optional_fields ~w(digest feed_digest feed_content published)a ++ @article_cast_fields
@optional_fields ~w(digest feed_digest feed_content published rss)a ++ @article_cast_fields

@type t :: %Blog{}
schema "cms_blogs" do
# for frontend constant
field(:copy_right, :string, default: "", virtual: true)
field(:rss, :string)

field(:feed_digest, :string)
field(:feed_content, :string)
Expand Down
4 changes: 2 additions & 2 deletionslib/groupher_server/cms/models/city.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,15 +12,15 @@ defmodule GroupherServer.CMS.Model.City do
@timestamps_opts [type: :utc_datetime_usec]

@required_fields ~w(title)a
@optional_fields ~w(logo desclink)a
@optional_fields ~w(logo descraw)a

@type t :: %City{}
schema "cms_cities" do
## mailstone
field(:title, :string)
field(:logo, :string)
field(:desc, :string)
field(:link, :string)
field(:raw, :string)

timestamps()
end
Expand Down
3 changes: 2 additions & 1 deletionlib/groupher_server/cms/models/techstack.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,13 +11,14 @@ defmodule GroupherServer.CMS.Model.Techstack do

@timestamps_opts [type: :utc_datetime_usec]

@required_fields ~w(title)a
@required_fields ~w(title raw)a
@optional_fields ~w(logo desc home_link community_link category)a

@type t :: %Techstack{}
schema "cms_techstacks" do
## mailstone
field(:title, :string)
field(:raw, :string)
field(:logo, :string)
field(:desc, :string)

Expand Down
3 changes: 2 additions & 1 deletionlib/groupher_server/cms/models/works.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,13 +15,14 @@ defmodule GroupherServer.CMS.Model.Works do

@required_fields ~w(title digest)a
@article_cast_fields general_article_cast_fields()
@optional_fields ~w(home_link profit_mode working_mode community_link interview_link)a ++
@optional_fields ~w(deschome_link profit_mode working_mode community_link interview_link)a ++
@article_cast_fields

@type t :: %Works{}
schema "cms_works" do
## mailstone
field(:home_link, :string)
field(:desc, :string)
# ...
field(:profit_mode, :string)
# fulltime / parttime
Expand Down
9 changes: 9 additions & 0 deletionslib/groupher_server_web/resolvers/cms_resolver.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,6 +67,11 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.blog_rss_info(rss)
end

def update_rss_author(_root, attrs, _) do
rss = Map.get(attrs, :rss)
CMS.update_rss_author(rss, attrs)
end

def wiki(_root, ~m(community)a, _info), do: CMS.get_wiki(%Community{raw: community})
def cheatsheet(_root, ~m(community)a, _info), do: CMS.get_cheatsheet(%Community{raw: community})

Expand DownExpand Up@@ -422,6 +427,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.add_contributor(%CommunityCheatsheet{id: id}, contributor)
end

def search_communities(_root, %{title: title, category: category}, _info) do
CMS.search_communities(title, category)
end

def search_communities(_root, %{title: title}, _info) do
CMS.search_communities(title)
end
Expand Down
6 changes: 2 additions & 4 deletionslib/groupher_server_web/schema/Helper/imports.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,8 +12,7 @@ defmodule GroupherServerWeb.Schema.Helper.Imports do
import article fields based on @article_threads
e.g:
----
import_types(:cms_post_mutations)
import_types(:cms_job_mutations)
import_types(:cms_[article]_mutations)
# ...
"""
defmacro import_article_fields(:mutations) do
Expand All@@ -29,8 +28,7 @@ defmodule GroupherServerWeb.Schema.Helper.Imports do
import article fields based on @article_threads
e.g:
----
import_types(CMS.Mutations.Post)
import_types(CMS.Mutations.Job)
import_types(CMS.Mutations.[Article])
# ...
"""
defmacro import_article_fields(:mutations, :module) do
Expand Down
10 changes: 6 additions & 4 deletionslib/groupher_server_web/schema/cms/cms_metrics.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -309,28 +309,30 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do

# works-spec
enum :profit_mode do
value(:free)
value(:ad)
value(:freemium)
value(:paid)
value(:free)
value(:product)
value(:others)
end

enum :working_mode do
value(:fulltime)
value(:parttime)
value(:side_project)
end

object :city do
field(:title, :string)
field(:logo, :string)
field(:desc, :string)
field(:link, :string)
field(:raw, :string)
end

object :techstack do
field(:title, :string)
field(:logo, :string)
field(:desc, :string)
field(:raw, :string)

field(:home_link, :string)
field(:community_link, :string)
Expand Down
2 changes: 2 additions & 0 deletionslib/groupher_server_web/schema/cms/cms_queries.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -91,6 +91,7 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
field :comments_state, :comments_list_state do
arg(:id, non_null(:id))
arg(:thread, :thread, default_value: :post)
arg(:freshkey, :string)

resolve(&R.CMS.comments_state/3)
end
Expand DownExpand Up@@ -151,6 +152,7 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
@desc "search communities by title"
field :search_communities, :paged_communities do
arg(:title, non_null(:string))
arg(:category, :string)

resolve(&R.CMS.search_communities/3)
end
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp