
- Set up project structure - Add database models and SQLAlchemy setup - Configure Alembic for migrations - Create API endpoints for items - Add health check endpoint - Update documentation generated with BackendIM... (backend.im) Co-Authored-By: Claude <noreply@anthropic.com>
93 lines
1.9 KiB
Python
93 lines
1.9 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.database.session import get_db
|
|
from app.schemas.item import Item, ItemCreate, ItemUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[Item])
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve items.
|
|
"""
|
|
items = crud.get_multi(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.post("", response_model=Item)
|
|
def create_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_in: ItemCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new item.
|
|
"""
|
|
item = crud.create(db=db, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.get("/{item_id}", response_model=Item)
|
|
def read_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get item by ID.
|
|
"""
|
|
item = crud.get(db=db, item_id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Item not found",
|
|
)
|
|
return item
|
|
|
|
|
|
@router.put("/{item_id}", response_model=Item)
|
|
def update_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
item_in: ItemUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
"""
|
|
item = crud.get(db=db, item_id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Item not found",
|
|
)
|
|
item = crud.update(db=db, db_obj=item, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.delete("/{item_id}", response_model=Item)
|
|
def delete_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete an item.
|
|
"""
|
|
item = crud.get(db=db, item_id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Item not found",
|
|
)
|
|
item = crud.remove(db=db, item_id=item_id)
|
|
return item |