Refactoring code for modular approach
This commit is contained in:
@@ -4,6 +4,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, BackgroundTasks
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_user, get_user_shop
|
||||
from app.services.shop_service import shop_service
|
||||
from app.tasks.background_tasks import process_marketplace_import
|
||||
from middleware.decorators import rate_limit
|
||||
from models.api_models import MarketplaceImportJobResponse, MarketplaceImportRequest, ShopResponse, ShopCreate, \
|
||||
@@ -24,25 +25,14 @@ def create_shop(
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Create a new shop (Protected)"""
|
||||
# Check if shop code already exists
|
||||
existing_shop = db.query(Shop).filter(Shop.shop_code == shop_data.shop_code).first()
|
||||
if existing_shop:
|
||||
raise HTTPException(status_code=400, detail="Shop code already exists")
|
||||
|
||||
# Create shop
|
||||
new_shop = Shop(
|
||||
**shop_data.dict(),
|
||||
owner_id=current_user.id,
|
||||
is_active=True,
|
||||
is_verified=(current_user.role == "admin") # Auto-verify if admin creates shop
|
||||
)
|
||||
|
||||
db.add(new_shop)
|
||||
db.commit()
|
||||
db.refresh(new_shop)
|
||||
|
||||
logger.info(f"New shop created: {new_shop.shop_code} by {current_user.username}")
|
||||
return new_shop
|
||||
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")
|
||||
|
||||
|
||||
@router.get("/shops", response_model=ShopListResponse)
|
||||
@@ -55,45 +45,40 @@ def get_shops(
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get shops with filtering (Protected)"""
|
||||
query = db.query(Shop)
|
||||
|
||||
# Non-admin users can only see active and verified shops, plus their own
|
||||
if current_user.role != "admin":
|
||||
query = query.filter(
|
||||
(Shop.is_active == True) &
|
||||
((Shop.is_verified == True) | (Shop.owner_id == current_user.id))
|
||||
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
|
||||
)
|
||||
else:
|
||||
# Admin can apply filters
|
||||
if active_only:
|
||||
query = query.filter(Shop.is_active == True)
|
||||
if verified_only:
|
||||
query = query.filter(Shop.is_verified == True)
|
||||
|
||||
total = query.count()
|
||||
shops = query.offset(skip).limit(limit).all()
|
||||
|
||||
return ShopListResponse(
|
||||
shops=shops,
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
)
|
||||
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")
|
||||
|
||||
|
||||
@router.get("/shops/{shop_code}", response_model=ShopResponse)
|
||||
def get_shop(shop_code: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get shop details (Protected)"""
|
||||
shop = db.query(Shop).filter(Shop.shop_code == shop_code.upper()).first()
|
||||
if not shop:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
# Non-admin users can only see active verified shops or their own shops
|
||||
if current_user.role != "admin":
|
||||
if not shop.is_active or (not shop.is_verified and shop.owner_id != current_user.id):
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
return shop
|
||||
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 Product Management
|
||||
@@ -105,39 +90,26 @@ 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
|
||||
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
|
||||
)
|
||||
|
||||
# Check if product exists
|
||||
product = db.query(Product).filter(Product.product_id == shop_product.product_id).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found in marketplace catalog")
|
||||
|
||||
# Check if product already in shop
|
||||
existing_shop_product = db.query(ShopProduct).filter(
|
||||
ShopProduct.shop_id == shop.id,
|
||||
ShopProduct.product_id == product.id
|
||||
).first()
|
||||
|
||||
if existing_shop_product:
|
||||
raise HTTPException(status_code=400, detail="Product already in shop catalog")
|
||||
|
||||
# Create shop-product association
|
||||
new_shop_product = ShopProduct(
|
||||
shop_id=shop.id,
|
||||
product_id=product.id,
|
||||
**shop_product.dict(exclude={'product_id'})
|
||||
)
|
||||
|
||||
db.add(new_shop_product)
|
||||
db.commit()
|
||||
db.refresh(new_shop_product)
|
||||
|
||||
# Return with product details
|
||||
response = ShopProductResponse.model_validate(new_shop_product)
|
||||
response.product = product
|
||||
return response
|
||||
# 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")
|
||||
|
||||
|
||||
@router.get("/shops/{shop_code}/products")
|
||||
@@ -151,39 +123,37 @@ 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 (public can view active/verified shops)
|
||||
shop = db.query(Shop).filter(Shop.shop_code == shop_code.upper()).first()
|
||||
if not shop:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
# 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
|
||||
)
|
||||
|
||||
# Non-owners can only see active verified shops
|
||||
if current_user.role != "admin" and shop.owner_id != current_user.id:
|
||||
if not shop.is_active or not shop.is_verified:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
# Format response
|
||||
products = []
|
||||
for sp in shop_products:
|
||||
product_response = ShopProductResponse.model_validate(sp)
|
||||
product_response.product = sp.product
|
||||
products.append(product_response)
|
||||
|
||||
# Query shop products
|
||||
query = db.query(ShopProduct).filter(ShopProduct.shop_id == shop.id)
|
||||
|
||||
if active_only:
|
||||
query = query.filter(ShopProduct.is_active == True)
|
||||
if featured_only:
|
||||
query = query.filter(ShopProduct.is_featured == True)
|
||||
|
||||
total = query.count()
|
||||
shop_products = query.offset(skip).limit(limit).all()
|
||||
|
||||
# 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)
|
||||
}
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user