
This commit includes: - Project structure setup with FastAPI and SQLite - Database models and schemas for inventory management - CRUD operations for all entities - API endpoints for product, category, supplier, and inventory management - User authentication with JWT tokens - Initial database migration - Comprehensive README with setup instructions
96 lines
2.5 KiB
Python
96 lines
2.5 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.create(db, obj_in=supplier_in)
|
|
return supplier
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.Supplier)
|
|
def read_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
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.put("/{id}", response_model=schemas.Supplier)
|
|
def update_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
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=id)
|
|
if not supplier:
|
|
raise HTTPException(status_code=404, detail="Supplier not found")
|
|
supplier = crud.supplier.update(db, db_obj=supplier, obj_in=supplier_in)
|
|
return supplier
|
|
|
|
|
|
@router.delete("/{id}", status_code=204, response_model=None)
|
|
def delete_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> 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 the supplier has products
|
|
if supplier.products:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Cannot delete supplier with associated products. Reassign or delete the products first.",
|
|
)
|
|
|
|
crud.supplier.remove(db, id=id)
|
|
return None |