-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from jqueja/music
Music
- Loading branch information
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from fastapi import APIRouter, Request, HTTPException, status | ||
from sqlalchemy import create_engine, text | ||
import sqlalchemy | ||
from src import database as db | ||
from dotenv import load_dotenv | ||
from fastapi.responses import JSONResponse | ||
from uuid import UUID | ||
import re | ||
load_dotenv() | ||
|
||
import os | ||
from supabase import create_client | ||
from pydantic import BaseModel | ||
|
||
|
||
url = os.environ.get("SUPABASE_URL") | ||
key = os.environ.get("SUPABASE_KEY") | ||
supabase = create_client(url, key) | ||
|
||
|
||
router = APIRouter( | ||
prefix="/music", | ||
tags=["music"], | ||
) | ||
|
||
class MusicRequest(BaseModel): | ||
user_id: UUID | ||
link: str | ||
|
||
|
||
uuid_pattern = re.compile(r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') | ||
|
||
@router.get("/all-links/{user_id}") | ||
def get_music_list(user_id): | ||
|
||
if not uuid_pattern.match(user_id): | ||
raise HTTPException(status_code=400, detail="Invalid UUID format") | ||
|
||
with db.engine.begin() as connection: | ||
result = connection.execute( | ||
sqlalchemy.text( | ||
""" | ||
SELECT link | ||
FROM music_links | ||
WHERE "user" = :user_id | ||
"""), | ||
{"user_id": user_id} | ||
) | ||
|
||
playlists = [row[0] for row in result.fetchall()] | ||
|
||
# If no links are found, raise a 404 Not Found exception | ||
if not playlists: | ||
raise HTTPException(status_code=404, detail="No music links found for the specified user") | ||
|
||
return {"playlist_links": playlists} | ||
|
||
|
||
@router.post("/add-music/{user_id}/{link}") | ||
def add_music(request: MusicRequest): | ||
|
||
# Checks for duplications in the database | ||
with db.engine.begin() as connection: | ||
record = connection.execute( | ||
sqlalchemy.text( | ||
""" | ||
SELECT * | ||
FROM music_links | ||
WHERE "user" = :user_id AND "link" = :user_link | ||
"""), | ||
{"user_id": request.user_id, "user_link": request.link} | ||
) | ||
|
||
existing_record = record.fetchone() | ||
|
||
if existing_record: | ||
raise HTTPException(status_code=409, detail="Song is already associated with user") | ||
|
||
# Add the song | ||
with db.engine.begin() as connection: | ||
result = connection.execute( | ||
sqlalchemy.text( | ||
""" | ||
INSERT INTO music_links ("user", "link") | ||
VALUES (:user_id, :user_link) | ||
"""), | ||
{"user_id": request.user_id, "user_link": request.link} | ||
) | ||
|
||
return "OK" | ||
|
||
@router.post("/delete-music/{user_id}/{link}") | ||
def delete_music(request: MusicRequest): | ||
|
||
# Checks if the record exists in the database | ||
with db.engine.begin() as connection: | ||
record = connection.execute( | ||
sqlalchemy.text( | ||
""" | ||
SELECT * | ||
FROM music_links | ||
WHERE "user" = :user_id AND "link" = :user_link | ||
"""), | ||
{"user_id": request.user_id, "user_link": request.link} | ||
) | ||
|
||
existing_record = record.fetchone() | ||
|
||
# If the record does not exist, raise a 404 Not Found exception | ||
if not existing_record: | ||
raise HTTPException(status_code=404, detail="Song not found for the specified user") | ||
|
||
# If the record exists, delete the song | ||
with db.engine.begin() as connection: | ||
result = connection.execute( | ||
sqlalchemy.text( | ||
""" | ||
DELETE FROM music_links | ||
WHERE "user" = :user_id AND "link" = :user_link | ||
"""), | ||
{"user_id": request.user_id, "user_link": request.link} | ||
) | ||
|
||
return "OK" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters