Exception handling enhancement
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
# app/api/v1/stock.py
|
||||
"""Summary description ....
|
||||
"""
|
||||
Stock endpoints - simplified with service-level exception handling.
|
||||
|
||||
This module provides classes and functions for:
|
||||
- ....
|
||||
- ....
|
||||
- ....
|
||||
- Stock quantity management (set, add, remove)
|
||||
- Stock information retrieval and filtering
|
||||
- Location-based stock tracking
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_user
|
||||
@@ -24,9 +25,6 @@ router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Stock Management Routes (Protected)
|
||||
|
||||
|
||||
@router.post("/stock", response_model=StockResponse)
|
||||
def set_stock(
|
||||
stock: StockCreate,
|
||||
@@ -34,14 +32,7 @@ def set_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Set exact stock quantity for a GTIN at a specific location (replaces existing quantity)."""
|
||||
try:
|
||||
result = stock_service.set_stock(db, stock)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error setting stock: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.set_stock(db, stock)
|
||||
|
||||
|
||||
@router.post("/stock/add", response_model=StockResponse)
|
||||
@@ -51,14 +42,7 @@ def add_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Add quantity to existing stock for a GTIN at a specific location (adds to existing quantity)."""
|
||||
try:
|
||||
result = stock_service.add_stock(db, stock)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding stock: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.add_stock(db, stock)
|
||||
|
||||
|
||||
@router.post("/stock/remove", response_model=StockResponse)
|
||||
@@ -68,14 +52,7 @@ def remove_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Remove quantity from existing stock for a GTIN at a specific location."""
|
||||
try:
|
||||
result = stock_service.remove_stock(db, stock)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error removing stock: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.remove_stock(db, stock)
|
||||
|
||||
|
||||
@router.get("/stock/{gtin}", response_model=StockSummaryResponse)
|
||||
@@ -85,14 +62,7 @@ def get_stock_by_gtin(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get all stock locations and total quantity for a specific GTIN."""
|
||||
try:
|
||||
result = stock_service.get_stock_by_gtin(db, gtin)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting stock for GTIN {gtin}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.get_stock_by_gtin(db, gtin)
|
||||
|
||||
|
||||
@router.get("/stock/{gtin}/total")
|
||||
@@ -102,14 +72,7 @@ def get_total_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get total quantity in stock for a specific GTIN."""
|
||||
try:
|
||||
result = stock_service.get_total_stock(db, gtin)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting total stock for GTIN {gtin}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.get_total_stock(db, gtin)
|
||||
|
||||
|
||||
@router.get("/stock", response_model=List[StockResponse])
|
||||
@@ -122,14 +85,9 @@ def get_all_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get all stock entries with optional filtering."""
|
||||
try:
|
||||
result = stock_service.get_all_stock(
|
||||
db=db, skip=skip, limit=limit, location=location, gtin=gtin
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting all stock: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.get_all_stock(
|
||||
db=db, skip=skip, limit=limit, location=location, gtin=gtin
|
||||
)
|
||||
|
||||
|
||||
@router.put("/stock/{stock_id}", response_model=StockResponse)
|
||||
@@ -140,14 +98,7 @@ def update_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Update stock quantity for a specific stock entry."""
|
||||
try:
|
||||
result = stock_service.update_stock(db, stock_id, stock_update)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating stock {stock_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return stock_service.update_stock(db, stock_id, stock_update)
|
||||
|
||||
|
||||
@router.delete("/stock/{stock_id}")
|
||||
@@ -157,11 +108,5 @@ def delete_stock(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Delete a stock entry."""
|
||||
try:
|
||||
stock_service.delete_stock(db, stock_id)
|
||||
return {"message": "Stock entry deleted successfully"}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting stock {stock_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
stock_service.delete_stock(db, stock_id)
|
||||
return {"message": "Stock entry deleted successfully"}
|
||||
|
||||
Reference in New Issue
Block a user