11from typing import List , Optional
22from src .database .crud .album import create_album_or_none
3- from src .database .models import DbAlbum , DbPlaylist , DbUser , PlaylistAlbumRelationship
3+ from src .database .models import (
4+ AlbumArtistRelationship ,
5+ AlbumGenreRelationship ,
6+ DbAlbum ,
7+ DbArtist ,
8+ DbGenre ,
9+ DbPlaylist ,
10+ DbUser ,
11+ PlaylistAlbumRelationship ,
12+ peewee_model_to_dict ,
13+ )
414from src .dataclasses .album import Album
515from src .dataclasses .playlist import Playlist
16+ from peewee import fn
17+ import re
18+ from datetime import datetime
619
720
821def get_playlist_by_id_or_none (id : str ):
@@ -27,7 +40,34 @@ def create_playlist(playlist: Playlist, albums: List[Album], user: DbUser):
2740 return playlist
2841
2942
30- def update_playlist (playlist : Playlist , albums : List [Album ]):
43+ def update_playlist_info (
44+ id : str ,
45+ name : Optional [str ] = None ,
46+ description : Optional [str ] = None ,
47+ snapshot_id : Optional [str ] = None ,
48+ uri : Optional [str ] = None ,
49+ ) -> DbPlaylist | None :
50+ update_data = {}
51+ if name is not None :
52+ update_data ["name" ] = name
53+ if description is not None :
54+ update_data ["description" ] = description
55+ if snapshot_id is not None :
56+ update_data ["snapshot_id" ] = snapshot_id
57+ if uri is not None :
58+ update_data ["uri" ] = uri
59+
60+ if update_data :
61+ query = DbPlaylist .update (update_data ).where (DbPlaylist .id == id )
62+ query .execute ()
63+
64+ updated_playlist = DbPlaylist .get_by_id (id )
65+ return updated_playlist
66+
67+ return None
68+
69+
70+ def update_playlist_with_albums (playlist : Playlist , albums : List [Album ]):
3171 playlist = DbPlaylist .update (
3272 id = playlist .id ,
3373 description = playlist .description ,
@@ -78,6 +118,48 @@ def get_user_playlists(
78118 return list (query .execute ())
79119
80120
121+ def get_recent_user_playlists (
122+ user_id : str ,
123+ limit : Optional [int ] = None ,
124+ offset : Optional [int ] = None ,
125+ search : Optional [str ] = None ,
126+ ) -> List [DbPlaylist ]:
127+
128+ # Use TO_DATE function in PostgreSQL to convert the extracted date into a proper date
129+ query = (
130+ DbPlaylist .select ()
131+ .where (
132+ (
133+ DbPlaylist .name .startswith ("New Albums" )
134+ | DbPlaylist .name .startswith ("Best Albums" )
135+ )
136+ )
137+ # Convert the date string into a proper date format for ordering
138+ .order_by (fn .TO_DATE (fn .RIGHT (DbPlaylist .name , 8 ), "DD/MM/YY" ).desc ())
139+ .limit (limit )
140+ .offset (offset )
141+ )
142+
143+ if search :
144+ query = query .where (DbPlaylist .name .contains (search ))
145+
146+ playlists = []
147+ for playlist in query :
148+ # Add playlist to the result with parsed date
149+ playlists .append (
150+ {
151+ "id" : playlist .id ,
152+ "name" : playlist .name ,
153+ "description" : playlist .description ,
154+ "image_url" : playlist .image_url ,
155+ "user_id" : playlist .user .id ,
156+ "snapshot_id" : playlist .snapshot_id ,
157+ "uri" : playlist .uri ,
158+ }
159+ )
160+ return playlists
161+
162+
81163def get_playlist_albums (playlist_id : str ) -> List [DbAlbum ]:
82164 query = (
83165 DbAlbum .select ()
@@ -86,3 +168,51 @@ def get_playlist_albums(playlist_id: str) -> List[DbAlbum]:
86168 .where (DbPlaylist .id == playlist_id )
87169 )
88170 return list (query )
171+
172+
173+ def get_playlist_albums_with_genres (playlist_id : str ) -> List [dict ]:
174+ # Step 1: Retrieve all albums associated with the given playlist
175+ albums_query = (
176+ DbAlbum .select ()
177+ .join (
178+ PlaylistAlbumRelationship ,
179+ on = (PlaylistAlbumRelationship .album == DbAlbum .id ),
180+ )
181+ .join (DbPlaylist , on = (PlaylistAlbumRelationship .playlist == DbPlaylist .id ))
182+ .where (DbPlaylist .id == playlist_id )
183+ )
184+
185+ # Step 2: Retrieve genres and artists for each album
186+ albums_with_details = []
187+ for album in albums_query :
188+ genres = (
189+ DbGenre .select (DbGenre .name )
190+ .join (
191+ AlbumGenreRelationship , on = (AlbumGenreRelationship .genre == DbGenre .id )
192+ )
193+ .where (AlbumGenreRelationship .album == album .id )
194+ .execute ()
195+ )
196+
197+ artists = (
198+ DbArtist .select ()
199+ .join (
200+ AlbumArtistRelationship ,
201+ on = (AlbumArtistRelationship .artist == DbArtist .id ),
202+ )
203+ .where (AlbumArtistRelationship .album == album .id )
204+ )
205+
206+ album_details = {
207+ "id" : album .id ,
208+ "name" : album .name ,
209+ "uri" : album .uri ,
210+ "image_url" : album .image_url ,
211+ "release_date" : album .release_date ,
212+ "total_tracks" : album .total_tracks ,
213+ "genres" : [genre .name for genre in genres ],
214+ "artists" : [peewee_model_to_dict (artist ) for artist in artists ],
215+ }
216+ albums_with_details .append (album_details )
217+
218+ return albums_with_details
0 commit comments