diff --git a/docs/APISpec.md b/docs/APISpec.md index 5e45cb4..0b7faa7 100644 --- a/docs/APISpec.md +++ b/docs/APISpec.md @@ -51,7 +51,19 @@ Add song to the song library. Each song should be a unique version. } ] ``` -## Delete Song - `/song/{song_id}/remove` (POST) +## Search Song - `/song/search/{query}` (GET) + +Searching song name and artist for the given query. + +**Request**: + +```json +{ + "query": "string", /* Any search query, is wildcarded front and back */ + "page": "int" /* page number, starting from 0 */ +} +``` +## Search Song - `/song/{song_id}/remove` (POST) Remove song from the song library. @@ -64,6 +76,7 @@ Remove song from the song library. } ] ``` + ## Play Song - `/song/{song_id}/play` (GET) (*COMPLEX*) Returns a URL to play the given song corespronging to the song_id. May play an ad. diff --git a/src/api/songs.py b/src/api/songs.py index 9d35ec3..820f3f7 100644 --- a/src/api/songs.py +++ b/src/api/songs.py @@ -285,4 +285,57 @@ def play_song(song_id: int, user_id: str = Header(None)) -> SongResponse: return SongResponse(url=query.song_url, is_ad=False) - +@router.get("/search/{query}") +def search_song(query: str, page: int=0) -> list: + library = [] + + with db.engine.begin() as conn: + library_result = conn.execute(sqlalchemy.text(""" + SELECT id, song_name, artist, album + FROM songs + WHERE + song_name like :query + OR artist like :query + LIMIT 10 + OFFSET :offset + """), + [{ + "query":query + "%", + "offset":page*10 + }]) + for song in library_result: + library.append( + { + "Song_Id": song.id, + "Song_Name": song.song_name, + "Artist": song.artist, + "Album": song.album + } + ) + if len(library) < 10: + library = [] + with db.engine.begin() as conn: + library_result = conn.execute(sqlalchemy.text(""" + SELECT id, song_name, artist, album + FROM songs + WHERE + song_name like :query + OR artist like :query + LIMIT 10 + OFFSET :offset + """), + [{ + "query": "%" + query + "%", + "offset": page*10 + }]) + + for song in library_result: + library.append( + { + "Song_Id": song.id, + "Song_Name": song.song_name, + "Artist": song.artist, + "Album": song.album + } + ) + return library \ No newline at end of file