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 | |