Skip to content

Commit

Permalink
Merge pull request #75 from jqueja/music
Browse files Browse the repository at this point in the history
Music
  • Loading branch information
joanna-chou authored Dec 5, 2023
2 parents 035072c + 4358086 commit 8334a5e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
Binary file modified backend/src/api/__pycache__/login.cpython-311.pyc
Binary file not shown.
Binary file modified backend/src/api/__pycache__/server.cpython-311.pyc
Binary file not shown.
Binary file modified backend/src/api/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified backend/src/api/__pycache__/signup.cpython-311.pyc
Binary file not shown.
Binary file modified backend/src/api/__pycache__/workouts.cpython-311.pyc
Binary file not shown.
124 changes: 124 additions & 0 deletions backend/src/api/music.py
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"
3 changes: 2 additions & 1 deletion backend/src/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import ValidationError
from src.api import settings, login, signup, info, workouts
from src.api import settings, login, signup, info, workouts, music
import json
import logging
import sys
Expand Down Expand Up @@ -40,6 +40,7 @@
app.include_router(signup.router)
# app.include_router(info.router)
app.include_router(workouts.router)
app.include_router(music.router)

@app.exception_handler(exceptions.RequestValidationError)
@app.exception_handler(ValidationError)
Expand Down

0 comments on commit 8334a5e

Please sign in to comment.