shop product refactoring

This commit is contained in:
2025-10-04 21:27:48 +02:00
parent c971674ec2
commit 4d2866af5e
21 changed files with 259 additions and 220 deletions

View File

@@ -21,13 +21,15 @@ from app.exceptions import (
UnauthorizedShopAccessException,
InvalidShopDataException,
MarketplaceProductNotFoundException,
ShopProductAlreadyExistsException,
ProductAlreadyExistsException,
MaxShopsReachedException,
ValidationException,
)
from models.schemas.shop import ShopCreate, ShopProductCreate
from models.schemas.shop import ShopCreate
from models.schemas.product import ProductCreate
from models.database.marketplace_product import MarketplaceProduct
from models.database.shop import Shop, ShopProduct
from models.database.shop import Shop
from models.database.product import Product
from models.database.user import User
logger = logging.getLogger(__name__)
@@ -184,49 +186,49 @@ class ShopService:
raise ValidationException("Failed to retrieve shop")
def add_product_to_shop(
self, db: Session, shop: Shop, shop_product: ShopProductCreate
) -> ShopProduct:
self, db: Session, shop: Shop, product: ProductCreate
) -> Product:
"""
Add existing product to shop catalog with shop-specific settings.
Args:
db: Database session
shop: Shop to add product to
shop_product: Shop product data
product: Shop product data
Returns:
Created ShopProduct object
Raises:
MarketplaceProductNotFoundException: If product not found
ShopProductAlreadyExistsException: If product already in shop
ProductAlreadyExistsException: If product already in shop
"""
try:
# Check if product exists
marketplace_product = self._get_product_by_id_or_raise(db, shop_product.marketplace_product_id)
marketplace_product = self._get_product_by_id_or_raise(db, product.marketplace_product_id)
# Check if product already in shop
if self._product_in_shop(db, shop.id, marketplace_product.id):
raise ShopProductAlreadyExistsException(shop.shop_code, shop_product.marketplace_product_id)
raise ProductAlreadyExistsException(shop.shop_code, product.marketplace_product_id)
# Create shop-product association
new_shop_product = ShopProduct(
new_product = Product(
shop_id=shop.id,
marketplace_product_id=marketplace_product.id,
**shop_product.model_dump(exclude={"marketplace_product_id"}),
**product.model_dump(exclude={"marketplace_product_id"}),
)
db.add(new_shop_product)
db.add(new_product)
db.commit()
db.refresh(new_shop_product)
db.refresh(new_product)
# Load the product relationship
db.refresh(new_shop_product)
db.refresh(new_product)
logger.info(f"MarketplaceProduct {shop_product.marketplace_product_id} added to shop {shop.shop_code}")
return new_shop_product
logger.info(f"MarketplaceProduct {product.marketplace_product_id} added to shop {shop.shop_code}")
return new_product
except (MarketplaceProductNotFoundException, ShopProductAlreadyExistsException):
except (MarketplaceProductNotFoundException, ProductAlreadyExistsException):
db.rollback()
raise # Re-raise custom exceptions
except Exception as e:
@@ -234,7 +236,7 @@ class ShopService:
logger.error(f"Error adding product to shop: {str(e)}")
raise ValidationException("Failed to add product to shop")
def get_shop_products(
def get_products(
self,
db: Session,
shop: Shop,
@@ -243,7 +245,7 @@ class ShopService:
limit: int = 100,
active_only: bool = True,
featured_only: bool = False,
) -> Tuple[List[ShopProduct], int]:
) -> Tuple[List[Product], int]:
"""
Get products in shop catalog with filtering.
@@ -257,7 +259,7 @@ class ShopService:
featured_only: Filter for featured products only
Returns:
Tuple of (shop_products_list, total_count)
Tuple of (products_list, total_count)
Raises:
UnauthorizedShopAccessException: If shop access denied
@@ -268,17 +270,17 @@ class ShopService:
raise UnauthorizedShopAccessException(shop.shop_code, current_user.id)
# Query shop products
query = db.query(ShopProduct).filter(ShopProduct.shop_id == shop.id)
query = db.query(Product).filter(Product.shop_id == shop.id)
if active_only:
query = query.filter(ShopProduct.is_active == True)
query = query.filter(Product.is_active == True)
if featured_only:
query = query.filter(ShopProduct.is_featured == True)
query = query.filter(Product.is_featured == True)
total = query.count()
shop_products = query.offset(skip).limit(limit).all()
products = query.offset(skip).limit(limit).all()
return shop_products, total
return products, total
except UnauthorizedShopAccessException:
raise # Re-raise custom exceptions
@@ -332,10 +334,10 @@ class ShopService:
def _product_in_shop(self, db: Session, shop_id: int, marketplace_product_id: int) -> bool:
"""Check if product is already in shop."""
return (
db.query(ShopProduct)
db.query(Product)
.filter(
ShopProduct.shop_id == shop_id,
ShopProduct.marketplace_product_id == marketplace_product_id
Product.shop_id == shop_id,
Product.marketplace_product_id == marketplace_product_id
)
.first() is not None
)