File size: 938 Bytes
165b349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712bb74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165b349
 
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
defmodule MedicalTranscription.CodeFeedback do
  use Ecto.Schema

  schema "code_feedbacks" do
    field :text, :string
    field :code, :string
    field :response, :boolean
  end

  def changeset(code_feedback, params \\ %{}) do
    code_feedback
    |> Ecto.Changeset.cast(params, [:text, :code, :response])
    |> Ecto.Changeset.validate_required([:text, :code, :response])
  end

  def track_response(params) do
    changeset =
      %MedicalTranscription.CodeFeedback{}
      |> changeset(params)

    case MedicalTranscription.Repo.insert(changeset) do
      {:ok, _code_feedback} ->
        {:ok, "Success!"}

      {:error, changeset} ->
        messages =
          Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
            Enum.reduce(opts, msg, fn {key, value}, acc ->
              String.replace(acc, "%{#{key}}", to_string(value))
            end)
          end)

        {:error, messages}
    end
  end
end