from typing import Any, Optional from fastapi import APIRouter, Depends, HTTPException, Query, Path from sqlalchemy.orm import Session from app.api import crud from app.api.schemas.actor import ( Actor, ActorCreate, ActorUpdate, ActorDetails, ActorList ) from app.db.session import get_db router = APIRouter(prefix="/actors") @router.get("", response_model=ActorList) def read_actors( db: Session = Depends(get_db), skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=100), name: Optional[str] = None, movie_id: Optional[int] = None, ) -> Any: """ Retrieve all actors with optional filtering. """ # Build filter dict from query parameters filters = {} if name: filters["name"] = name if movie_id: filters["movie_id"] = movie_id actors, total = crud.actor.get_multi(db, skip=skip, limit=limit, filters=filters) return {"data": actors, "total": total} @router.post("", response_model=Actor, status_code=201) def create_actor( *, db: Session = Depends(get_db), actor_in: ActorCreate, ) -> Any: """ Create a new actor. """ actor = crud.actor.create(db, actor_in=actor_in) return actor @router.get("/{actor_id}", response_model=ActorDetails) def read_actor( *, db: Session = Depends(get_db), actor_id: int = Path(..., ge=1), ) -> Any: """ Get a specific actor by ID. """ actor = crud.actor.get(db, actor_id=actor_id) if not actor: raise HTTPException(status_code=404, detail="Actor not found") return actor @router.put("/{actor_id}", response_model=Actor) def update_actor( *, db: Session = Depends(get_db), actor_id: int = Path(..., ge=1), actor_in: ActorUpdate, ) -> Any: """ Update an actor. """ actor = crud.actor.get(db, actor_id=actor_id) if not actor: raise HTTPException(status_code=404, detail="Actor not found") actor = crud.actor.update(db, db_actor=actor, actor_in=actor_in) return actor @router.delete("/{actor_id}", status_code=204, response_model=None) def delete_actor( *, db: Session = Depends(get_db), actor_id: int = Path(..., ge=1), ) -> Any: """ Delete an actor. """ success = crud.actor.delete(db, actor_id=actor_id) if not success: raise HTTPException(status_code=404, detail="Actor not found") return None