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

Commit 81e4436

Browse files
committed
refactor: wip
1 parent 2f79153 commit 81e4436

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
88

99
import Helper.ErrorCode
1010
import ShortMaps
11-
import Helper.Utils, only: [strip_struct: 1, integerfy: 1]
11+
import Helper.Utils, only: [strip_struct: 1, integerfy: 1, done: 1]
1212
import GroupherServer.CMS.Helper.Matcher2
1313

1414
alias Helper.Types, as: T
@@ -29,6 +29,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2929
alias GroupherServer.CMS.Repo, as: CMSRepo
3030
alias GroupherServer.Repo
3131

32+
alias Ecto.Multi
33+
3234
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()
3335

3436
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
@@ -103,7 +105,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
103105
end
104106

105107
@doc """
106-
set content to diffent community
108+
mirror article to other community
107109
"""
108110
def set_community(thread, article_id, community_id) do
109111
with {:ok, info} <- match(thread),
@@ -116,6 +118,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
116118
end
117119
end
118120

121+
@doc """
122+
unmirror article to a community
123+
"""
119124
def unset_community(thread, article_id, community_id) do
120125
with {:ok, info} <- match(thread),
121126
{:ok, article} <-
@@ -135,18 +140,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
135140
end
136141

137142
@doc """
138-
set article original community in meta
143+
move article original community to other community
139144
"""
140145
def move_article(thread, article_id, community_id) do
141146
with {:ok, info} <- match(thread),
142-
{:ok, article} <- ORM.find(info.model, article_id) do
143-
article
144-
|> Ecto.Changeset.change()
145-
|> Ecto.Changeset.put_change(:original_community_id, integerfy(community_id))
146-
|> Repo.update()
147+
{:ok, community} <- ORM.find(Community, community_id),
148+
{:ok, article} <- ORM.find(info.model, article_id, preload: [:communities]) do
149+
Multi.new()
150+
|> Multi.run(:change_original_community, fn _, _ ->
151+
article
152+
|> Ecto.Changeset.change()
153+
|> Ecto.Changeset.put_change(:original_community_id, community.id)
154+
|> Repo.update()
155+
end)
156+
|> Multi.run(:unmirror_community, fn _, %{change_original_community: article} ->
157+
article
158+
|> Ecto.Changeset.change()
159+
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
160+
|> Repo.update()
161+
end)
162+
|> Repo.transaction()
163+
|> result()
147164
end
148165
end
149166

167+
defp result({:ok, %{change_original_community: result}}), do: result |> done()
168+
defp result({:error, _, result, _steps}), do: {:error, result}
169+
150170
@doc """
151171
set general tag for post / tuts ...
152172
"""

test/groupher_server/cms/article_community/job_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Job do
2525
assert job.original_community_id == community.id
2626
end
2727

28+
@tag :wip2
2829
test "job can be move to other community", ~m(user community community2 job_attrs)a do
2930
{:ok, job} = CMS.create_article(community, :job, job_attrs, user)
3031
assert job.original_community_id == community.id
3132

3233
{:ok, _} = CMS.move_article(:job, job.id, community2.id)
33-
{:ok, job} = ORM.find(CMS.Job, job.id, preload: :original_community)
34+
{:ok, job} = ORM.find(CMS.Job, job.id, preload: [:original_community, :communities])
3435

3536
assert job.original_community.id == community2.id
37+
assert is_nil(Enum.find(job.communities, &(&1.id == community2.id)))
3638
end
3739

3840
test "job can be mirror to other community", ~m(user community community2 job_attrs)a do

test/groupher_server/cms/article_community/post_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Post do
2525
assert post.original_community_id == community.id
2626
end
2727

28+
@tag :wip2
2829
test "post can be move to other community", ~m(user community community2 post_attrs)a do
2930
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
3031
assert post.original_community_id == community.id
3132

3233
{:ok, _} = CMS.move_article(:post, post.id, community2.id)
33-
{:ok, post} = ORM.find(CMS.Post, post.id, preload: :original_community)
34+
{:ok, post} = ORM.find(CMS.Post, post.id, preload: [:original_community, :communities])
3435

3536
assert post.original_community.id == community2.id
37+
assert is_nil(Enum.find(post.communities, &(&1.id == community2.id)))
3638
end
3739

3840
test "post can be mirror to other community", ~m(user community community2 post_attrs)a do

test/groupher_server/cms/article_community/repo_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Repo do
2525
assert repo.original_community_id == community.id
2626
end
2727

28+
@tag :wip2
2829
test "repo can be move to other community", ~m(user community community2 repo_attrs)a do
2930
{:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user)
3031
assert repo.original_community_id == community.id
3132

3233
{:ok, _} = CMS.move_article(:repo, repo.id, community2.id)
33-
{:ok, repo} = ORM.find(CMS.Repo, repo.id, preload: :original_community)
34+
{:ok, repo} = ORM.find(CMS.Repo, repo.id, preload: [:original_community, :communities])
3435

3536
assert repo.original_community.id == community2.id
37+
assert is_nil(Enum.find(repo.communities, &(&1.id == community2.id)))
3638
end
3739

3840
test "repo can be mirror to other community", ~m(user community community2 repo_attrs)a do

0 commit comments

Comments
 (0)