113 lines
3.4 KiB
Python
113 lines
3.4 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from pydantic import BaseModel
|
|
from app.database import get_db
|
|
from app.models.models import Playlist, Song, User
|
|
from app.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
class PlaylistCreate(BaseModel):
|
|
name: str
|
|
description: str | None = None
|
|
is_public: bool = False
|
|
|
|
class PlaylistResponse(BaseModel):
|
|
id: int
|
|
name: str
|
|
description: str | None
|
|
is_public: bool
|
|
owner_id: int
|
|
songs: List[dict]
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
@router.post("/create")
|
|
def create_playlist(
|
|
playlist: PlaylistCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
db_playlist = Playlist(
|
|
name=playlist.name,
|
|
description=playlist.description,
|
|
is_public=playlist.is_public,
|
|
owner_id=current_user.id
|
|
)
|
|
db.add(db_playlist)
|
|
db.commit()
|
|
db.refresh(db_playlist)
|
|
return db_playlist
|
|
|
|
@router.get("/my-playlists")
|
|
def get_my_playlists(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
|
return db.query(Playlist).filter(Playlist.owner_id == current_user.id).all()
|
|
|
|
@router.get("/public")
|
|
def get_public_playlists(db: Session = Depends(get_db)):
|
|
return db.query(Playlist).filter(Playlist.is_public == True).all()
|
|
|
|
@router.post("/{playlist_id}/add-song/{song_id}")
|
|
def add_song_to_playlist(
|
|
playlist_id: int,
|
|
song_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
playlist = db.query(Playlist).filter(Playlist.id == playlist_id).first()
|
|
if not playlist or playlist.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not authorized")
|
|
|
|
song = db.query(Song).filter(Song.id == song_id).first()
|
|
if not song:
|
|
raise HTTPException(status_code=404, detail="Song not found")
|
|
|
|
playlist.songs.append(song)
|
|
db.commit()
|
|
return {"message": "Song added to playlist"}
|
|
|
|
@router.get("/{playlist_id}")
|
|
def get_playlist(playlist_id: int, db: Session = Depends(get_db)):
|
|
playlist = db.query(Playlist).filter(Playlist.id == playlist_id).first()
|
|
if not playlist:
|
|
raise HTTPException(status_code=404, detail="Playlist not found")
|
|
|
|
songs_data = [{
|
|
"id": song.id,
|
|
"title": song.title,
|
|
"artist": song.artist,
|
|
"file_path": song.file_path,
|
|
"cover_path": song.cover_path,
|
|
"duration": song.duration,
|
|
"owner_id": song.owner_id
|
|
} for song in playlist.songs]
|
|
|
|
return {
|
|
"id": playlist.id,
|
|
"name": playlist.name,
|
|
"description": playlist.description,
|
|
"is_public": playlist.is_public,
|
|
"owner_id": playlist.owner_id,
|
|
"songs": songs_data
|
|
}
|
|
|
|
@router.delete("/{playlist_id}/remove-song/{song_id}")
|
|
def remove_song_from_playlist(
|
|
playlist_id: int,
|
|
song_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
playlist = db.query(Playlist).filter(Playlist.id == playlist_id).first()
|
|
if not playlist or playlist.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not authorized")
|
|
|
|
song = db.query(Song).filter(Song.id == song_id).first()
|
|
if song in playlist.songs:
|
|
playlist.songs.remove(song)
|
|
db.commit()
|
|
|
|
return {"message": "Song removed from playlist"}
|