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(community): article_tags_count in field#368

Merged
mydearxym merged 1 commit intodevfromupdate_community_count_when_tags_curd
May 24, 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/community.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,6 +48,8 @@ defmodule GroupherServer.CMS.Community do
field(:articles_count, :integer, default: 0)
field(:editors_count, :integer, default: 0)
field(:subscribers_count, :integer, default: 0)
field(:article_tags_count, :integer, default: 0)
field(:threads_count, :integer, default: 0)

field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
field(:viewer_is_editor, :boolean, default: false, virtual: true)
Expand Down
43 changes: 33 additions & 10 deletionslib/groupher_server/cms/delegates/article_tag.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,20 +15,30 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
alias GroupherServer.{Accounts, Repo}

alias Accounts.User
alias GroupherServer.CMS.{Community, ArticleTag}
alias GroupherServer.CMS.{ArticleTag, Community, Delegate}

alias Delegate.CommunityCURD

alias Ecto.Multi

@doc """
create a article tag
"""
def create_article_tag(%Community{id: community_id}, thread, attrs, %User{id: user_id}) do
def create_article_tag(%Community{} = community, thread, attrs, %User{id: user_id}) do
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
{:ok, community} <- ORM.find(Community,community_id) do
attrs =
attrs
|> Map.merge(%{author_id: author.id, community_id: community.id, thread: thread})
|> map_atom_values_to_upcase_str
{:ok, community} <- ORM.find(Community,community.id) do
Multi.new()
|> Multi.run(:create_article_tag, fn _, _ ->
update_attrs =%{author_id: author.id, community_id: community.id, thread: thread}
attrs = attrs |> Map.merge(update_attrs)|> map_atom_values_to_upcase_str

ArticleTag |> ORM.create(attrs)
ORM.create(ArticleTag, attrs)
end)
|> Multi.run(:update_community_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, :article_tags_count)
end)
|> Repo.transaction()
|> result()
end
end

Expand All@@ -46,8 +56,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
delete an article tag
"""
def delete_article_tag(id) do
with {:ok, article_tag} <- ORM.find(ArticleTag, id) do
ORM.delete(article_tag)
with {:ok, article_tag} <- ORM.find(ArticleTag, id),
{:ok, community} <- ORM.find(Community, article_tag.community_id) do
Multi.new()
|> Multi.run(:delete_article_tag, fn _, _ ->
ORM.delete(article_tag)
end)
|> Multi.run(:update_community_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, :article_tags_count)
end)
|> Repo.transaction()
|> result()
end
end

Expand DownExpand Up@@ -142,4 +161,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
|> ORM.paginater(%{page: 1, size: 100})
|> done()
end

defp result({:ok, %{create_article_tag: result}}), do: {:ok, result}
defp result({:ok, %{delete_article_tag: result}}), do: {:ok, result}
defp result({:error, _, result, _steps}), do: {:error, result}
end
12 changes: 12 additions & 0 deletionslib/groupher_server/cms/delegates/community_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
|> Repo.update()
end

@doc """
update article_tags_count of a community
"""
def update_community_count_field(%Community{} = community, :article_tags_count) do
count_query = from(t in ArticleTag, where: t.community_id == ^community.id)
article_tags_count = Repo.aggregate(count_query, :count)

community
|> Ecto.Changeset.change(%{article_tags_count: article_tags_count})
|> Repo.update()
end

@doc """
update subscribers_count of a community
"""
Expand Down
6 changes: 1 addition & 5 deletionslib/groupher_server_web/schema/cms/cms_types.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -266,17 +266,13 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:articles_count, :integer)
field(:subscribers_count, :integer)
field(:editors_count, :integer)
field(:article_tags_count, :integer)

# TODO: remove
field :threads_count, :integer do
resolve(&R.CMS.threads_count/3)
end

# TODO: remove
field :article_tags_count, :integer do
resolve(&R.CMS.article_tags_count/3)
end

field :contributes_digest, list_of(:integer) do
# TODO add complex here to warning N+1 problem
resolve(&R.Statistics.list_contributes_digest/3)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
defmodule GroupherServer.Repo.Migrations.AddArticleTagsCount do
use Ecto.Migration

def change do
alter table(:communities) do
add(:article_tags_count, :integer, default: 0)
add(:threads_count, :integer, default: 0)
end
end
end
23 changes: 22 additions & 1 deletiontest/groupher_server/cms/community/community_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ defmodule GroupherServer.Test.CMS.Community do
{:ok, user2} = db_insert(:user)
{:ok, community} = db_insert(:community)

{:ok, ~m(user community user2)a}
article_tag_attrs = mock_attrs(:article_tag)

{:ok, ~m(user community article_tag_attrs user2)a}
end

describe "[cms community read]" do
Expand DownExpand Up@@ -56,6 +58,25 @@ defmodule GroupherServer.Test.CMS.Community do
end
end

describe "[cms community article_tag]" do
@tag :wip2
test "articleTagsCount should work", ~m(community article_tag_attrs user)a do
{:ok, tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
{:ok, tag2} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
{:ok, tag3} = CMS.create_article_tag(community, :repo, article_tag_attrs, user)

{:ok, community} = ORM.find(Community, community.id)
assert community.article_tags_count == 3

{:ok, _} = CMS.delete_article_tag(tag.id)
{:ok, _} = CMS.delete_article_tag(tag2.id)
{:ok, _} = CMS.delete_article_tag(tag3.id)

{:ok, community} = ORM.find(Community, community.id)
assert community.article_tags_count == 0
end
end

describe "[cms community editor]" do
test "can set editor to a community", ~m(user community)a do
title = "chief editor"
Expand Down
1 change: 0 additions & 1 deletiontest/groupher_server/cms/search_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -68,7 +68,6 @@ defmodule GroupherServer.Test.CMS.Search do
assert searched.entries |> Enum.at(0) |> Map.get(:title) == "react"
end

@tag :wip2
test "search community blur title should return valid communities" do
{:ok, searched} = CMS.search_communities("reac")
assert searched.entries |> Enum.at(0) |> Map.get(:title) == "react"
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,6 @@ defmodule GroupherServer.Test.Query.CMS.ArticleTags do
}
}
"""

test "guest user can get paged tags without filter",
~m(guest_conn community article_tag_attrs article_tag_attrs2 user)a do
variables = %{}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp