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

Commit da89cec

Browse files
committed
refactor(pin-article): basic workflow
1 parent e1bb1b8 commit da89cec

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ defmodule GroupherServer.CMS do
8686
# ArticleOperation
8787
# >> set flag on article, like: pin / unpin article
8888
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
89+
defdelegate pin_article(thread, id, community_id), to: ArticleOperation
8990
defdelegate pin_content(queryable, community_id), to: ArticleOperation
9091
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation
9192

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
66
import Ecto.Query, warn: false
77
# import Helper.ErrorCode
88
import ShortMaps
9+
import GroupherServer.CMS.Utils.Matcher2
910

11+
alias Helper.Types, as: T
1012
alias Helper.ORM
1113

1214
alias GroupherServer.CMS.{
@@ -18,6 +20,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
1820
JobCommunityFlag,
1921
RepoCommunityFlag,
2022
Tag,
23+
PinedArticle,
2124
PinedPost,
2225
PinedJob,
2326
PinedRepo
@@ -26,6 +29,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2629
alias GroupherServer.CMS.Repo, as: CMSRepo
2730
alias GroupherServer.Repo
2831

32+
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinedArticle.t()}
33+
def pin_article(thread, article_id, community_id) do
34+
with {:ok, info} <- match(thread),
35+
{:ok, community} <- ORM.find(Community, community_id) do
36+
args = Map.put(%{community_id: community.id}, info.foreign_key, article_id)
37+
PinedArticle |> ORM.create(args)
38+
end
39+
end
40+
2941
def pin_content(%Post{id: post_id}, %Community{id: community_id}) do
3042
with {:ok, pined} <-
3143
ORM.findby_or_insert(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule GroupherServer.CMS.PinedArticle do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias GroupherServer.CMS
9+
alias CMS.{Community, Post, Job}
10+
11+
@required_fields ~w(community_id)a
12+
@optional_fields ~w(post_id job_id)a
13+
14+
@type t :: %PinedArticle{}
15+
schema "pined_articles" do
16+
belongs_to(:post, Post, foreign_key: :post_id)
17+
belongs_to(:job, Job, foreign_key: :job_id)
18+
belongs_to(:community, Community, foreign_key: :community_id)
19+
20+
timestamps(type: :utc_datetime)
21+
end
22+
23+
@doc false
24+
def changeset(%PinedArticle{} = pined_article, attrs) do
25+
pined_article
26+
|> cast(attrs, @optional_fields ++ @required_fields)
27+
|> validate_required(@required_fields)
28+
|> foreign_key_constraint(:post_id)
29+
|> foreign_key_constraint(:job_id)
30+
|> foreign_key_constraint(:community_id)
31+
32+
# |> unique_constraint(:pined_posts, name: :pined_posts_post_id_community_id_index)
33+
end
34+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule GroupherServer.Repo.Migrations.CreatePinedArticle do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:pined_articles) do
6+
add(:post_id, references(:cms_posts, on_delete: :delete_all))
7+
add(:job_id, references(:cms_jobs, on_delete: :delete_all))
8+
add(:community_id, references(:communities, on_delete: :delete_all), null: false)
9+
10+
timestamps()
11+
end
12+
13+
create(index(:pined_articles, [:post_id]))
14+
create(index(:pined_articles, [:job_id]))
15+
create(index(:pined_articles, [:community_id]))
16+
create(unique_index(:pined_articles, [:post_id, :job_id, :community_id]))
17+
end
18+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
defmodule GroupherServer.Test.CMS.ArticlePin do
2+
use GroupherServer.TestTools
3+
4+
alias Helper.ORM
5+
alias GroupherServer.CMS
6+
7+
alias CMS.{
8+
PinedArticle,
9+
Post,
10+
Job
11+
}
12+
13+
setup do
14+
{:ok, user} = db_insert(:user)
15+
{:ok, community} = db_insert(:community)
16+
17+
{:ok, post} = CMS.create_content(community, :post, mock_attrs(:post), user)
18+
{:ok, job} = CMS.create_content(community, :job, mock_attrs(:job), user)
19+
{:ok, repo} = CMS.create_content(community, :repo, mock_attrs(:repo), user)
20+
21+
{:ok, ~m(user community post job repo)a}
22+
end
23+
24+
describe "[cms post pin]" do
25+
@tag :wip2
26+
test "can pin a post", ~m(community post)a do
27+
{:ok, _} = CMS.pin_article(:post, post.id, community.id)
28+
{:ok, pind_article} = ORM.find_by(PinedArticle, %{post_id: post.id})
29+
30+
assert pind_article.post_id == post.id
31+
end
32+
33+
@tag :wip2
34+
test "can not pin a non-exsit post", ~m(community post)a do
35+
assert {:error, _} = CMS.pin_article(:post, 8848, community.id)
36+
end
37+
38+
# test "can undo pin to a post", ~m(community post)a do
39+
# {:ok, pined_post} = CMS.pin_content(post, community)
40+
# assert pined_post.id == post.id
41+
42+
# assert {:ok, unpined} = CMS.undo_pin_content(post, community)
43+
# assert unpined.id == post.id
44+
# end
45+
end
46+
47+
# describe "[cms job pin]" do
48+
# test "can pin a job", ~m(community job)a do
49+
# {:ok, pined_job} = CMS.pin_content(job, community)
50+
51+
# assert pined_job.id == job.id
52+
# end
53+
54+
# test "can undo pin to a job", ~m(community job)a do
55+
# {:ok, pined_job} = CMS.pin_content(job, community)
56+
# assert pined_job.id == job.id
57+
58+
# assert {:ok, unpined} = CMS.undo_pin_content(job, community)
59+
# assert unpined.id == job.id
60+
# end
61+
# end
62+
63+
# describe "[cms repo pin]" do
64+
# test "can pin a repo", ~m(community repo)a do
65+
# {:ok, pined_repo} = CMS.pin_content(repo, community)
66+
67+
# assert pined_repo.id == repo.id
68+
# end
69+
70+
# test "can undo pin to a repo", ~m(community repo)a do
71+
# {:ok, pined_repo} = CMS.pin_content(repo, community)
72+
# assert pined_repo.id == repo.id
73+
74+
# assert {:ok, unpined} = CMS.undo_pin_content(repo, community)
75+
# assert unpined.id == repo.id
76+
# end
77+
# end
78+
end

0 commit comments

Comments
 (0)