Automated Action be0ae5f3b3 Implement small business inventory management system
- Create project structure with FastAPI and SQLAlchemy
- Implement database models for items, categories, suppliers, and stock movements
- Add CRUD operations for all models
- Configure Alembic for database migrations
- Create RESTful API endpoints for inventory management
- Add health endpoint for system monitoring
- Update README with setup and usage instructions

generated with BackendIM... (backend.im)
2025-05-12 16:23:23 +00:00

72 lines
2.4 KiB
Python

from sqlalchemy.orm import Session
from fastapi import HTTPException, status
from app.models.item import Item
from app.schemas.item import ItemCreate, ItemUpdate
def get_items(db: Session, skip: int = 0, limit: int = 100, category_id: int = None,
supplier_id: int = None, include_inactive: bool = False):
query = db.query(Item)
if category_id:
query = query.filter(Item.category_id == category_id)
if supplier_id:
query = query.filter(Item.supplier_id == supplier_id)
if not include_inactive:
query = query.filter(Item.is_active == True)
return query.offset(skip).limit(limit).all()
def get_item(db: Session, item_id: int):
return db.query(Item).filter(Item.id == item_id).first()
def get_item_by_sku(db: Session, sku: str):
return db.query(Item).filter(Item.sku == sku).first()
def create_item(db: Session, item: ItemCreate):
existing_item = get_item_by_sku(db, item.sku)
if existing_item:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Item with this SKU already exists")
db_item = Item(**item.model_dump())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
def update_item(db: Session, item_id: int, item: ItemUpdate):
db_item = get_item(db, item_id)
if not db_item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")
update_data = item.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(db_item, key, value)
db.commit()
db.refresh(db_item)
return db_item
def delete_item(db: Session, item_id: int):
db_item = get_item(db, item_id)
if not db_item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")
db_item.is_active = False
db.commit()
return {"message": "Item deleted successfully"}
def update_item_quantity(db: Session, item_id: int, quantity_change: int):
db_item = get_item(db, item_id)
if not db_item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")
new_quantity = db_item.quantity + quantity_change
if new_quantity < 0:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Insufficient stock")
db_item.quantity = new_quantity
db.commit()
db.refresh(db_item)
return db_item