Skip to content

Commit

Permalink
Merge pull request #54 from StarDylan/searchsong
Browse files Browse the repository at this point in the history
Searchsong
  • Loading branch information
cup0noodles authored Dec 6, 2023
2 parents 7c73ff0 + ae3a51a commit f83206d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
15 changes: 14 additions & 1 deletion docs/APISpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
55 changes: 54 additions & 1 deletion src/api/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f83206d

Please sign in to comment.