123 lines
3.3 KiB
Python
123 lines
3.3 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_active_superuser
|
|
from app.models.user import User
|
|
from app.schemas.album import Album, AlbumCreate, AlbumUpdate
|
|
from app.services.album import (
|
|
create_album,
|
|
delete_album,
|
|
get_album,
|
|
get_albums,
|
|
update_album,
|
|
)
|
|
from app.services.artist import get_artist
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Album])
|
|
def read_albums(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
artist_id: Optional[int] = Query(None, description="Filter by artist ID"),
|
|
search: Optional[str] = Query(None, description="Search by album title"),
|
|
) -> Any:
|
|
"""
|
|
Retrieve albums.
|
|
"""
|
|
albums = get_albums(db, skip=skip, limit=limit, artist_id=artist_id, search=search)
|
|
return albums
|
|
|
|
|
|
@router.post("/", response_model=Album)
|
|
def create_new_album(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
album_in: AlbumCreate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new album. Only superusers can create albums.
|
|
"""
|
|
# Check if artist exists
|
|
artist = get_artist(db, artist_id=album_in.artist_id)
|
|
if not artist:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Artist not found",
|
|
)
|
|
album = create_album(db, obj_in=album_in)
|
|
return album
|
|
|
|
|
|
@router.get("/{album_id}", response_model=Album)
|
|
def read_album(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
album_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get album by ID.
|
|
"""
|
|
album = get_album(db, album_id=album_id)
|
|
if not album:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Album not found",
|
|
)
|
|
return album
|
|
|
|
|
|
@router.put("/{album_id}", response_model=Album)
|
|
def update_album_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
album_id: int,
|
|
album_in: AlbumUpdate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update an album. Only superusers can update albums.
|
|
"""
|
|
album = get_album(db, album_id=album_id)
|
|
if not album:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Album not found",
|
|
)
|
|
|
|
# If updating artist_id, check if artist exists
|
|
if album_in.artist_id is not None and album_in.artist_id != album.artist_id:
|
|
artist = get_artist(db, artist_id=album_in.artist_id)
|
|
if not artist:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Artist not found",
|
|
)
|
|
|
|
album = update_album(db, db_obj=album, obj_in=album_in)
|
|
return album
|
|
|
|
|
|
@router.delete("/{album_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_album_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
album_id: int,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete an album. Only superusers can delete albums.
|
|
"""
|
|
album = get_album(db, album_id=album_id)
|
|
if not album:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Album not found",
|
|
)
|
|
delete_album(db, id=album_id)
|
|
return None |