Exception handling enhancement
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
# app/api/v1/shop.py
|
||||
"""Summary description ....
|
||||
"""
|
||||
Shop endpoints - simplified with service-level exception handling.
|
||||
|
||||
This module provides classes and functions for:
|
||||
- ....
|
||||
- ....
|
||||
- ....
|
||||
- Shop CRUD operations and management
|
||||
- Shop product catalog management
|
||||
- Shop filtering and verification
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
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, get_user_shop
|
||||
@@ -23,7 +24,6 @@ router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Shop Management Routes
|
||||
@router.post("/shop", response_model=ShopResponse)
|
||||
def create_shop(
|
||||
shop_data: ShopCreate,
|
||||
@@ -31,16 +31,10 @@ def create_shop(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new shop (Protected)."""
|
||||
try:
|
||||
shop = shop_service.create_shop(
|
||||
db=db, shop_data=shop_data, current_user=current_user
|
||||
)
|
||||
return ShopResponse.model_validate(shop)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating shop: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
shop = shop_service.create_shop(
|
||||
db=db, shop_data=shop_data, current_user=current_user
|
||||
)
|
||||
return ShopResponse.model_validate(shop)
|
||||
|
||||
|
||||
@router.get("/shop", response_model=ShopListResponse)
|
||||
@@ -53,22 +47,16 @@ def get_shops(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get shops with filtering (Protected)."""
|
||||
try:
|
||||
shops, total = shop_service.get_shops(
|
||||
db=db,
|
||||
current_user=current_user,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
active_only=active_only,
|
||||
verified_only=verified_only,
|
||||
)
|
||||
shops, total = shop_service.get_shops(
|
||||
db=db,
|
||||
current_user=current_user,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
active_only=active_only,
|
||||
verified_only=verified_only,
|
||||
)
|
||||
|
||||
return ShopListResponse(shops=shops, total=total, skip=skip, limit=limit)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting shops: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return ShopListResponse(shops=shops, total=total, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.get("/shop/{shop_code}", response_model=ShopResponse)
|
||||
@@ -78,19 +66,12 @@ def get_shop(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get shop details (Protected)."""
|
||||
try:
|
||||
shop = shop_service.get_shop_by_code(
|
||||
db=db, shop_code=shop_code, current_user=current_user
|
||||
)
|
||||
return ShopResponse.model_validate(shop)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting shop {shop_code}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
shop = shop_service.get_shop_by_code(
|
||||
db=db, shop_code=shop_code, current_user=current_user
|
||||
)
|
||||
return ShopResponse.model_validate(shop)
|
||||
|
||||
|
||||
# Shop Product Management
|
||||
@router.post("/shop/{shop_code}/products", response_model=ShopProductResponse)
|
||||
def add_product_to_shop(
|
||||
shop_code: str,
|
||||
@@ -99,24 +80,18 @@ def add_product_to_shop(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Add existing product to shop catalog with shop-specific settings (Protected)."""
|
||||
try:
|
||||
# Get and verify shop (using existing dependency)
|
||||
shop = get_user_shop(shop_code, current_user, db)
|
||||
# Get and verify shop (using existing dependency)
|
||||
shop = get_user_shop(shop_code, current_user, db)
|
||||
|
||||
# Add product to shop
|
||||
new_shop_product = shop_service.add_product_to_shop(
|
||||
db=db, shop=shop, shop_product=shop_product
|
||||
)
|
||||
# Add product to shop
|
||||
new_shop_product = shop_service.add_product_to_shop(
|
||||
db=db, shop=shop, shop_product=shop_product
|
||||
)
|
||||
|
||||
# Return with product details
|
||||
response = ShopProductResponse.model_validate(new_shop_product)
|
||||
response.product = new_shop_product.product
|
||||
return response
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding product to shop {shop_code}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
# Return with product details
|
||||
response = ShopProductResponse.model_validate(new_shop_product)
|
||||
response.product = new_shop_product.product
|
||||
return response
|
||||
|
||||
|
||||
@router.get("/shop/{shop_code}/products")
|
||||
@@ -130,39 +105,33 @@ def get_shop_products(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get products in shop catalog (Protected)."""
|
||||
try:
|
||||
# Get shop
|
||||
shop = shop_service.get_shop_by_code(
|
||||
db=db, shop_code=shop_code, current_user=current_user
|
||||
)
|
||||
# Get shop
|
||||
shop = shop_service.get_shop_by_code(
|
||||
db=db, shop_code=shop_code, current_user=current_user
|
||||
)
|
||||
|
||||
# Get shop products
|
||||
shop_products, total = shop_service.get_shop_products(
|
||||
db=db,
|
||||
shop=shop,
|
||||
current_user=current_user,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
active_only=active_only,
|
||||
featured_only=featured_only,
|
||||
)
|
||||
# Get shop products
|
||||
shop_products, total = shop_service.get_shop_products(
|
||||
db=db,
|
||||
shop=shop,
|
||||
current_user=current_user,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
active_only=active_only,
|
||||
featured_only=featured_only,
|
||||
)
|
||||
|
||||
# Format response
|
||||
products = []
|
||||
for sp in shop_products:
|
||||
product_response = ShopProductResponse.model_validate(sp)
|
||||
product_response.product = sp.product
|
||||
products.append(product_response)
|
||||
# Format response
|
||||
products = []
|
||||
for sp in shop_products:
|
||||
product_response = ShopProductResponse.model_validate(sp)
|
||||
product_response.product = sp.product
|
||||
products.append(product_response)
|
||||
|
||||
return {
|
||||
"products": products,
|
||||
"total": total,
|
||||
"skip": skip,
|
||||
"limit": limit,
|
||||
"shop": ShopResponse.model_validate(shop),
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting products for shop {shop_code}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
return {
|
||||
"products": products,
|
||||
"total": total,
|
||||
"skip": skip,
|
||||
"limit": limit,
|
||||
"shop": ShopResponse.model_validate(shop),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user