-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathai_comments.ex
More file actions
95 lines (83 loc) · 2.36 KB
/
ai_comments.ex
File metadata and controls
95 lines (83 loc) · 2.36 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
defmodule Cadet.AIComments do
@moduledoc """
Handles operations related to AI comments, including creation, updates, and retrieval.
"""
import Ecto.Query
alias Cadet.Repo
alias Cadet.AIComments.AIComment
@doc """
Creates a new AI comment log entry.
"""
def create_ai_comment(attrs \\ %{}) do
%AIComment{}
|> AIComment.changeset(attrs)
|> Repo.insert()
end
@doc """
Gets an AI comment by ID.
"""
def get_ai_comment!(id), do: Repo.get!(AIComment, id)
@doc """
Retrieves an AI comment for a specific submission and question.
Returns `nil` if no comment exists.
"""
def get_ai_comments_for_submission(submission_id, question_id) do
Repo.one(
from(c in AIComment,
where: c.submission_id == ^submission_id and c.question_id == ^question_id
)
)
end
@doc """
Retrieves the latest AI comment for a specific submission and question.
Returns `nil` if no comment exists.
"""
def get_latest_ai_comment(submission_id, question_id) do
Repo.one(
from(c in AIComment,
where: c.submission_id == ^submission_id and c.question_id == ^question_id,
order_by: [desc: c.inserted_at],
limit: 1
)
)
end
@doc """
Updates the final comment for a specific submission and question.
Returns the most recent comment entry for that submission/question.
"""
def update_final_comment(submission_id, question_id, final_comment) do
comment = get_latest_ai_comment(submission_id, question_id)
case comment do
nil ->
{:error, :not_found}
_ ->
comment
|> AIComment.changeset(%{final_comment: final_comment})
|> Repo.update()
end
end
@doc """
Updates an existing AI comment with new attributes.
"""
def update_ai_comment(id, attrs) do
id
|> get_ai_comment!()
|> AIComment.changeset(attrs)
|> Repo.update()
end
@doc """
Updates the chosen comments for a specific submission and question.
Accepts an array of comments and replaces the existing array in the database.
"""
def update_chosen_comments(submission_id, question_id, new_comments) do
comment = get_latest_ai_comment(submission_id, question_id)
case comment do
nil ->
{:error, :not_found}
_ ->
comment
|> AIComment.changeset(%{comment_chosen: new_comments})
|> Repo.update()
end
end
end