code quality run
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
|
||||
from app.api.deps import get_current_user
|
||||
from models.api_models import (ProductListResponse, ProductResponse, ProductCreate, ProductDetailResponse,
|
||||
from app.core.database import get_db
|
||||
from app.services.product_service import product_service
|
||||
from models.api_models import (ProductCreate, ProductDetailResponse,
|
||||
ProductListResponse, ProductResponse,
|
||||
ProductUpdate)
|
||||
from models.database_models import User
|
||||
import logging
|
||||
|
||||
from app.services.product_service import product_service
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -20,16 +20,16 @@ logger = logging.getLogger(__name__)
|
||||
# Enhanced Product Routes with Marketplace Support
|
||||
@router.get("/product", response_model=ProductListResponse)
|
||||
def get_products(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
brand: Optional[str] = Query(None),
|
||||
category: Optional[str] = Query(None),
|
||||
availability: Optional[str] = Query(None),
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
search: Optional[str] = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
brand: Optional[str] = Query(None),
|
||||
category: Optional[str] = Query(None),
|
||||
availability: Optional[str] = Query(None),
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
search: Optional[str] = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get products with advanced filtering including marketplace and shop (Protected)"""
|
||||
|
||||
@@ -43,14 +43,11 @@ def get_products(
|
||||
availability=availability,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name,
|
||||
search=search
|
||||
search=search,
|
||||
)
|
||||
|
||||
return ProductListResponse(
|
||||
products=products,
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
products=products, total=total, skip=skip, limit=limit
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting products: {str(e)}")
|
||||
@@ -59,9 +56,9 @@ def get_products(
|
||||
|
||||
@router.post("/product", response_model=ProductResponse)
|
||||
def create_product(
|
||||
product: ProductCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
product: ProductCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new product with validation and marketplace support (Protected)"""
|
||||
|
||||
@@ -75,7 +72,9 @@ def create_product(
|
||||
|
||||
if existing:
|
||||
logger.info("Product already exists, raising 400 error")
|
||||
raise HTTPException(status_code=400, detail="Product with this ID already exists")
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Product with this ID already exists"
|
||||
)
|
||||
|
||||
logger.info("No existing product found, proceeding to create...")
|
||||
db_product = product_service.create_product(db, product)
|
||||
@@ -93,11 +92,12 @@ def create_product(
|
||||
logger.error(f"Unexpected error: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.get("/product/{product_id}", response_model=ProductDetailResponse)
|
||||
def get_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get product with stock information (Protected)"""
|
||||
|
||||
@@ -111,10 +111,7 @@ def get_product(
|
||||
if product.gtin:
|
||||
stock_info = product_service.get_stock_info(db, product.gtin)
|
||||
|
||||
return ProductDetailResponse(
|
||||
product=product,
|
||||
stock_info=stock_info
|
||||
)
|
||||
return ProductDetailResponse(product=product, stock_info=stock_info)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
@@ -125,10 +122,10 @@ def get_product(
|
||||
|
||||
@router.put("/product/{product_id}", response_model=ProductResponse)
|
||||
def update_product(
|
||||
product_id: str,
|
||||
product_update: ProductUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
product_id: str,
|
||||
product_update: ProductUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Update product with validation and marketplace support (Protected)"""
|
||||
|
||||
@@ -151,9 +148,9 @@ def update_product(
|
||||
|
||||
@router.delete("/product/{product_id}")
|
||||
def delete_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Delete product and associated stock (Protected)"""
|
||||
|
||||
@@ -176,19 +173,18 @@ def delete_product(
|
||||
# Export with streaming for large datasets (Protected)
|
||||
@router.get("/export-csv")
|
||||
async def export_csv(
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Export products as CSV with streaming and marketplace filtering (Protected)"""
|
||||
|
||||
try:
|
||||
|
||||
def generate_csv():
|
||||
return product_service.generate_csv_export(
|
||||
db=db,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name
|
||||
db=db, marketplace=marketplace, shop_name=shop_name
|
||||
)
|
||||
|
||||
filename = "products_export"
|
||||
@@ -201,7 +197,7 @@ async def export_csv(
|
||||
return StreamingResponse(
|
||||
generate_csv(),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"},
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user