diff --git a/app/api/favorites.py b/app/api/favorites.py index 7883cbc..e5b0c12 100644 --- a/app/api/favorites.py +++ b/app/api/favorites.py @@ -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,17 +155,15 @@ def get_all_favorites(): favs = favdb.get_all() favs.reverse() - favs = [fav for fav in favs if fav[1] != ""] - tracks = [] albums = [] artists = [] for fav in favs: if ( - len(tracks) >= track_limit - and len(albums) >= album_limit - and len(artists) >= artist_limit + len(tracks) >= track_limit + and len(albums) >= album_limit + and len(artists) >= artist_limit ): break @@ -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, diff --git a/app/db/sqlite/favorite.py b/app/db/sqlite/favorite.py index 56573a3..b7c9368 100644 --- a/app/db/sqlite/favorite.py +++ b/app/db/sqlite/favorite.py @@ -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]: diff --git a/app/serializers/__init__.py b/app/serializers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/serializers/favorites_serializer.py b/app/serializers/favorites_serializer.py new file mode 100644 index 0000000..f907c64 --- /dev/null +++ b/app/serializers/favorites_serializer.py @@ -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, + }