|
1 | 1 | defmodule MastaniServer.CMS.Delegate.CommentCURD do |
| 2 | + @moduledoc """ |
| 3 | + CURD for comments |
| 4 | + """ |
2 | 5 | import Ecto.Query, warn: false |
3 | 6 | import Helper.Utils, only: [done: 1] |
| 7 | + import Helper.ErrorCode |
| 8 | + |
4 | 9 | import MastaniServer.CMS.Utils.Matcher |
5 | 10 | import ShortMaps |
6 | 11 |
|
7 | 12 | alias MastaniServer.{Repo, Accounts} |
8 | 13 | alias Helper.{ORM, QueryBuilder} |
9 | 14 | alias MastaniServer.CMS.{PostCommentReply, JobCommentReply} |
10 | 15 |
|
| 16 | + alias Ecto.Multi |
| 17 | + |
11 | 18 | @doc """ |
12 | 19 | Creates a comment for psot, job ... |
13 | 20 | """ |
@@ -38,20 +45,36 @@ defmodule MastaniServer.CMS.Delegate.CommentCURD do |
38 | 45 | def delete_comment(thread, content_id) do |
39 | 46 | with {:ok, action} <- match_action(thread, :comment), |
40 | 47 | {:ok, comment} <- ORM.find(action.reactor, content_id) do |
41 | | - # TODO: should use Nulti |
42 | | - case ORM.delete(comment) do |
43 | | - {:ok, comment} -> |
| 48 | + Multi.new() |
| 49 | + |> Multi.run(:delete_comment, fn _ -> |
| 50 | + ORM.delete(comment) |
| 51 | + end) |
| 52 | + |> Multi.run(:update_floor, fn _ -> |
| 53 | + ret = |
44 | 54 | Repo.update_all( |
45 | 55 | from(p in action.reactor, where: p.id > ^comment.id), |
46 | 56 | inc: [floor: -1] |
47 | 57 | ) |
| 58 | + |> done() |
| 59 | + |
| 60 | + case ret do |
| 61 | + {:ok, _} -> {:ok, comment} |
| 62 | + _ -> {:error, ""} |
| 63 | + end |
| 64 | + end) |
| 65 | + |> Repo.transaction() |
| 66 | + |> delete_comment_result() |
| 67 | + end |
| 68 | + end |
48 | 69 |
|
49 | | - {:ok, comment} |
| 70 | + defp delete_comment_result({:ok, %{delete_comment: result}}), do: {:ok, result} |
50 | 71 |
|
51 | | - {:error, error} -> |
52 | | - {:error, error} |
53 | | - end |
54 | | - end |
| 72 | + defp delete_comment_result({:error, :delete_comment, result, _steps}) do |
| 73 | + {:error, [message: "delete comment fails", code: ecode(:delete_fails)]} |
| 74 | + end |
| 75 | + |
| 76 | + defp delete_comment_result({:error, :update_floor, _result, _steps}) do |
| 77 | + {:error, [message: "update follor fails", code: ecode(:delete_fails)]} |
55 | 78 | end |
56 | 79 |
|
57 | 80 | def list_comments(thread, content_id, %{page: page, size: size} = filters) do |
|
0 commit comments