Use embedded schemas for LastFm track, artist and album

Preparation for persisting the scrobble activity to database
This commit is contained in:
Claudio Ortolina
2025-05-30 17:01:12 +01:00
parent 06511f89db
commit 46ffa375af
3 changed files with 32 additions and 25 deletions
+5 -2
View File
@@ -1,9 +1,12 @@
defmodule LastFm.Album do
@enforce_keys [:musicbrainz_id, :title]
defstruct [:musicbrainz_id, :title]
use Ecto.Schema
@type t :: %__MODULE__{
musicbrainz_id: String.t(),
title: String.t()
}
embedded_schema do
field :musicbrainz_id, :string
field :title, :string
end
end
+12 -2
View File
@@ -1,6 +1,5 @@
defmodule LastFm.Artist do
@enforce_keys [:musicbrainz_id, :name]
defstruct [:musicbrainz_id, :name, :summary, :bio, :image, :play_count, :on_tour, :base_url]
use Ecto.Schema
@type t :: %__MODULE__{
musicbrainz_id: String.t(),
@@ -13,6 +12,17 @@ defmodule LastFm.Artist do
base_url: String.t()
}
embedded_schema do
field :musicbrainz_id, :string
field :name, :string
field :summary, :string
field :bio, :string
field :image, :string
field :play_count, :integer, default: 0
field :on_tour, :boolean, default: false
field :base_url, :string
end
def from_api_response(api_response) do
%__MODULE__{
musicbrainz_id: api_response["mbid"],
+15 -21
View File
@@ -5,28 +5,9 @@ defmodule LastFm.Track do
- musicbrainz_id can be an empty string
"""
alias LastFm.{Album, Artist}
use Ecto.Schema
@enforce_keys [
:musicbrainz_id,
:title,
:artist,
:album,
:cover_url,
:scrobbled_at_uts,
:scrobbled_at_label,
:last_fm_data
]
defstruct [
:musicbrainz_id,
:title,
:artist,
:album,
:cover_url,
:scrobbled_at_uts,
:scrobbled_at_label,
:last_fm_data
]
alias LastFm.{Album, Artist}
@type t :: %__MODULE__{
musicbrainz_id: String.t(),
@@ -39,6 +20,19 @@ defmodule LastFm.Track do
last_fm_data: map()
}
embedded_schema do
field :musicbrainz_id, :string
field :title, :string
field :cover_url, :string
field :scrobbled_at_uts, :integer
field :scrobbled_at_label, :string
embeds_one :artist, Artist
embeds_one :album, Album
field :last_fm_data, :map, default: %{}
end
def from_api_response(raw_tracks) do
Enum.map(raw_tracks, fn t ->
album = %Album{