155 lines
4.7 KiB
Python
155 lines
4.7 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Body
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.InventoryItem])
|
|
def read_inventory_items(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve inventory items.
|
|
"""
|
|
inventory_items = crud.inventory.get_multi(db, skip=skip, limit=limit)
|
|
return inventory_items
|
|
|
|
|
|
@router.get("/low-stock", response_model=List[schemas.InventoryItem])
|
|
def read_low_stock_items(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve low stock inventory items.
|
|
"""
|
|
inventory_items = crud.inventory.get_low_stock(db, skip=skip, limit=limit)
|
|
return inventory_items
|
|
|
|
|
|
@router.post("/", response_model=schemas.InventoryItem)
|
|
def create_inventory_item(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
item_in: schemas.InventoryItemCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new inventory item.
|
|
"""
|
|
product = crud.product.get(db, id=item_in.product_id)
|
|
if not product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
existing_item = crud.inventory.get_by_product(db, product_id=item_in.product_id)
|
|
if existing_item:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="An inventory item for this product already exists.",
|
|
)
|
|
|
|
inventory_item = crud.inventory.create(db, obj_in=item_in)
|
|
return inventory_item
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.InventoryItem)
|
|
def update_inventory_item(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
item_in: schemas.InventoryItemUpdate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update an inventory item.
|
|
"""
|
|
inventory_item = crud.inventory.get(db, id=id)
|
|
if not inventory_item:
|
|
raise HTTPException(status_code=404, detail="Inventory item not found")
|
|
|
|
inventory_item = crud.inventory.update(db, db_obj=inventory_item, obj_in=item_in)
|
|
return inventory_item
|
|
|
|
|
|
@router.put("/{id}/adjust-quantity", response_model=schemas.InventoryItem)
|
|
def adjust_inventory_quantity(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
quantity_change: int = Body(..., embed=True),
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Adjust inventory quantity. Use positive number to increase, negative to decrease.
|
|
"""
|
|
inventory_item = crud.inventory.get(db, id=id)
|
|
if not inventory_item:
|
|
raise HTTPException(status_code=404, detail="Inventory item not found")
|
|
|
|
if inventory_item.quantity + quantity_change < 0:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Cannot reduce quantity below zero."
|
|
)
|
|
|
|
inventory_item = crud.inventory.update_quantity(db, item_id=id, quantity_change=quantity_change)
|
|
return inventory_item
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.InventoryItem)
|
|
def read_inventory_item(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get inventory item by ID.
|
|
"""
|
|
inventory_item = crud.inventory.get(db, id=id)
|
|
if not inventory_item:
|
|
raise HTTPException(status_code=404, detail="Inventory item not found")
|
|
return inventory_item
|
|
|
|
|
|
@router.get("/product/{product_id}", response_model=schemas.InventoryItem)
|
|
def read_inventory_by_product(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
product_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get inventory item by product ID.
|
|
"""
|
|
inventory_item = crud.inventory.get_by_product(db, product_id=product_id)
|
|
if not inventory_item:
|
|
raise HTTPException(status_code=404, detail="Inventory item not found")
|
|
return inventory_item
|
|
|
|
|
|
@router.delete("/{id}", response_model=schemas.InventoryItem)
|
|
def delete_inventory_item(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete an inventory item.
|
|
"""
|
|
inventory_item = crud.inventory.get(db, id=id)
|
|
if not inventory_item:
|
|
raise HTTPException(status_code=404, detail="Inventory item not found")
|
|
inventory_item = crud.inventory.remove(db, id=id)
|
|
return inventory_item |