
Features include: - User management with JWT authentication and role-based access - Inventory items with SKU/barcode tracking and stock management - Categories and suppliers organization - Inventory transactions with automatic stock updates - Low stock alerts and advanced search/filtering - RESTful API with comprehensive CRUD operations - SQLite database with Alembic migrations - Auto-generated API documentation Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.db.session import get_db
|
|
from app.crud import supplier
|
|
from app.schemas.supplier import Supplier, SupplierCreate, SupplierUpdate
|
|
from app.auth.auth_handler import get_current_active_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=Supplier)
|
|
def create_supplier(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
supplier_in: SupplierCreate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_supplier = supplier.get_by_name(db, name=supplier_in.name)
|
|
if db_supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Supplier with this name already exists"
|
|
)
|
|
return supplier.create(db, obj_in=supplier_in)
|
|
|
|
@router.get("/", response_model=List[Supplier])
|
|
def read_suppliers(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
return supplier.get_multi(db, skip=skip, limit=limit)
|
|
|
|
@router.get("/{supplier_id}", response_model=Supplier)
|
|
def read_supplier(
|
|
supplier_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_supplier = supplier.get(db, id=supplier_id)
|
|
if not db_supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found"
|
|
)
|
|
return db_supplier
|
|
|
|
@router.put("/{supplier_id}", response_model=Supplier)
|
|
def update_supplier(
|
|
supplier_id: int,
|
|
supplier_in: SupplierUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_supplier = supplier.get(db, id=supplier_id)
|
|
if not db_supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found"
|
|
)
|
|
return supplier.update(db, db_obj=db_supplier, obj_in=supplier_in)
|
|
|
|
@router.delete("/{supplier_id}")
|
|
def delete_supplier(
|
|
supplier_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_supplier = supplier.get(db, id=supplier_id)
|
|
if not db_supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found"
|
|
)
|
|
supplier.remove(db, id=supplier_id)
|
|
return {"message": "Supplier deleted successfully"} |