Automated Action e8172f2bc2 Implement complete FastAPI inventory management system
- Set up project structure with FastAPI and SQLite
- Created database models for users, categories, suppliers, items, and stock transactions
- Implemented Alembic for database migrations with proper absolute paths
- Built comprehensive CRUD operations for all entities
- Added JWT-based authentication and authorization system
- Created RESTful API endpoints for all inventory operations
- Implemented search, filtering, and low stock alerts
- Added health check endpoint and base URL response
- Configured CORS for all origins
- Set up Ruff for code linting and formatting
- Updated README with comprehensive documentation and usage examples

The system provides complete inventory management functionality for small businesses
including product tracking, supplier management, stock transactions, and reporting.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-18 10:55:22 +00:00

122 lines
3.4 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from app.core.auth import get_current_active_user
from app.crud import item as item_crud
from app.db.session import get_db
from app.models import User
from app.schemas.item import Item, ItemCreate, ItemUpdate, ItemWithLowStock
router = APIRouter()
@router.get("/", response_model=List[Item])
def read_items(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_active_user),
) -> Any:
items = item_crud.get_multi(db, skip=skip, limit=limit)
return items
@router.get("/search", response_model=List[Item])
def search_items(
query: str = Query(..., description="Search query"),
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_active_user),
) -> Any:
items = item_crud.search_items(db, query=query, skip=skip, limit=limit)
return items
@router.get("/low-stock", response_model=List[ItemWithLowStock])
def read_low_stock_items(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_active_user),
) -> Any:
items = item_crud.get_low_stock_items(db, skip=skip, limit=limit)
return items
@router.get("/active", response_model=List[Item])
def read_active_items(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_active_user),
) -> Any:
items = item_crud.get_active_items(db, skip=skip, limit=limit)
return items
@router.post("/", response_model=Item)
def create_item(
*,
db: Session = Depends(get_db),
item_in: ItemCreate,
current_user: User = Depends(get_current_active_user),
) -> Any:
item = item_crud.get_by_sku(db, sku=item_in.sku)
if item:
raise HTTPException(
status_code=400,
detail="Item with this SKU already exists.",
)
if item_in.barcode:
item = item_crud.get_by_barcode(db, barcode=item_in.barcode)
if item:
raise HTTPException(
status_code=400,
detail="Item with this barcode already exists.",
)
item = item_crud.create(db, obj_in=item_in)
return item
@router.put("/{id}", response_model=Item)
def update_item(
*,
db: Session = Depends(get_db),
id: int,
item_in: ItemUpdate,
current_user: User = Depends(get_current_active_user),
) -> Any:
item = item_crud.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
item = item_crud.update(db, db_obj=item, obj_in=item_in)
return item
@router.get("/{id}", response_model=Item)
def read_item(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
item = item_crud.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
@router.delete("/{id}", response_model=Item)
def delete_item(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
item = item_crud.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
item = item_crud.remove(db, id=id)
return item