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

Commit aa54b76

Browse files
committed
refactor(article-curd): update tags
1 parent 46e52c5 commit aa54b76

File tree

1 file changed

+66
-85
lines changed

1 file changed

+66
-85
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 66 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -95,50 +95,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
9595
end
9696
end
9797

98-
# for create content step in Multi.new
99-
defp exec_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
100-
target
101-
|> struct()
102-
|> target.changeset(attrs)
103-
|> Ecto.Changeset.put_change(:author_id, aid)
104-
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
105-
|> Repo.insert()
106-
end
107-
108-
defp exec_set_topic(thread, id, %{topic: topic}) do
109-
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
110-
end
111-
112-
# if topic is not provide, use posts as default
113-
defp exec_set_topic(thread, id, _attrs) do
114-
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
115-
end
116-
117-
defp exec_set_tag(thread, id, %{tags: tags}) do
118-
try do
119-
Enum.each(tags, fn tag ->
120-
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, id)
121-
end)
122-
123-
{:ok, "psss"}
124-
rescue
125-
_ -> {:error, [message: "set tag", code: ecode(:create_fails)]}
126-
end
127-
end
128-
129-
defp exec_set_tag(_thread, _id, _attrs), do: {:ok, :pass}
130-
131-
# TODO: flag 逻辑似乎有问题
132-
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
133-
ArticleOperation.set_community_flags(community, content, %{
134-
trash: false
135-
})
136-
end
137-
138-
defp exec_set_community_flag(_community, _content, _action) do
139-
{:ok, :pass}
140-
end
141-
14298
@doc """
14399
update a content(post/job ...)
144100
"""
@@ -148,7 +104,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
148104
ORM.update(content, args)
149105
end)
150106
|> Multi.run(:update_tag, fn _, _ ->
151-
update_tags(content, args.tags)
107+
exec_update_tags(content, args.tags)
152108
end)
153109
|> Repo.transaction()
154110
|> update_content_result()
@@ -446,60 +402,85 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
446402
{:error, [message: "log action", code: ecode(:create_fails)]}
447403
end
448404

449-
defp update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
405+
# except Job, other content will just pass, should use set_tag function instead
406+
# defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}
450407

451-
# Job is special, the tags in job only represent city, so everytime update
452-
# tags on job content, should be override the old ones, in this way, every
453-
# communiies contains this job will have the same city info
454-
defp update_tags(%CMS.Job{} = content, tags_ids) do
455-
with {:ok, content} <- ORM.find(CMS.Job, content.id, preload: :tags) do
456-
concat_tags(content, tags_ids)
457-
end
408+
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
409+
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
410+
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
411+
412+
defp content_id(:post, id), do: %{post_id: id}
413+
defp content_id(:job, id), do: %{job_id: id}
414+
defp content_id(:repo, id), do: %{repo_id: id}
415+
defp content_id(:video, id), do: %{video_id: id}
416+
417+
# for create content step in Multi.new
418+
defp exec_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
419+
target
420+
|> struct()
421+
|> target.changeset(attrs)
422+
|> Ecto.Changeset.put_change(:author_id, aid)
423+
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
424+
|> Repo.insert()
458425
end
459426

460-
defp update_tags(%CMS.Post{} = content, tags_ids) do
461-
with {:ok, content} <- ORM.find(CMS.Post, content.id, preload: :tags) do
462-
concat_tags(content, tags_ids)
463-
end
427+
defp exec_set_topic(thread, id, %{topic: topic}) do
428+
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
464429
end
465430

466-
defp update_tags(%CMS.Video{} = content, tags_ids) do
467-
with {:ok, content} <- ORM.find(CMS.Video, content.id, preload: :tags) do
468-
concat_tags(content, tags_ids)
469-
end
431+
# if topic is not provide, use posts as default
432+
defp exec_set_topic(thread, id, _attrs) do
433+
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
470434
end
471435

472-
# except Job, other content will just pass, should use set_tag function instead
473-
defp update_tags(_, _tags_ids), do: {:ok, :pass}
436+
defp exec_set_tag(thread, id, %{tags: tags}) do
437+
try do
438+
Enum.each(tags, fn tag ->
439+
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, id)
440+
end)
474441

475-
defp concat_tags(content, tags_ids) do
476-
tags =
477-
Enum.reduce(tags_ids, [], fn t, acc ->
478-
{:ok, tag} = ORM.find(Tag, t.id)
442+
{:ok, "psss"}
443+
rescue
444+
_ -> {:error, [message: "set tag", code: ecode(:create_fails)]}
445+
end
446+
end
479447

480-
case tag.title == "refined" do
481-
true ->
482-
acc
448+
defp exec_set_tag(_thread, _id, _attrs), do: {:ok, :pass}
483449

484-
false ->
485-
acc ++ [tag]
486-
end
487-
end)
450+
# TODO: flag 逻辑似乎有问题
451+
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
452+
ArticleOperation.set_community_flags(community, content, %{
453+
trash: false
454+
})
455+
end
488456

489-
content
490-
|> Ecto.Changeset.change()
491-
|> Ecto.Changeset.put_assoc(:tags, tags)
492-
|> Repo.update()
457+
defp exec_set_community_flag(_community, _content, _action) do
458+
{:ok, :pass}
493459
end
494460

495-
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
496-
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
497-
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
461+
defp exec_update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
498462

499-
defp content_id(:post, id), do: %{post_id: id}
500-
defp content_id(:job, id), do: %{job_id: id}
501-
defp content_id(:repo, id), do: %{repo_id: id}
502-
defp content_id(:video, id), do: %{video_id: id}
463+
defp exec_update_tags(content, tags_ids) do
464+
with {:ok, content} <- ORM.find(content.__struct__, content.id, preload: :tags) do
465+
tags =
466+
Enum.reduce(tags_ids, [], fn t, acc ->
467+
{:ok, tag} = ORM.find(Tag, t.id)
468+
469+
case tag.title == "refined" do
470+
true ->
471+
acc
472+
473+
false ->
474+
acc ++ [tag]
475+
end
476+
end)
477+
478+
content
479+
|> Ecto.Changeset.change()
480+
|> Ecto.Changeset.put_assoc(:tags, tags)
481+
|> Repo.update()
482+
end
483+
end
503484

504485
defp nofify_admin_new_content(%{id: id} = result) do
505486
target = result.__struct__

0 commit comments

Comments
 (0)