I am a newbie to elixir. I have a Ecto Schema
defmodule MyScoreSchema do
use Ecto.Schema
import Ecto.Changeset
schema "historical_extra_fuels" do
field :average, :float
field :count, :float
field :percent, :float
field :name, :string
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:average, :count, :percent])
|> validate_required([])
end
end
and parent schema
defmodule OverallScore do
use Ecto.Schema
import Ecto.Changeset
schema "OverallScore" do
field :avg_pass, :float
field :avg_fail, :float
field :total_students, :float
embeds_many :my_score_schema, MyScoreSchema
end
@required_fields ~w[]a
@optional_fields ~w[avg_pass, avg_fail, total_students ]a
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @optional_fields, required: false )
|> cast_embed(:my_score_schema, required: false)
end
end
And have a HTTP REST API http://localhost:8080/getScoreData
which gives data
{
"avgPass": 85.55,
"avgFail": 14.45,
"totalStudents": 80.0,
"myScoreSchema": [
{
"average": 80.0,
"count": 8.0,
"percent": 80.0,
"name": "John"
},
{
"average": 90.0,
"count": 8.0,
"percent": 90.0,
"name": "Cena"
},
{
"average": 80.0,
"count": 8.0,
"percent": 80.0,
"name": "Sunny"
},
{
"average": 70.0,
"count": 8.0,
"percent": 70.0,
"name": "Michael"
}
]
}
and the code
url = "http://localhost:8080/getScoreData"
Logger.info("the url is #{url}")
case HTTPoison.get(url) do
{:ok, %{status_code: 200, body: body}} ->
overall_score = Jason.decode!(body, as: [%OverallScore{}])
{:ok, overall_score}
end
This somehow works and don't give error but the result is some struct
and not really OverallScore
ecto
schema object