
- Set up project structure for FastAPI application - Create database models for items, categories, suppliers, and transactions - Set up Alembic for database migrations - Implement API endpoints for all entities - Add authentication with JWT tokens - Add health check endpoint - Create comprehensive README with documentation
110 lines
2.9 KiB
Python
110 lines
2.9 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_user, get_db
|
|
from app.models.supplier import Supplier
|
|
from app.models.user import User
|
|
from app.schemas.supplier import Supplier as SupplierSchema, SupplierCreate, SupplierUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[SupplierSchema])
|
|
def read_suppliers(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve suppliers.
|
|
"""
|
|
suppliers = db.query(Supplier).offset(skip).limit(limit).all()
|
|
return suppliers
|
|
|
|
|
|
@router.post("/", response_model=SupplierSchema)
|
|
def create_supplier(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
supplier_in: SupplierCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new supplier.
|
|
"""
|
|
supplier = Supplier(**supplier_in.dict())
|
|
db.add(supplier)
|
|
db.commit()
|
|
db.refresh(supplier)
|
|
return supplier
|
|
|
|
|
|
@router.put("/{supplier_id}", response_model=SupplierSchema)
|
|
def update_supplier(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
supplier_id: int,
|
|
supplier_in: SupplierUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a supplier.
|
|
"""
|
|
supplier = db.query(Supplier).filter(Supplier.id == supplier_id).first()
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
|
|
update_data = supplier_in.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(supplier, field, value)
|
|
|
|
db.add(supplier)
|
|
db.commit()
|
|
db.refresh(supplier)
|
|
return supplier
|
|
|
|
|
|
@router.get("/{supplier_id}", response_model=SupplierSchema)
|
|
def read_supplier(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
supplier_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get supplier by ID.
|
|
"""
|
|
supplier = db.query(Supplier).filter(Supplier.id == supplier_id).first()
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
return supplier
|
|
|
|
|
|
@router.delete("/{supplier_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_supplier(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
supplier_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a supplier.
|
|
"""
|
|
supplier = db.query(Supplier).filter(Supplier.id == supplier_id).first()
|
|
if not supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Supplier not found",
|
|
)
|
|
db.delete(supplier)
|
|
db.commit()
|
|
return None |