1515)
1616from src .dataclasses .album import Album
1717from src .dataclasses .playlist import Playlist
18- from peewee import fn
18+ from peewee import fn , prefetch
1919from playhouse .shortcuts import model_to_dict
20+ import time
2021
2122
2223def get_playlist_by_id_or_none (id : str ):
@@ -167,9 +168,8 @@ def get_playlist_albums(playlist_id: str) -> List[DbAlbum]:
167168
168169
169170def get_playlist_albums_with_genres (playlist_id : str ) -> List [dict ]:
170- # Step 1: Retrieve all albums associated with the given playlist
171171 albums_query = (
172- DbAlbum .select ()
172+ DbAlbum .select (DbAlbum . id , DbAlbum . name , DbAlbum . image_url )
173173 .join (
174174 PlaylistAlbumRelationship ,
175175 on = (PlaylistAlbumRelationship .album == DbAlbum .id ),
@@ -179,37 +179,23 @@ def get_playlist_albums_with_genres(playlist_id: str) -> List[dict]:
179179 .order_by (PlaylistAlbumRelationship .album_index .asc ())
180180 )
181181
182- # Step 2: Retrieve genres and artists for each album
183- albums_with_details = []
184- for album in albums_query :
185- genres = (
186- DbGenre .select (DbGenre .name )
187- .join (
188- AlbumGenreRelationship , on = (AlbumGenreRelationship .genre == DbGenre .id )
189- )
190- .where (AlbumGenreRelationship .album == album .id )
191- .execute ()
192- )
182+ albums_with_genres_and_artists = prefetch (
183+ albums_query , AlbumGenreRelationship , DbGenre , AlbumArtistRelationship , DbArtist
184+ )
193185
194- artists = (
195- DbArtist .select ()
196- .join (
197- AlbumArtistRelationship ,
198- on = (AlbumArtistRelationship .artist == DbArtist .id ),
199- )
200- .where (AlbumArtistRelationship .album == album .id )
201- )
186+ albums_with_details = []
187+ for album in albums_with_genres_and_artists :
188+ genres = [genre_rel .genre .name for genre_rel in album .genres ]
189+ artists = [{"name" : artist_rel .artist .name } for artist_rel in album .artists ]
202190
203191 album_details = {
204192 "id" : album .id ,
205193 "name" : album .name ,
206- "uri" : album .uri ,
207194 "image_url" : album .image_url ,
208- "release_date" : album .release_date ,
209- "total_tracks" : album .total_tracks ,
210- "genres" : [genre .name for genre in genres ],
211- "artists" : [model_to_dict (artist ) for artist in artists ],
195+ "genres" : genres ,
196+ "artists" : artists ,
212197 }
198+
213199 albums_with_details .append (album_details )
214200
215201 return albums_with_details
@@ -234,52 +220,42 @@ def playlist_has_null_album_indexes(playlist_id: str):
234220
235221def get_playlist_track_list (playlist_id : str ):
236222 query = (
237- DbTrack .select (DbTrack , DbAlbum , PlaylistAlbumRelationship .album_index )
238- .join (DbAlbum , on = (DbTrack .album == DbAlbum .id ))
223+ DbTrack .select (
224+ DbTrack .name ,
225+ DbTrack .id ,
226+ DbAlbum .name ,
227+ PlaylistAlbumRelationship .album_index ,
228+ )
229+ .join (DbAlbum )
239230 .join (
240231 PlaylistAlbumRelationship ,
241- on = (PlaylistAlbumRelationship .album == DbAlbum . id ),
232+ on = (DbTrack .album == PlaylistAlbumRelationship . album ),
242233 )
243234 .where (PlaylistAlbumRelationship .playlist == playlist_id )
244235 .order_by (
245- PlaylistAlbumRelationship .album_index . asc () ,
246- DbTrack .disc_number . asc () ,
247- DbTrack .track_number . asc () ,
236+ PlaylistAlbumRelationship .album_index ,
237+ DbTrack .disc_number ,
238+ DbTrack .track_number ,
248239 )
249240 )
250241
251- # Preparing the result as a list of dictionaries
252- results = []
253- for track in query :
254- track_dict = model_to_dict (track , recurse = False )
255- album_dict = model_to_dict (track .album , recurse = False )
256-
257- # Fetch all artists related to this track
258- artists_query = (
259- DbArtist .select ()
260- .join (
261- TrackArtistRelationship ,
262- on = (DbArtist .id == TrackArtistRelationship .artist ),
263- )
264- .where (TrackArtistRelationship .track == track .id )
265- )
266-
267- # Serialize the artists as a list of dictionaries
268- artists_list = [
269- model_to_dict (artist , recurse = False ) for artist in artists_query
270- ]
271-
272- # Adding the album and artist information to the track
273- track_dict ["album" ] = album_dict
274- track_dict ["artists" ] = artists_list
275-
276- # Accessing the album_index from the PlaylistAlbumRelationship
277- track_dict ["album_index" ] = track .album .playlistalbumrelationship .album_index
242+ tracks_with_artists = prefetch (query , TrackArtistRelationship , DbArtist )
278243
279- results .append (track_dict )
244+ result = []
245+ for track in tracks_with_artists :
246+ artists = [{"name" : rel .artist .name for rel in track .artists }]
247+ result .append (
248+ {
249+ "id" : track .id ,
250+ "name" : track .name ,
251+ "album" : {
252+ "name" : track .album .name ,
253+ },
254+ "artists" : artists ,
255+ }
256+ )
280257
281- # Returning the results as JSON
282- return results
258+ return result
283259
284260
285261def get_playlist_duration (playlist_id ):
0 commit comments