
- Set up project structure - Create FastAPI app with health endpoint - Implement SQLAlchemy with SQLite database - Set up Alembic for database migrations - Create CRUD operations for items - Add comprehensive documentation
134 lines
3.2 KiB
Python
134 lines
3.2 KiB
Python
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.models import models
|
|
from app.schemas.item import Item, ItemCreate, ItemUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
|
|
def create_item(item: ItemCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new item.
|
|
|
|
Args:
|
|
item: The item data to create
|
|
db: Database session
|
|
|
|
Returns:
|
|
The created item
|
|
"""
|
|
db_item = models.Item(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.get("/items/", response_model=List[Item])
|
|
def read_items(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
name: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Retrieve items with optional filtering.
|
|
|
|
Args:
|
|
skip: Number of items to skip
|
|
limit: Maximum number of items to return
|
|
name: Optional filter by item name
|
|
db: Database session
|
|
|
|
Returns:
|
|
List of items
|
|
"""
|
|
query = db.query(models.Item)
|
|
|
|
if name:
|
|
query = query.filter(models.Item.name.ilike(f"%{name}%"))
|
|
|
|
items = query.offset(skip).limit(limit).all()
|
|
return items
|
|
|
|
|
|
@router.get("/items/{item_id}", response_model=Item)
|
|
def read_item(item_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Get item by ID.
|
|
|
|
Args:
|
|
item_id: The ID of the item to retrieve
|
|
db: Database session
|
|
|
|
Returns:
|
|
The requested item
|
|
|
|
Raises:
|
|
HTTPException: If item is not found
|
|
"""
|
|
item = db.query(models.Item).filter(models.Item.id == item_id).first()
|
|
if item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return item
|
|
|
|
|
|
@router.patch("/items/{item_id}", response_model=Item)
|
|
def update_item(item_id: int, item_update: ItemUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update an item.
|
|
|
|
Args:
|
|
item_id: The ID of the item to update
|
|
item_update: The updated item data
|
|
db: Database session
|
|
|
|
Returns:
|
|
The updated item
|
|
|
|
Raises:
|
|
HTTPException: If item is not found
|
|
"""
|
|
db_item = db.query(models.Item).filter(models.Item.id == item_id).first()
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
# Update item attributes that are provided
|
|
update_data = item_update.model_dump(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_item, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.delete(
|
|
"/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None
|
|
)
|
|
def delete_item(item_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete an item.
|
|
|
|
Args:
|
|
item_id: The ID of the item to delete
|
|
db: Database session
|
|
|
|
Returns:
|
|
None
|
|
|
|
Raises:
|
|
HTTPException: If item is not found
|
|
"""
|
|
db_item = db.query(models.Item).filter(models.Item.id == item_id).first()
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
db.delete(db_item)
|
|
db.commit()
|
|
return None
|