from typing import Any, List, Optional from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps from app.core.config import settings from app.utils.file_storage import save_upload_file, validate_image_file router = APIRouter() @router.get("/", response_model=List[schemas.Artist]) def read_artists( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, name: Optional[str] = Query(None, description="Filter artists by name"), ) -> Any: """ Retrieve artists. """ if name: artists = crud.artist.search_by_name(db, name=name, skip=skip, limit=limit) else: artists = crud.artist.get_multi(db, skip=skip, limit=limit) return artists @router.post("/", response_model=schemas.Artist) def create_artist( *, db: Session = Depends(deps.get_db), artist_in: schemas.ArtistCreate, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Create new artist. """ artist = crud.artist.create(db, obj_in=artist_in) return artist @router.post("/{artist_id}/image", response_model=schemas.Artist) async def upload_artist_image( *, db: Session = Depends(deps.get_db), artist_id: str, file: UploadFile = File(...), current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Upload an image for an artist. """ artist = crud.artist.get(db, id=artist_id) if not artist: raise HTTPException( status_code=404, detail="Artist not found", ) validate_image_file(file) # Save the artist image filename = save_upload_file( file, settings.ARTIST_IMAGES_DIR, file_id=artist_id, ) # Update the artist's image field artist_in = schemas.ArtistUpdate(image=filename) artist = crud.artist.update(db, db_obj=artist, obj_in=artist_in) return artist @router.get("/{artist_id}", response_model=schemas.Artist) def read_artist( *, db: Session = Depends(deps.get_db), artist_id: str, ) -> Any: """ Get artist by ID. """ artist = crud.artist.get(db, id=artist_id) if not artist: raise HTTPException( status_code=404, detail="Artist not found", ) return artist @router.put("/{artist_id}", response_model=schemas.Artist) def update_artist( *, db: Session = Depends(deps.get_db), artist_id: str, artist_in: schemas.ArtistUpdate, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Update an artist. """ artist = crud.artist.get(db, id=artist_id) if not artist: raise HTTPException( status_code=404, detail="Artist not found", ) artist = crud.artist.update(db, db_obj=artist, obj_in=artist_in) return artist @router.delete("/{artist_id}", response_model=schemas.Artist) def delete_artist( *, db: Session = Depends(deps.get_db), artist_id: str, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Delete an artist. """ artist = crud.artist.get(db, id=artist_id) if not artist: raise HTTPException( status_code=404, detail="Artist not found", ) artist = crud.artist.remove(db, id=artist_id) return artist