Automated Action 700da98f88 Implement small business inventory management system with FastAPI and SQLite
- Created complete RESTful API for inventory management
- Set up database models for items, categories, suppliers, and transactions
- Implemented user authentication with JWT tokens
- Added transaction tracking for inventory movements
- Created comprehensive API endpoints for all CRUD operations
- Set up Alembic for database migrations
- Added input validation and error handling
- Created detailed documentation in README
2025-06-08 10:00:50 +00:00

199 lines
5.7 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app import crud, models, schemas
from app.api import deps
router = APIRouter()
@router.get("/", response_model=List[schemas.Item])
def read_items(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Retrieve items.
"""
items = crud.item.get_multi(db, skip=skip, limit=limit)
return items
@router.post("/", response_model=schemas.Item)
def create_item(
*,
db: Session = Depends(deps.get_db),
item_in: schemas.ItemCreate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Create new item.
"""
# Check if SKU already exists
item = crud.item.get_by_sku(db, sku=item_in.sku)
if item:
raise HTTPException(
status_code=400,
detail="An item with this SKU already exists."
)
# Validate category_id if provided
if item_in.category_id:
category = crud.category.get(db, id=item_in.category_id)
if not category:
raise HTTPException(
status_code=404,
detail=f"Category with ID {item_in.category_id} not found"
)
# Validate supplier_id if provided
if item_in.supplier_id:
supplier = crud.supplier.get(db, id=item_in.supplier_id)
if not supplier:
raise HTTPException(
status_code=404,
detail=f"Supplier with ID {item_in.supplier_id} not found"
)
item = crud.item.create(db, obj_in=item_in)
return item
@router.put("/{id}", response_model=schemas.Item)
def update_item(
*,
db: Session = Depends(deps.get_db),
id: str,
item_in: schemas.ItemUpdate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update an item.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
# Check if updating to an existing SKU
if item_in.sku and item_in.sku != item.sku:
existing_item = crud.item.get_by_sku(db, sku=item_in.sku)
if existing_item:
raise HTTPException(
status_code=400,
detail="An item with this SKU already exists."
)
# Validate category_id if provided
if item_in.category_id and item_in.category_id != item.category_id:
category = crud.category.get(db, id=item_in.category_id)
if not category:
raise HTTPException(
status_code=404,
detail=f"Category with ID {item_in.category_id} not found"
)
# Validate supplier_id if provided
if item_in.supplier_id and item_in.supplier_id != item.supplier_id:
supplier = crud.supplier.get(db, id=item_in.supplier_id)
if not supplier:
raise HTTPException(
status_code=404,
detail=f"Supplier with ID {item_in.supplier_id} not found"
)
item = crud.item.update(db, db_obj=item, obj_in=item_in)
return item
@router.get("/{id}", response_model=schemas.Item)
def read_item(
*,
db: Session = Depends(deps.get_db),
id: str,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get item by ID.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
@router.delete("/{id}", response_model=schemas.Item)
def delete_item(
*,
db: Session = Depends(deps.get_db),
id: str,
current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Delete an item.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
item = crud.item.remove(db, id=id)
return item
@router.get("/by-category/{category_id}", response_model=List[schemas.Item])
def read_items_by_category(
*,
db: Session = Depends(deps.get_db),
category_id: str,
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get items by category.
"""
# Validate category exists
category = crud.category.get(db, id=category_id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
items = crud.item.get_by_category(db, category_id=category_id, skip=skip, limit=limit)
return items
@router.get("/by-supplier/{supplier_id}", response_model=List[schemas.Item])
def read_items_by_supplier(
*,
db: Session = Depends(deps.get_db),
supplier_id: str,
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get items by supplier.
"""
# Validate supplier exists
supplier = crud.supplier.get(db, id=supplier_id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
items = crud.item.get_by_supplier(db, supplier_id=supplier_id, skip=skip, limit=limit)
return items
@router.get("/low-stock/", response_model=List[schemas.Item])
def read_low_stock_items(
*,
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get all items with stock levels at or below their minimum threshold.
"""
items = crud.item.get_low_stock_items(db, skip=skip, limit=limit)
return items