/
comment.ex
64 lines (54 loc) · 1.86 KB
/
comment.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Accent.GraphQL.Resolvers.Comment do
alias Accent.Scopes.Comment, as: CommentScope
alias Accent.{
Comment,
GraphQL.Paginated,
Hook,
Plugs.GraphQLContext,
Project,
Repo,
Translation
}
@typep comment_operation :: {:ok, %{comment: Comment.t() | nil, errors: [String.t()] | nil}}
@spec create(Translation.t(), %{text: String.t()}, GraphQLContext.t()) :: comment_operation
def create(translation, args, info) do
comment_params = %{
"text" => args.text,
"user_id" => info.context[:conn].assigns[:current_user].id,
"translation_id" => translation.id
}
changeset = Comment.changeset(%Comment{}, comment_params)
case Repo.insert(changeset) do
{:ok, comment} ->
comment = Repo.preload(comment, [:user, translation: [revision: :project]])
Accent.Hook.notify(%Hook.Context{
event: "create_comment",
project: comment.translation.revision.project,
user: info.context[:conn].assigns[:current_user],
payload: %{
comment: comment
}
})
{:ok, %{comment: comment, errors: nil}}
{:error, _reason} ->
{:ok, %{comment: nil, errors: ["unprocessable_entity"]}}
end
end
@spec list_project(Project.t(), %{page: number()}, GraphQLContext.t()) :: {:ok, Paginated.t(Comment.t())}
def list_project(project, args, _) do
Comment
|> CommentScope.from_project(project.id)
|> Repo.paginate(page: args[:page])
|> Paginated.format()
|> (&{:ok, &1}).()
end
@spec list_translation(Translation.t(), %{page: number()}, GraphQLContext.t()) :: {:ok, Paginated.t(Comment.t())}
def list_translation(translation, args, _) do
translation
|> Ecto.assoc(:comments)
|> CommentScope.default_order()
|> Repo.paginate(page: args[:page])
|> Paginated.format()
|> (&{:ok, &1}).()
end
end