|
1 | 1 | defmodule MastaniServer.CMS.Delegate.ArticleCURD do |
| 2 | + @moduledoc """ |
| 3 | + CURD operation on post/job/video ... |
| 4 | + """ |
2 | 5 | import Ecto.Query, warn: false |
3 | 6 | import MastaniServer.CMS.Utils.Matcher |
4 | 7 | import Helper.Utils, only: [done: 1] |
| 8 | + import Helper.ErrorCode |
5 | 9 | import ShortMaps |
6 | 10 |
|
7 | 11 | alias MastaniServer.Accounts.User |
8 | | - alias MastaniServer.{Repo, CMS, Statistics} |
9 | | - alias MastaniServer.CMS.Delegate.ArticleOperation |
| 12 | + alias MastaniServer.{CMS, Repo, Statistics} |
| 13 | + |
| 14 | + alias CMS.Delegate.ArticleOperation |
10 | 15 | alias Helper.{ORM, QueryBuilder} |
11 | 16 |
|
12 | | - alias CMS.{Author, Community} |
| 17 | + alias CMS.{Author, Community, Tag} |
| 18 | + alias Ecto.Multi |
13 | 19 |
|
14 | 20 | @doc """ |
15 | 21 | get paged post / job ... |
@@ -66,15 +72,62 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do |
66 | 72 | def create_content(%Community{id: community_id}, thread, attrs, %User{id: user_id}) do |
67 | 73 | with {:ok, author} <- ensure_author_exists(%User{id: user_id}), |
68 | 74 | {:ok, action} <- match_action(thread, :community), |
69 | | - {:ok, community} <- ORM.find(Community, community_id), |
70 | | - {:ok, content} <- |
71 | | - action.target |
72 | | - |> struct() |
73 | | - |> action.target.changeset(attrs) |
74 | | - |> Ecto.Changeset.put_change(:author_id, author.id) |
75 | | - |> Repo.insert() do |
76 | | - Statistics.log_publish_action(%User{id: user_id}) |
77 | | - ArticleOperation.set_community(community, thread, content.id) |
| 75 | + {:ok, community} <- ORM.find(Community, community_id) do |
| 76 | + Multi.new() |
| 77 | + |> Multi.run(:add_content_author, fn _ -> |
| 78 | + action.target |
| 79 | + |> struct() |
| 80 | + |> action.target.changeset(attrs) |
| 81 | + |> Ecto.Changeset.put_change(:author_id, author.id) |
| 82 | + |> Repo.insert() |
| 83 | + end) |
| 84 | + |> Multi.run(:set_community, fn %{add_content_author: content} -> |
| 85 | + ArticleOperation.set_community(community, thread, content.id) |
| 86 | + end) |
| 87 | + |> Multi.run(:set_tag, fn %{add_content_author: content} -> |
| 88 | + case attrs |> Map.has_key?(:tags) do |
| 89 | + true -> set_tags(community, thread, content.id, attrs.tags) |
| 90 | + false -> {:ok, "pass"} |
| 91 | + end |
| 92 | + end) |
| 93 | + |> Multi.run(:log_action, fn _ -> |
| 94 | + Statistics.log_publish_action(%User{id: user_id}) |
| 95 | + end) |
| 96 | + |> Repo.transaction() |
| 97 | + |> create_content_result() |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + defp create_content_result({:ok, %{add_content_author: result}}), do: {:ok, result} |
| 102 | + |
| 103 | + defp create_content_result({:error, :add_content_author, _result, _steps}) do |
| 104 | + {:error, [message: "assign author", code: ecode(:create_fails)]} |
| 105 | + end |
| 106 | + |
| 107 | + defp create_content_result({:error, :set_community, _result, _steps}) do |
| 108 | + {:error, [message: "set community", code: ecode(:create_fails)]} |
| 109 | + end |
| 110 | + |
| 111 | + defp create_content_result({:error, :set_tag, result, _steps}) do |
| 112 | + {:error, result} |
| 113 | + end |
| 114 | + |
| 115 | + defp create_content_result({:error, :log_action, result, _steps}) do |
| 116 | + {:error, [message: "log action", code: ecode(:create_fails)]} |
| 117 | + end |
| 118 | + |
| 119 | + # if empty just pass |
| 120 | + defp set_tags(community, thread, content_id, []), do: {:ok, "pass"} |
| 121 | + |
| 122 | + defp set_tags(community, thread, content_id, tags) do |
| 123 | + try do |
| 124 | + Enum.each(tags, fn tag -> |
| 125 | + {:ok, _} = ArticleOperation.set_tag(community, thread, %Tag{id: tag.id}, content_id) |
| 126 | + end) |
| 127 | + |
| 128 | + {:ok, "psss"} |
| 129 | + rescue |
| 130 | + _ -> {:error, [message: "set tag", code: ecode(:create_fails)]} |
78 | 131 | end |
79 | 132 | end |
80 | 133 |
|
|
0 commit comments