
- Setup FastAPI project structure with main.py and requirements.txt - Implement SQLAlchemy ORM with SQLite database - Create Item model with CRUD operations - Implement health endpoint for monitoring - Setup Alembic for database migrations - Add comprehensive documentation in README.md - Configure Ruff for code linting
136 lines
3.0 KiB
Python
136 lines
3.0 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, schemas
|
|
from app.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/items/", response_model=List[schemas.Item])
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
active_only: bool = False,
|
|
) -> Any:
|
|
"""
|
|
Retrieve items.
|
|
|
|
Args:
|
|
skip: Number of items to skip
|
|
limit: Maximum number of items to return
|
|
active_only: If True, only return active items
|
|
|
|
Returns:
|
|
List of items
|
|
"""
|
|
if active_only:
|
|
items = crud.item.get_multi_active(db, skip=skip, limit=limit)
|
|
else:
|
|
items = crud.item.get_multi(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.post("/items/", response_model=schemas.Item, status_code=status.HTTP_201_CREATED)
|
|
def create_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_in: schemas.ItemCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new item.
|
|
|
|
Args:
|
|
item_in: Item data
|
|
|
|
Returns:
|
|
Created item
|
|
"""
|
|
item = crud.item.get_by_title(db, title=item_in.title)
|
|
if item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="An item with this title already exists.",
|
|
)
|
|
item = crud.item.create(db, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.get("/items/{item_id}", response_model=schemas.Item)
|
|
def read_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get item by ID.
|
|
|
|
Args:
|
|
item_id: ID of item to get
|
|
|
|
Returns:
|
|
Item information
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return item
|
|
|
|
|
|
@router.put("/items/{item_id}", response_model=schemas.Item)
|
|
def update_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
item_in: schemas.ItemUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
|
|
Args:
|
|
item_id: ID of item to update
|
|
item_in: New item data
|
|
|
|
Returns:
|
|
Updated item
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
item = crud.item.update(db=db, db_obj=item, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.delete("/items/{item_id}", response_model=schemas.Item)
|
|
def delete_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete an item.
|
|
|
|
Args:
|
|
item_id: ID of item to delete
|
|
|
|
Returns:
|
|
Deleted item
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
item = crud.item.remove(db=db, id=item_id)
|
|
return item
|