Automated Action 4aac37bc90 Implement inventory management system with FastAPI and SQLite
- Setup project structure with FastAPI application
- Create database models with SQLAlchemy
- Configure Alembic for database migrations
- Implement CRUD operations for products, categories, suppliers
- Add inventory transaction functionality
- Implement user authentication with JWT
- Add health check endpoint
- Create comprehensive documentation
2025-06-05 11:43:07 +00:00

112 lines
3.4 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.
"""
# Check if supplier with the same name already exists
supplier = crud.supplier.get_by_name(db, name=supplier_in.name)
if supplier:
raise HTTPException(
status_code=400,
detail="The supplier with this name already exists in the system.",
)
supplier = crud.supplier.create(db, obj_in=supplier_in)
return supplier
@router.get("/{supplier_id}", response_model=schemas.Supplier)
def read_supplier(
*,
db: Session = Depends(deps.get_db),
supplier_id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get supplier by ID.
"""
supplier = crud.supplier.get(db, id=supplier_id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
return supplier
@router.put("/{supplier_id}", response_model=schemas.Supplier)
def update_supplier(
*,
db: Session = Depends(deps.get_db),
supplier_id: int,
supplier_in: schemas.SupplierUpdate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a supplier.
"""
supplier = crud.supplier.get(db, id=supplier_id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
# If name is being updated, check for duplicates
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="The supplier with this name already exists in the system.",
)
supplier = crud.supplier.update(db, db_obj=supplier, obj_in=supplier_in)
return supplier
@router.delete("/{supplier_id}", response_model=schemas.Supplier)
def delete_supplier(
*,
db: Session = Depends(deps.get_db),
supplier_id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Delete a supplier.
"""
supplier = crud.supplier.get(db, id=supplier_id)
if not supplier:
raise HTTPException(status_code=404, detail="Supplier not found")
# Check if any products are using this supplier
products = crud.product.get_by_supplier(db, supplier_id=supplier_id)
if products:
raise HTTPException(
status_code=400,
detail="Cannot delete supplier because it has associated products. Update or delete those products first.",
)
supplier = crud.supplier.remove(db, id=supplier_id)
return supplier