Automated Action cda1825688 Implement FastAPI inventory management system for small businesses
- Set up FastAPI application with CORS and health check endpoint
- Create SQLite database models for inventory items, categories, and suppliers
- Implement complete CRUD API endpoints for all entities
- Add low-stock monitoring functionality
- Configure Alembic for database migrations
- Set up Ruff for code linting and formatting
- Include comprehensive API documentation and README
2025-06-18 16:50:29 +00:00

65 lines
2.4 KiB
Python

from typing import List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.db.models import InventoryItem
from app.db.session import get_db
from app.schemas.inventory import InventoryItem as InventoryItemSchema
from app.schemas.inventory import InventoryItemCreate, InventoryItemUpdate
router = APIRouter()
@router.get("/", response_model=List[InventoryItemSchema])
def get_inventory_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
items = db.query(InventoryItem).offset(skip).limit(limit).all()
return items
@router.get("/{item_id}", response_model=InventoryItemSchema)
def get_inventory_item(item_id: int, db: Session = Depends(get_db)):
item = db.query(InventoryItem).filter(InventoryItem.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
@router.post("/", response_model=InventoryItemSchema)
def create_inventory_item(item: InventoryItemCreate, db: Session = Depends(get_db)):
existing_item = db.query(InventoryItem).filter(InventoryItem.sku == item.sku).first()
if existing_item:
raise HTTPException(status_code=400, detail="SKU already exists")
db_item = InventoryItem(**item.dict())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
@router.put("/{item_id}", response_model=InventoryItemSchema)
def update_inventory_item(item_id: int, item: InventoryItemUpdate, db: Session = Depends(get_db)):
db_item = db.query(InventoryItem).filter(InventoryItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Item not found")
update_data = item.dict(exclude_unset=True)
for field, value in update_data.items():
setattr(db_item, field, value)
db.commit()
db.refresh(db_item)
return db_item
@router.delete("/{item_id}")
def delete_inventory_item(item_id: int, db: Session = Depends(get_db)):
db_item = db.query(InventoryItem).filter(InventoryItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Item not found")
db.delete(db_item)
db.commit()
return {"message": "Item deleted successfully"}
@router.get("/low-stock/", response_model=List[InventoryItemSchema])
def get_low_stock_items(db: Session = Depends(get_db)):
items = db.query(InventoryItem).filter(InventoryItem.quantity <= InventoryItem.minimum_stock).all()
return items