Use embedded schemas for LastFm track, artist and album
Preparation for persisting the scrobble activity to database
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
defmodule LastFm.Album do
|
defmodule LastFm.Album do
|
||||||
@enforce_keys [:musicbrainz_id, :title]
|
use Ecto.Schema
|
||||||
defstruct [:musicbrainz_id, :title]
|
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
musicbrainz_id: String.t(),
|
musicbrainz_id: String.t(),
|
||||||
title: String.t()
|
title: String.t()
|
||||||
}
|
}
|
||||||
|
embedded_schema do
|
||||||
|
field :musicbrainz_id, :string
|
||||||
|
field :title, :string
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+12
-2
@@ -1,6 +1,5 @@
|
|||||||
defmodule LastFm.Artist do
|
defmodule LastFm.Artist do
|
||||||
@enforce_keys [:musicbrainz_id, :name]
|
use Ecto.Schema
|
||||||
defstruct [:musicbrainz_id, :name, :summary, :bio, :image, :play_count, :on_tour, :base_url]
|
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
musicbrainz_id: String.t(),
|
musicbrainz_id: String.t(),
|
||||||
@@ -13,6 +12,17 @@ defmodule LastFm.Artist do
|
|||||||
base_url: String.t()
|
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
|
def from_api_response(api_response) do
|
||||||
%__MODULE__{
|
%__MODULE__{
|
||||||
musicbrainz_id: api_response["mbid"],
|
musicbrainz_id: api_response["mbid"],
|
||||||
|
|||||||
+15
-21
@@ -5,28 +5,9 @@ defmodule LastFm.Track do
|
|||||||
- musicbrainz_id can be an empty string
|
- musicbrainz_id can be an empty string
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias LastFm.{Album, Artist}
|
use Ecto.Schema
|
||||||
|
|
||||||
@enforce_keys [
|
alias LastFm.{Album, Artist}
|
||||||
: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
|
|
||||||
]
|
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
musicbrainz_id: String.t(),
|
musicbrainz_id: String.t(),
|
||||||
@@ -39,6 +20,19 @@ defmodule LastFm.Track do
|
|||||||
last_fm_data: map()
|
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
|
def from_api_response(raw_tracks) do
|
||||||
Enum.map(raw_tracks, fn t ->
|
Enum.map(raw_tracks, fn t ->
|
||||||
album = %Album{
|
album = %Album{
|
||||||
|
|||||||
Reference in New Issue
Block a user