Automated Action a9210ca8ed Create manga inventory API with FastAPI and SQLite
- Implemented CRUD operations for manga, authors, publishers, and genres
- Added search and filtering functionality
- Set up SQLAlchemy ORM with SQLite database
- Configured Alembic for database migrations
- Implemented logging with Loguru
- Added comprehensive API documentation
- Set up error handling and validation
- Added ruff for linting and formatting
2025-05-30 12:29:35 +00:00

117 lines
2.9 KiB
Python

from typing import Any
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app import crud, schemas
from app.api import deps
router = APIRouter()
@router.get("/", response_model=list[schemas.Author])
def read_authors(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve authors.
"""
authors = crud.author.get_multi(db, skip=skip, limit=limit)
return authors
@router.post("/", response_model=schemas.Author)
def create_author(
*,
db: Session = Depends(deps.get_db),
author_in: schemas.AuthorCreate,
) -> Any:
"""
Create new author.
"""
author = crud.author.get_by_name(db, name=author_in.name)
if author:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="An author with this name already exists.",
)
author = crud.author.create(db, obj_in=author_in)
return author
@router.get("/{author_id}", response_model=schemas.Author)
def read_author(
*,
db: Session = Depends(deps.get_db),
author_id: int,
) -> Any:
"""
Get author by ID.
"""
author = crud.author.get(db, id=author_id)
if not author:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Author not found",
)
return author
@router.put("/{author_id}", response_model=schemas.Author)
def update_author(
*,
db: Session = Depends(deps.get_db),
author_id: int,
author_in: schemas.AuthorUpdate,
) -> Any:
"""
Update an author.
"""
author = crud.author.get(db, id=author_id)
if not author:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Author not found",
)
# Check if updating to an existing name
if author_in.name and author_in.name != author.name:
existing_author = crud.author.get_by_name(db, name=author_in.name)
if existing_author:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="An author with this name already exists.",
)
author = crud.author.update(db, db_obj=author, obj_in=author_in)
return author
@router.delete("/{author_id}", response_model=schemas.Author)
def delete_author(
*,
db: Session = Depends(deps.get_db),
author_id: int,
) -> Any:
"""
Delete an author.
"""
author = crud.author.get(db, id=author_id)
if not author:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Author not found",
)
# Check if author has manga associated with it
if author.manga_list:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Cannot delete author with associated manga. Remove manga first.",
)
author = crud.author.remove(db, id=author_id)
return author