102 lines
2.7 KiB
Python
102 lines
2.7 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_db, get_current_user
|
|
from app.models.user import User
|
|
from app.crud import supplier
|
|
from app.schemas.supplier import Supplier, SupplierCreate, SupplierUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@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_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve suppliers.
|
|
"""
|
|
suppliers = supplier.get_multi(db, skip=skip, limit=limit)
|
|
return suppliers
|
|
|
|
|
|
@router.post("/", response_model=Supplier)
|
|
def create_supplier(
|
|
*,
|
|
supplier_in: SupplierCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Create new supplier.
|
|
"""
|
|
db_supplier = supplier.get_by_name(db, name=supplier_in.name)
|
|
if db_supplier:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A supplier with this name already exists",
|
|
)
|
|
return supplier.create(db, obj_in=supplier_in)
|
|
|
|
|
|
@router.get("/{supplier_id}", response_model=Supplier)
|
|
def read_supplier(
|
|
*,
|
|
supplier_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get supplier by ID.
|
|
"""
|
|
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_user),
|
|
) -> Any:
|
|
"""
|
|
Update a supplier.
|
|
"""
|
|
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}", response_model=Supplier)
|
|
def delete_supplier(
|
|
*,
|
|
supplier_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a supplier.
|
|
"""
|
|
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.remove(db, id=supplier_id) |