Parse matching record fields in Records.Record

This commit is contained in:
Claudio Ortolina
2026-04-14 16:51:06 +01:00
parent 8db324d74a
commit 2bb755b1c8
2 changed files with 27 additions and 17 deletions
+1 -17
View File
@@ -599,22 +599,6 @@ defmodule MusicLibrary.ListeningStats do
defp parse_matching_records(json) when is_binary(json) do defp parse_matching_records(json) when is_binary(json) do
json json
|> JSON.decode!() |> JSON.decode!()
|> Enum.map(fn record -> |> Enum.map(&Record.parse_matching_record/1)
%{
id: record["id"],
title: record["title"],
format: record["format"],
type: record["type"],
purchased_at: parse_purchased_at(record["purchased_at"]),
cover_hash: record["cover_hash"]
}
end)
end
defp parse_purchased_at(nil), do: nil
defp parse_purchased_at(dt_string) do
{:ok, dt, _offset} = DateTime.from_iso8601(dt_string)
dt
end end
end end
+26
View File
@@ -321,4 +321,30 @@ defmodule MusicLibrary.Records.Record do
def format_as_date(dt) do def format_as_date(dt) do
Calendar.strftime(dt, "%d/%m/%Y") Calendar.strftime(dt, "%d/%m/%Y")
end end
@spec parse_matching_record(map()) :: map()
def parse_matching_record(%{
"id" => id,
"title" => title,
"format" => format,
"type" => type,
"purchased_at" => purchased_at,
"cover_hash" => cover_hash
}) do
%{
id: id,
title: title,
format: String.to_existing_atom(format),
type: String.to_existing_atom(type),
purchased_at: parse_datetime(purchased_at),
cover_hash: cover_hash
}
end
defp parse_datetime(nil), do: nil
defp parse_datetime(dt_string) do
{:ok, dt, _offset} = DateTime.from_iso8601(dt_string)
dt
end
end end