
- Set up project structure with app modules - Configure SQLite database connection - Set up Alembic for database migrations - Implement Item model with CRUD operations - Create API endpoints for items management - Add health check endpoint - Add API documentation - Add comprehensive README
140 lines
3.0 KiB
Python
140 lines
3.0 KiB
Python
"""
|
|
API endpoints for items.
|
|
"""
|
|
from typing import List, Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db
|
|
from app.schemas.item import Item as ItemSchema
|
|
from app.schemas.item import ItemCreate, ItemUpdate
|
|
from app.services.crud import item
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[ItemSchema])
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
active_only: bool = False,
|
|
) -> Any:
|
|
"""
|
|
Retrieve items.
|
|
|
|
Args:
|
|
db: Database session
|
|
skip: Number of items to skip
|
|
limit: Maximum number of items to return
|
|
active_only: Only return active items
|
|
|
|
Returns:
|
|
List of items
|
|
"""
|
|
if active_only:
|
|
items = item.get_multi_by_active(db, skip=skip, limit=limit)
|
|
else:
|
|
items = item.get_multi(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.post("/", response_model=ItemSchema, status_code=status.HTTP_201_CREATED)
|
|
def create_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_in: ItemCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new item.
|
|
|
|
Args:
|
|
db: Database session
|
|
item_in: Item to create
|
|
|
|
Returns:
|
|
Created item
|
|
"""
|
|
db_item = item.get_by_title(db, title=item_in.title)
|
|
if db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Item with this title already exists",
|
|
)
|
|
return item.create(db, obj_in=item_in)
|
|
|
|
|
|
@router.get("/{id}", response_model=ItemSchema)
|
|
def read_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get item by ID.
|
|
|
|
Args:
|
|
db: Database session
|
|
id: Item ID
|
|
|
|
Returns:
|
|
Item
|
|
"""
|
|
db_item = item.get(db, id=id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return db_item
|
|
|
|
|
|
@router.put("/{id}", response_model=ItemSchema)
|
|
def update_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
item_in: ItemUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
|
|
Args:
|
|
db: Database session
|
|
id: Item ID
|
|
item_in: Item data to update
|
|
|
|
Returns:
|
|
Updated item
|
|
"""
|
|
db_item = item.get(db, id=id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return item.update(db, db_obj=db_item, obj_in=item_in)
|
|
|
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> None:
|
|
"""
|
|
Delete an item.
|
|
|
|
Args:
|
|
db: Database session
|
|
id: Item ID
|
|
"""
|
|
db_item = item.get(db, id=id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
item.remove(db, id=id)
|
|
return None |