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

Commit 36b1d2a

Browse files
committed
refactor(pinned-article): fix pined test & re-org
1 parent 67cf040 commit 36b1d2a

File tree

25 files changed

+82
-282
lines changed

25 files changed

+82
-282
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ defmodule GroupherServer.CMS do
8888
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
8989
defdelegate pin_article(thread, id, community_id), to: ArticleOperation
9090
defdelegate undo_pin_article(thread, id, community_id), to: ArticleOperation
91-
defdelegate pin_content(queryable, community_id), to: ArticleOperation
92-
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation
9391

9492
defdelegate lock_article_comment(content), to: ArticleOperation
9593

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
323323
pind_entries =
324324
pined_content
325325
|> Map.get(:entries)
326-
|> Enum.map(&struct(&1, %{pin: true}))
326+
|> Enum.map(&struct(&1, %{is_pinned: true}))
327327

328328
normal_entries = normal_contents |> Map.get(:entries)
329329

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2121
JobCommunityFlag,
2222
RepoCommunityFlag,
2323
Tag,
24-
PinnedArticle,
25-
PinedPost,
26-
PinedJob,
27-
PinedRepo
24+
PinnedArticle
2825
}
2926

3027
alias GroupherServer.CMS.Repo, as: CMSRepo
@@ -63,54 +60,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
6360
)
6461

6562
ORM.findby_delete(PinnedArticle, args)
66-
end
67-
end
68-
69-
def pin_content(%Post{id: post_id}, %Community{id: community_id}) do
70-
with {:ok, pined} <-
71-
ORM.findby_or_insert(
72-
PinedPost,
73-
~m(post_id community_id)a,
74-
~m(post_id community_id)a
75-
) do
76-
Post |> ORM.find(pined.post_id)
77-
end
78-
end
79-
80-
def pin_content(%Job{id: job_id}, %Community{id: community_id}) do
81-
attrs = ~m(job_id community_id)a
82-
83-
with {:ok, pined} <- ORM.findby_or_insert(PinedJob, attrs, attrs) do
84-
Job |> ORM.find(pined.job_id)
85-
end
86-
end
87-
88-
def pin_content(%CMSRepo{id: repo_id}, %Community{id: community_id}) do
89-
attrs = ~m(repo_id community_id)a
90-
91-
with {:ok, pined} <- ORM.findby_or_insert(PinedRepo, attrs, attrs) do
92-
CMSRepo |> ORM.find(pined.repo_id)
93-
end
94-
end
95-
96-
def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}) do
97-
with {:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id)a),
98-
{:ok, deleted} <- ORM.delete(pined) do
99-
Post |> ORM.find(deleted.post_id)
100-
end
101-
end
102-
103-
def undo_pin_content(%Job{id: job_id}, %Community{id: community_id}) do
104-
with {:ok, pined} <- ORM.find_by(PinedJob, ~m(job_id community_id)a),
105-
{:ok, deleted} <- ORM.delete(pined) do
106-
Job |> ORM.find(deleted.job_id)
107-
end
108-
end
109-
110-
def undo_pin_content(%CMSRepo{id: repo_id}, %Community{id: community_id}) do
111-
with {:ok, pined} <- ORM.find_by(PinedRepo, ~m(repo_id community_id)a),
112-
{:ok, deleted} <- ORM.delete(pined) do
113-
CMSRepo |> ORM.find(deleted.repo_id)
63+
ORM.find(info.model, article_id)
11464
end
11565
end
11666

lib/groupher_server/cms/job.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule GroupherServer.CMS.Job do
5656
has_many(:community_flags, {"jobs_communities_flags", JobCommunityFlag})
5757

5858
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
59-
field(:pin, :boolean, default_value: false, virtual: true)
59+
field(:is_pinned, :boolean, default: false, virtual: true)
6060
field(:trash, :boolean, default_value: false, virtual: true)
6161

6262
has_many(:article_comments, {"articles_comments", ArticleComment})

lib/groupher_server/cms/pined_job.ex

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/groupher_server/cms/pined_post.ex

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/groupher_server/cms/pined_repo.ex

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/groupher_server/cms/pinned_article.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ defmodule GroupherServer.CMS.PinnedArticle do
66
import Ecto.Changeset
77

88
alias GroupherServer.CMS
9-
alias CMS.{Community, Post, Job}
9+
alias CMS.{Community, Post, Job, Repo}
1010

1111
@required_fields ~w(community_id thread)a
12-
@optional_fields ~w(post_id job_id)a
12+
@optional_fields ~w(post_id job_id repo_id)a
1313

1414
@type t :: %PinnedArticle{}
1515
schema "pinned_articles" do
1616
belongs_to(:post, Post, foreign_key: :post_id)
1717
belongs_to(:job, Job, foreign_key: :job_id)
18+
belongs_to(:repo, Repo, foreign_key: :repo_id)
1819
belongs_to(:community, Community, foreign_key: :community_id)
1920

2021
field(:thread, :string)
@@ -29,8 +30,11 @@ defmodule GroupherServer.CMS.PinnedArticle do
2930
|> validate_required(@required_fields)
3031
|> foreign_key_constraint(:post_id)
3132
|> foreign_key_constraint(:job_id)
33+
|> foreign_key_constraint(:repo_id)
3234
|> foreign_key_constraint(:community_id)
35+
|> unique_constraint(:pinned_articles, name: :pinned_articles_post_id_community_id_index)
3336

34-
# |> unique_constraint(:pined_posts, name: :pined_posts_post_id_community_id_index)
37+
# |> unique_constraint(:pinned_articles, name: :pinned_articles_job_id_community_id_index)
38+
# |> unique_constraint(:pinned_articles, name: :pinned_articles_repo_id_community_id_index)
3539
end
3640
end

lib/groupher_server/cms/post.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ defmodule GroupherServer.CMS.Post do
4545
has_many(:community_flags, {"posts_communities_flags", PostCommunityFlag})
4646

4747
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
48-
field(:pin, :boolean, default_value: false, virtual: true)
48+
# field(:pin, :boolean, default_value: false, virtual: true)
49+
field(:is_pinned, :boolean, default: false, virtual: true)
4950
field(:trash, :boolean, default_value: false, virtual: true)
5051

5152
belongs_to(:author, Author)

lib/groupher_server/cms/repo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule GroupherServer.CMS.Repo do
5656
has_many(:community_flags, {"repos_communities_flags", RepoCommunityFlag})
5757

5858
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
59-
field(:pin, :boolean, default_value: false)
59+
field(:is_pinned, :boolean, default: false, virtual: true)
6060
field(:trash, :boolean, default_value: false)
6161

6262
field(:last_sync, :utc_datetime)

0 commit comments

Comments
 (0)