Find last listened track based on title and artist as well
This commit is contained in:
@@ -211,17 +211,24 @@ defmodule MusicLibrary.Records do
|
||||
end
|
||||
end
|
||||
|
||||
def get_last_listened_track(record_id) do
|
||||
def get_last_listened_track(record) do
|
||||
record_id = record.id
|
||||
|
||||
q =
|
||||
from r in fragment("records, json_each(records.release_ids)"),
|
||||
where: fragment("records.id = ?", ^record_id),
|
||||
select: r.value
|
||||
|
||||
release_ids = Repo.all(q)
|
||||
main_artist_name = Record.main_artist(record).name
|
||||
record_title = record.title
|
||||
|
||||
q =
|
||||
from t in LastFm.Track,
|
||||
where: fragment("? ->> '$.musicbrainz_id'", t.album) in ^release_ids,
|
||||
or_where:
|
||||
fragment("? ->> '$.title'", t.album) == ^record_title and
|
||||
fragment("? ->> '$.name'", t.artist) == ^main_artist_name,
|
||||
order_by: [desc: t.scrobbled_at_uts],
|
||||
limit: 1
|
||||
|
||||
|
||||
@@ -37,6 +37,13 @@ defmodule MusicLibrary.Records.Record do
|
||||
Enum.map_join(record.artists, ", ", fn artist -> artist.name end)
|
||||
end
|
||||
|
||||
def main_artist(record) do
|
||||
case record.artists do
|
||||
[] -> nil
|
||||
[main_artist | _] -> main_artist
|
||||
end
|
||||
end
|
||||
|
||||
def artist_ids(record) do
|
||||
Enum.map(record.artists, fn artist -> artist.musicbrainz_id end)
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
record = Records.get_record!(id)
|
||||
last_listened_track = Records.get_last_listened_track(id)
|
||||
last_listened_track = Records.get_last_listened_track(record)
|
||||
|
||||
socket =
|
||||
if record.selected_release_id do
|
||||
|
||||
Reference in New Issue
Block a user