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

113 lines
3.2 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.Supplier])
def read_suppliers(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Retrieve suppliers.
"""
suppliers = crud.supplier.get_multi(db, skip=skip, limit=limit)
return suppliers
@router.post("/", response_model=schemas.Supplier)
def create_supplier(
*,
db: Session = Depends(deps.get_db),
supplier_in: schemas.SupplierCreate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Create new supplier.
"""
supplier = crud.supplier.get_by_name(db, name=supplier_in.name)
if supplier:
raise HTTPException(
status_code=400,
detail="A supplier with this name already exists.",
)
supplier = crud.supplier.create(db, obj_in=supplier_in)
return supplier
@router.put("/{id}", response_model=schemas.Supplier)
def update_supplier(
*,
db: Session = Depends(deps.get_db),
id: str,
supplier_in: schemas.SupplierUpdate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a supplier.
"""
supplier = crud.supplier.get(db, id=id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
# Check if updating to an existing name
if supplier_in.name and supplier_in.name != supplier.name:
existing_supplier = crud.supplier.get_by_name(db, name=supplier_in.name)
if existing_supplier:
raise HTTPException(
status_code=400,
detail="A supplier with this name already exists.",
)
supplier = crud.supplier.update(db, db_obj=supplier, obj_in=supplier_in)
return supplier
@router.get("/{id}", response_model=schemas.Supplier)
def read_supplier(
*,
db: Session = Depends(deps.get_db),
id: str,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get supplier by ID.
"""
supplier = crud.supplier.get(db, id=id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
return supplier
@router.delete("/{id}", response_model=schemas.Supplier)
def delete_supplier(
*,
db: Session = Depends(deps.get_db),
id: str,
current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Delete a supplier.
"""
supplier = crud.supplier.get(db, id=id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
# Check if any items are using this supplier
items = crud.item.get_by_supplier(db, supplier_id=id)
if items:
raise HTTPException(
status_code=400,
detail="Cannot delete supplier that is in use by items. Update or delete those items first.",
)
supplier = crud.supplier.remove(db, id=id)
return supplier