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