
- 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
117 lines
3.1 KiB
Python
117 lines
3.1 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.Publisher])
|
|
def read_publishers(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve publishers.
|
|
"""
|
|
publishers = crud.publisher.get_multi(db, skip=skip, limit=limit)
|
|
return publishers
|
|
|
|
|
|
@router.post("/", response_model=schemas.Publisher)
|
|
def create_publisher(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
publisher_in: schemas.PublisherCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new publisher.
|
|
"""
|
|
publisher = crud.publisher.get_by_name(db, name=publisher_in.name)
|
|
if publisher:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A publisher with this name already exists.",
|
|
)
|
|
publisher = crud.publisher.create(db, obj_in=publisher_in)
|
|
return publisher
|
|
|
|
|
|
@router.get("/{publisher_id}", response_model=schemas.Publisher)
|
|
def read_publisher(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
publisher_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get publisher by ID.
|
|
"""
|
|
publisher = crud.publisher.get(db, id=publisher_id)
|
|
if not publisher:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Publisher not found",
|
|
)
|
|
return publisher
|
|
|
|
|
|
@router.put("/{publisher_id}", response_model=schemas.Publisher)
|
|
def update_publisher(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
publisher_id: int,
|
|
publisher_in: schemas.PublisherUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a publisher.
|
|
"""
|
|
publisher = crud.publisher.get(db, id=publisher_id)
|
|
if not publisher:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Publisher not found",
|
|
)
|
|
|
|
# Check if updating to an existing name
|
|
if publisher_in.name and publisher_in.name != publisher.name:
|
|
existing_publisher = crud.publisher.get_by_name(db, name=publisher_in.name)
|
|
if existing_publisher:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A publisher with this name already exists.",
|
|
)
|
|
|
|
publisher = crud.publisher.update(db, db_obj=publisher, obj_in=publisher_in)
|
|
return publisher
|
|
|
|
|
|
@router.delete("/{publisher_id}", response_model=schemas.Publisher)
|
|
def delete_publisher(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
publisher_id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a publisher.
|
|
"""
|
|
publisher = crud.publisher.get(db, id=publisher_id)
|
|
if not publisher:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Publisher not found",
|
|
)
|
|
|
|
# Check if publisher has manga associated with it
|
|
if publisher.manga_list:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Cannot delete publisher with associated manga. Remove manga first.",
|
|
)
|
|
|
|
publisher = crud.publisher.remove(db, id=publisher_id)
|
|
return publisher
|