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

Commit 742969d

Browse files
committed
refactor(cited-article): wip
1 parent b4b9b0d commit 742969d

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

lib/groupher_server/cms/delegates/block_tasks.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
22
@moduledoc """
33
run tasks in every article blocks if need
44
"""
5+
import Ecto.Query, warn: false
56
import Helper.Utils, only: [get_config: 2]
67
import GroupherServer.CMS.Helper.Matcher
78

@@ -48,9 +49,27 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
4849

4950
IO.inspect(cited_contents, label: "fuck all")
5051

52+
CitedContent |> Repo.insert_all(cited_contents)
53+
update_citing_count(cited_contents)
54+
5155
# CitedContent |> ORM.create
5256
end
5357

58+
defp update_citing_count(cited_contents) do
59+
Enum.each(cited_contents, fn content ->
60+
count_query = from(c in CitedContent, where: c.cited_by_id == ^content.cited_by_id)
61+
count = Repo.aggregate(count_query, :count)
62+
63+
IO.inspect(count, label: "the count")
64+
end)
65+
66+
# count = Repo.aggregate(count_query, :count)
67+
# IO.inspect cited_contents, label: "update_citing_count"
68+
end
69+
70+
# count_query = from(s in CommunitySubscriber, where: s.user_id == ^user.id)
71+
# count = Repo.aggregate(count_query, :count)
72+
5473
@doc """
5574
e.g:
5675
[
@@ -128,6 +147,7 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
128147
%{
129148
cited_by_id: cited_article.id,
130149
cited_by_type: cited_article.meta.thread,
150+
# cited_article: cited_article,
131151
block_linker: [block_id],
132152
user_id: article.author.user.id
133153
}

lib/groupher_server/cms/helper/macros.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ defmodule GroupherServer.CMS.Helper.Macros do
207207
article_comment_fields()
208208

209209
field(:active_at, :utc_datetime_usec)
210+
field(:citing_count, :integer, default: 0)
210211
# TODO:
211-
# reference_articles
212212
# related_articles
213213
timestamps()
214214
end

lib/groupher_server/cms/models/cited_content.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ defmodule GroupherServer.CMS.Model.CitedContent do
1111
alias GroupherServer.{Accounts, CMS}
1212
alias Accounts.Model.User
1313

14-
alias CMS.Model.{ArticleComment, Embeds}
14+
alias CMS.Model.ArticleComment
1515

1616
@timestamps_opts [type: :utc_datetime_usec]
1717

1818
@required_fields ~w(cited_by_type cited_by_id user_id)a
1919
@article_cast_fields general_article_fields(:cast)
20-
@optional_fields ~w(article_comment_id block_linkers)a ++ @article_cast_fields
20+
@optional_fields ~w(article_comment_id block_linker)a ++ @article_cast_fields
2121

2222
@type t :: %CitedContent{}
2323
schema "cited_contents" do
@@ -29,7 +29,7 @@ defmodule GroupherServer.CMS.Model.CitedContent do
2929

3030
article_belongs_to_fields()
3131

32-
field(:block_linkers, {:array, :string})
32+
field(:block_linker, {:array, :string})
3333
# content.block_linker = ["block-eee_block-bbb", "block-eee_block-bbb"]
3434
timestamps()
3535
end

lib/groupher_server/cms/models/post.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule GroupherServer.CMS.Model.Post do
3232

3333
# TODO: move to general_article_fields
3434
# embeds_one(:block_task_runner, Embeds.BlockTaskRunner, on_replace: :update)
35+
# embeds_many(:citing_contents, CMS.CitedContent, on_replace: :delete)
3536

3637
article_tags_field(:post)
3738
article_communities_field(:post)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule GroupherServer.Repo.Migrations.AddCitingCountToArticles do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter(table(:cms_posts), do: add(:citing_count, :integer))
6+
alter(table(:cms_jobs), do: add(:citing_count, :integer))
7+
alter(table(:cms_repos), do: add(:citing_count, :integer))
8+
alter(table(:cms_blogs), do: add(:citing_count, :integer))
9+
end
10+
end

0 commit comments

Comments
 (0)