
- Create project structure and dependencies - Set up SQLAlchemy models for anime, genres, and their relationships - Implement CRUD operations for all models - Set up FastAPI endpoints for managing anime and genres - Add health check endpoint - Configure Alembic for database migrations - Add data seeding capability - Update README with project information
92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
from typing import Any, Optional
|
|
|
|
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=schemas.anime.AnimeSearchResults)
|
|
def search_anime(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
title: Optional[str] = None,
|
|
genre_id: Optional[int] = None,
|
|
status: Optional[str] = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Search for anime with filters.
|
|
"""
|
|
anime = crud.anime.search(db, title=title, genre_id=genre_id, status=status, skip=skip, limit=limit)
|
|
total = crud.anime.search_count(db, title=title, genre_id=genre_id, status=status)
|
|
return {
|
|
"results": anime,
|
|
"total": total,
|
|
"page": skip // limit + 1 if limit > 0 else 1,
|
|
"size": limit
|
|
}
|
|
|
|
|
|
@router.post("/", response_model=schemas.anime.Anime)
|
|
def create_anime(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
anime_in: schemas.anime.AnimeCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new anime.
|
|
"""
|
|
return crud.anime.create_with_genres(db=db, obj_in=anime_in)
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.anime.Anime)
|
|
def read_anime(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get anime by ID.
|
|
"""
|
|
anime = crud.anime.get(db=db, id=id)
|
|
if not anime:
|
|
raise HTTPException(status_code=404, detail="Anime not found")
|
|
return anime
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.anime.Anime)
|
|
def update_anime(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
anime_in: schemas.anime.AnimeUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update an anime.
|
|
"""
|
|
anime = crud.anime.get(db=db, id=id)
|
|
if not anime:
|
|
raise HTTPException(status_code=404, detail="Anime not found")
|
|
anime = crud.anime.update_with_genres(db=db, db_obj=anime, obj_in=anime_in)
|
|
return anime
|
|
|
|
|
|
@router.delete("/{id}", response_model=None, status_code=204)
|
|
def delete_anime(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete an anime.
|
|
"""
|
|
anime = crud.anime.get(db=db, id=id)
|
|
if not anime:
|
|
raise HTTPException(status_code=404, detail="Anime not found")
|
|
crud.anime.remove(db=db, id=id)
|
|
return None |