113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
# app/api/v1/stock.py
|
|
"""
|
|
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, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_user
|
|
from app.core.database import get_db
|
|
from app.services.stock_service import stock_service
|
|
from models.schemas.stock import (StockAdd, StockCreate, StockResponse,
|
|
StockSummaryResponse, StockUpdate)
|
|
from models.database.user import User
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.post("/stock", response_model=StockResponse)
|
|
def set_stock(
|
|
stock: StockCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Set exact stock quantity for a GTIN at a specific location (replaces existing quantity)."""
|
|
return stock_service.set_stock(db, stock)
|
|
|
|
|
|
@router.post("/stock/add", response_model=StockResponse)
|
|
def add_stock(
|
|
stock: StockAdd,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Add quantity to existing stock for a GTIN at a specific location (adds to existing quantity)."""
|
|
return stock_service.add_stock(db, stock)
|
|
|
|
|
|
@router.post("/stock/remove", response_model=StockResponse)
|
|
def remove_stock(
|
|
stock: StockAdd,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Remove quantity from existing stock for a GTIN at a specific location."""
|
|
return stock_service.remove_stock(db, stock)
|
|
|
|
|
|
@router.get("/stock/{gtin}", response_model=StockSummaryResponse)
|
|
def get_stock_by_gtin(
|
|
gtin: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get all stock locations and total quantity for a specific GTIN."""
|
|
return stock_service.get_stock_by_gtin(db, gtin)
|
|
|
|
|
|
@router.get("/stock/{gtin}/total")
|
|
def get_total_stock(
|
|
gtin: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get total quantity in stock for a specific GTIN."""
|
|
return stock_service.get_total_stock(db, gtin)
|
|
|
|
|
|
@router.get("/stock", response_model=List[StockResponse])
|
|
def get_all_stock(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=1000),
|
|
location: Optional[str] = Query(None, description="Filter by location"),
|
|
gtin: Optional[str] = Query(None, description="Filter by GTIN"),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get all stock entries with optional filtering."""
|
|
return stock_service.get_all_stock(
|
|
db=db, skip=skip, limit=limit, location=location, gtin=gtin
|
|
)
|
|
|
|
|
|
@router.put("/stock/{stock_id}", response_model=StockResponse)
|
|
def update_stock(
|
|
stock_id: int,
|
|
stock_update: StockUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Update stock quantity for a specific stock entry."""
|
|
return stock_service.update_stock(db, stock_id, stock_update)
|
|
|
|
|
|
@router.delete("/stock/{stock_id}")
|
|
def delete_stock(
|
|
stock_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Delete a stock entry."""
|
|
stock_service.delete_stock(db, stock_id)
|
|
return {"message": "Stock entry deleted successfully"}
|