Automated Action 252ce19872 Implement complete small business inventory management system
Features include:
- User management with JWT authentication and role-based access
- Inventory items with SKU/barcode tracking and stock management
- Categories and suppliers organization
- Inventory transactions with automatic stock updates
- Low stock alerts and advanced search/filtering
- RESTful API with comprehensive CRUD operations
- SQLite database with Alembic migrations
- Auto-generated API documentation

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-21 16:10:33 +00:00

138 lines
4.9 KiB
Python

from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.crud import inventory_item
from app.schemas.inventory_item import InventoryItem, InventoryItemCreate, InventoryItemUpdate
from app.auth.auth_handler import get_current_active_user
from app.models.user import User
router = APIRouter()
@router.post("/", response_model=InventoryItem)
def create_inventory_item(
*,
db: Session = Depends(get_db),
item_in: InventoryItemCreate,
current_user: User = Depends(get_current_active_user)
):
db_item = inventory_item.get_by_sku(db, sku=item_in.sku)
if db_item:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Item with this SKU already exists"
)
if item_in.barcode:
db_item_by_barcode = inventory_item.get_by_barcode(db, barcode=item_in.barcode)
if db_item_by_barcode:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Item with this barcode already exists"
)
return inventory_item.create(db, obj_in=item_in)
@router.get("/", response_model=List[InventoryItem])
def read_inventory_items(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
search: Optional[str] = Query(None, description="Search by item name"),
category_id: Optional[int] = Query(None, description="Filter by category ID"),
supplier_id: Optional[int] = Query(None, description="Filter by supplier ID"),
low_stock_only: bool = Query(False, description="Show only low stock items"),
current_user: User = Depends(get_current_active_user)
):
if low_stock_only:
return inventory_item.get_low_stock_items(db)
elif search:
return inventory_item.search_by_name(db, name=search, skip=skip, limit=limit)
elif category_id:
return inventory_item.get_by_category(db, category_id=category_id, skip=skip, limit=limit)
elif supplier_id:
return inventory_item.get_by_supplier(db, supplier_id=supplier_id, skip=skip, limit=limit)
else:
return inventory_item.get_multi(db, skip=skip, limit=limit)
@router.get("/low-stock", response_model=List[InventoryItem])
def get_low_stock_items(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
return inventory_item.get_low_stock_items(db)
@router.get("/{item_id}", response_model=InventoryItem)
def read_inventory_item(
item_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
db_item = inventory_item.get(db, id=item_id)
if not db_item:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Inventory item not found"
)
return db_item
@router.get("/sku/{sku}", response_model=InventoryItem)
def read_inventory_item_by_sku(
sku: str,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
db_item = inventory_item.get_by_sku(db, sku=sku)
if not db_item:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Inventory item not found"
)
return db_item
@router.put("/{item_id}", response_model=InventoryItem)
def update_inventory_item(
item_id: int,
item_in: InventoryItemUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
db_item = inventory_item.get(db, id=item_id)
if not db_item:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Inventory item not found"
)
if item_in.sku and item_in.sku != db_item.sku:
existing_item = inventory_item.get_by_sku(db, sku=item_in.sku)
if existing_item:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Item with this SKU already exists"
)
if item_in.barcode and item_in.barcode != db_item.barcode:
existing_item = inventory_item.get_by_barcode(db, barcode=item_in.barcode)
if existing_item:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Item with this barcode already exists"
)
return inventory_item.update(db, db_obj=db_item, obj_in=item_in)
@router.delete("/{item_id}")
def delete_inventory_item(
item_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
db_item = inventory_item.get(db, id=item_id)
if not db_item:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Inventory item not found"
)
inventory_item.remove(db, id=item_id)
return {"message": "Inventory item deleted successfully"}