diff --git a/lib/last_fm/album.ex b/lib/last_fm/album.ex index fb2964ae..206198b3 100644 --- a/lib/last_fm/album.ex +++ b/lib/last_fm/album.ex @@ -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 diff --git a/lib/last_fm/artist.ex b/lib/last_fm/artist.ex index 1ef6d389..51c980c0 100644 --- a/lib/last_fm/artist.ex +++ b/lib/last_fm/artist.ex @@ -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"], diff --git a/lib/last_fm/track.ex b/lib/last_fm/track.ex index 2e9460a5..4cccf2a7 100644 --- a/lib/last_fm/track.ex +++ b/lib/last_fm/track.ex @@ -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{