add recent items to the get_all_favorites api route

This commit is contained in:
geoffrey45 2023-04-30 15:38:38 +03:00
parent 3ddb6295c6
commit 5d0b59ea60
4 changed files with 71 additions and 6 deletions

View File

@ -7,6 +7,8 @@ from app.utils.bisection import UseBisection
from app.store.artists import ArtistStore
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
from app.serializers.favorites_serializer import recent_fav_track_serializer, recent_fav_album_serializer, \
recent_fav_artist_serializer
api = Blueprint("favorite", __name__, url_prefix="/")
@ -153,8 +155,6 @@ def get_all_favorites():
favs = favdb.get_all()
favs.reverse()
favs = [fav for fav in favs if fav[1] != ""]
tracks = []
albums = []
artists = []
@ -191,7 +191,33 @@ def get_all_favorites():
albums = remove_none(albums)
artists = remove_none(artists)
recents = []
first_n = favs[:album_limit]
for fav in first_n:
if fav[2] == FavType.track:
track = [t for t in tracks if t.trackhash == fav[1]][0]
recents.append({
"type": "track",
"item": recent_fav_track_serializer(track)
})
elif fav[2] == FavType.album:
album = [a for a in albums if a.albumhash == fav[1]][0]
recents.append({
"type": "album",
"item": recent_fav_album_serializer(album)
})
elif fav[2] == FavType.artist:
artist = [a for a in artists if a.artisthash == fav[1]][0]
recents.append({
"type": "artist",
"item": recent_fav_artist_serializer(artist)
})
return {
"recents": recents,
"tracks": tracks,
"albums": albums,
"artists": artists,

View File

@ -37,7 +37,8 @@ class SQLiteFavoriteMethods:
sql = """SELECT * FROM favorites"""
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql)
return cur.fetchall()
favs = cur.fetchall()
return [fav for fav in favs if fav[1] != ""]
@classmethod
def get_favorites(cls, fav_type: str) -> list[tuple]:

View File

View File

@ -0,0 +1,38 @@
from app.models import Track, Album, Artist
def recent_fav_track_serializer(track: Track) -> dict:
"""
Simplifies a track object into a dictionary to remove unused
properties on the client.
"""
return {
"image": track.image,
"title": track.title,
"trackhash": track.trackhash,
"filepath": track.filepath,
}
def recent_fav_album_serializer(album: Album) -> dict:
"""
Simplifies an album object into a dictionary to remove unused
properties on the client.
"""
return {
"image": album.image,
"title": album.title,
"albumhash": album.albumhash,
}
def recent_fav_artist_serializer(artist: Artist) -> dict:
"""
Simplifies an artist object into a dictionary to remove unused
properties on the client.
"""
return {
"image": artist.image,
"name": artist.name,
"artisthash": artist.artisthash,
}