
- Created FastAPI application with SQLite database - Implemented models for inventory items, categories, suppliers, and transactions - Added authentication system with JWT tokens - Implemented CRUD operations for all models - Set up Alembic for database migrations - Added comprehensive API documentation - Configured Ruff for code linting
127 lines
3.7 KiB
Python
127 lines
3.7 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
from app.models.user import User
|
|
from app.schemas.supplier import Supplier, SupplierCreate, SupplierUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Supplier])
|
|
def read_suppliers(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve suppliers.
|
|
"""
|
|
suppliers = crud.supplier.get_multi(db, skip=skip, limit=limit)
|
|
return suppliers
|
|
|
|
|
|
@router.post("/", response_model=Supplier, status_code=status.HTTP_201_CREATED)
|
|
def create_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
supplier_in: SupplierCreate,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Create new supplier.
|
|
"""
|
|
# Check if supplier with the same email already exists (if email is provided)
|
|
if supplier_in.email:
|
|
supplier = crud.supplier.get_by_email(db, email=supplier_in.email)
|
|
if supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Supplier with this email already exists",
|
|
)
|
|
|
|
supplier = crud.supplier.create(db, obj_in=supplier_in)
|
|
return supplier
|
|
|
|
|
|
@router.get("/{supplier_id}", response_model=Supplier)
|
|
def read_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
supplier_id: int,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get supplier by ID.
|
|
"""
|
|
supplier = crud.supplier.get(db, id=supplier_id)
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
return supplier
|
|
|
|
|
|
@router.put("/{supplier_id}", response_model=Supplier)
|
|
def update_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
supplier_id: int,
|
|
supplier_in: SupplierUpdate,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Update a supplier.
|
|
"""
|
|
supplier = crud.supplier.get(db, id=supplier_id)
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
|
|
# Check if email is being changed and if it already exists
|
|
if supplier_in.email and supplier_in.email != supplier.email:
|
|
existing_supplier = crud.supplier.get_by_email(db, email=supplier_in.email)
|
|
if existing_supplier and existing_supplier.id != supplier_id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Supplier with this email already exists",
|
|
)
|
|
|
|
supplier = crud.supplier.update(db, db_obj=supplier, obj_in=supplier_in)
|
|
return supplier
|
|
|
|
|
|
@router.delete("/{supplier_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_supplier(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
supplier_id: int,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a supplier.
|
|
"""
|
|
supplier = crud.supplier.get(db, id=supplier_id)
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
|
|
# Check if any items are using this supplier
|
|
items = crud.item.get_by_supplier(db, supplier_id=supplier_id, skip=0, limit=1)
|
|
if items:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Cannot delete supplier with associated items",
|
|
)
|
|
|
|
crud.supplier.remove(db, id=supplier_id)
|
|
return None |