Commit
•
609a959
1
Parent(s):
4553650
refactor: Create shared `collect_errors` function for Ecto changesets
Browse files
lib/medical_transcription/coding.ex
CHANGED
@@ -11,23 +11,14 @@ defmodule MedicalTranscription.Coding do
|
|
11 |
alias MedicalTranscription.Coding.CodeVector
|
12 |
|
13 |
def insert_vector(params) do
|
14 |
-
changeset =
|
15 |
-
%CodeVector{}
|
16 |
-
|> CodeVector.changeset(params)
|
17 |
|
18 |
case Repo.insert(changeset) do
|
19 |
{:ok, _} ->
|
20 |
{:ok, "Success!"}
|
21 |
|
22 |
{:error, changeset} ->
|
23 |
-
|
24 |
-
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
|
25 |
-
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
26 |
-
String.replace(acc, "%{#{key}}", to_string(value))
|
27 |
-
end)
|
28 |
-
end)
|
29 |
-
|
30 |
-
{:error, messages}
|
31 |
end
|
32 |
end
|
33 |
|
|
|
11 |
alias MedicalTranscription.Coding.CodeVector
|
12 |
|
13 |
def insert_vector(params) do
|
14 |
+
changeset = CodeVector.changeset(%CodeVector{}, params)
|
|
|
|
|
15 |
|
16 |
case Repo.insert(changeset) do
|
17 |
{:ok, _} ->
|
18 |
{:ok, "Success!"}
|
19 |
|
20 |
{:error, changeset} ->
|
21 |
+
{:error, Repo.collect_errors(changeset)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
end
|
23 |
end
|
24 |
|
lib/medical_transcription/feedback.ex
CHANGED
@@ -1,22 +1,19 @@
|
|
1 |
defmodule MedicalTranscription.Feedback do
|
|
|
|
|
|
|
2 |
alias MedicalTranscription.Feedback.CodeFeedback
|
|
|
3 |
|
4 |
def track_response(params) do
|
5 |
changeset = CodeFeedback.changeset(%CodeFeedback{}, params)
|
6 |
|
7 |
-
case
|
8 |
{:ok, _code_feedback} ->
|
9 |
{:ok, "Success!"}
|
10 |
|
11 |
{:error, changeset} ->
|
12 |
-
|
13 |
-
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
|
14 |
-
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
15 |
-
String.replace(acc, "%{#{key}}", to_string(value))
|
16 |
-
end)
|
17 |
-
end)
|
18 |
-
|
19 |
-
{:error, messages}
|
20 |
end
|
21 |
end
|
22 |
end
|
|
|
1 |
defmodule MedicalTranscription.Feedback do
|
2 |
+
@moduledoc """
|
3 |
+
Context for managing user feedback on transcribed text and codes.
|
4 |
+
"""
|
5 |
alias MedicalTranscription.Feedback.CodeFeedback
|
6 |
+
alias MedicalTranscription.Repo
|
7 |
|
8 |
def track_response(params) do
|
9 |
changeset = CodeFeedback.changeset(%CodeFeedback{}, params)
|
10 |
|
11 |
+
case Repo.insert(changeset) do
|
12 |
{:ok, _code_feedback} ->
|
13 |
{:ok, "Success!"}
|
14 |
|
15 |
{:error, changeset} ->
|
16 |
+
{:error, Repo.collect_errors(changeset)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
end
|
18 |
end
|
19 |
end
|
lib/medical_transcription/repo.ex
CHANGED
@@ -2,4 +2,12 @@ defmodule MedicalTranscription.Repo do
|
|
2 |
use Ecto.Repo,
|
3 |
otp_app: :medical_transcription,
|
4 |
adapter: Ecto.Adapters.Postgres
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
end
|
|
|
2 |
use Ecto.Repo,
|
3 |
otp_app: :medical_transcription,
|
4 |
adapter: Ecto.Adapters.Postgres
|
5 |
+
|
6 |
+
def collect_errors(changeset) do
|
7 |
+
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
|
8 |
+
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
9 |
+
String.replace(acc, "%{#{key}}", to_string(value))
|
10 |
+
end)
|
11 |
+
end)
|
12 |
+
end
|
13 |
end
|