
- Add proper model relationships for better querying - Implement character model and endpoints for managing anime characters - Add advanced filtering options (year, score range, source, studio, etc.) - Add statistics endpoint for analyzing anime collection - Include pagination metadata for easier navigation - Create Alembic migration for the new character model - Update README with new features and documentation
114 lines
3.1 KiB
Python
114 lines
3.1 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, schemas
|
|
from app.api import deps
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.character.Character])
|
|
def read_characters(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve characters.
|
|
"""
|
|
return crud.character.get_multi(db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("/", response_model=schemas.character.Character)
|
|
def create_character(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
character_in: schemas.character.CharacterCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new character.
|
|
"""
|
|
# Verify anime exists
|
|
anime = crud.anime.get(db=db, id=character_in.anime_id)
|
|
if not anime:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail=f"Anime with id {character_in.anime_id} not found",
|
|
)
|
|
return crud.character.create(db=db, obj_in=character_in)
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.character.Character)
|
|
def read_character(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get character by ID.
|
|
"""
|
|
character = crud.character.get(db=db, id=id)
|
|
if not character:
|
|
raise HTTPException(status_code=404, detail="Character not found")
|
|
return character
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.character.Character)
|
|
def update_character(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
character_in: schemas.character.CharacterUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a character.
|
|
"""
|
|
character = crud.character.get(db=db, id=id)
|
|
if not character:
|
|
raise HTTPException(status_code=404, detail="Character not found")
|
|
|
|
# If updating anime_id, verify new anime exists
|
|
if character_in.anime_id is not None and character_in.anime_id != character.anime_id:
|
|
anime = crud.anime.get(db=db, id=character_in.anime_id)
|
|
if not anime:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail=f"Anime with id {character_in.anime_id} not found",
|
|
)
|
|
|
|
return crud.character.update(db=db, db_obj=character, obj_in=character_in)
|
|
|
|
|
|
@router.delete("/{id}", response_model=None, status_code=204)
|
|
def delete_character(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a character.
|
|
"""
|
|
character = crud.character.get(db=db, id=id)
|
|
if not character:
|
|
raise HTTPException(status_code=404, detail="Character not found")
|
|
crud.character.remove(db=db, id=id)
|
|
return None
|
|
|
|
|
|
@router.get("/anime/{anime_id}", response_model=List[schemas.character.Character])
|
|
def read_anime_characters(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
anime_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get all characters for a specific anime.
|
|
"""
|
|
# Verify anime exists
|
|
anime = crud.anime.get(db=db, id=anime_id)
|
|
if not anime:
|
|
raise HTTPException(status_code=404, detail=f"Anime with id {anime_id} not found")
|
|
|
|
return crud.character.get_by_anime(db=db, anime_id=anime_id) |