marketplace refactoring

This commit is contained in:
2025-10-04 13:38:10 +02:00
parent 32be301d83
commit c971674ec2
68 changed files with 1102 additions and 1128 deletions

View File

@@ -20,13 +20,13 @@ from app.exceptions import (
ShopAlreadyExistsException,
UnauthorizedShopAccessException,
InvalidShopDataException,
ProductNotFoundException,
MarketplaceProductNotFoundException,
ShopProductAlreadyExistsException,
MaxShopsReachedException,
ValidationException,
)
from models.schemas.shop import ShopCreate, ShopProductCreate
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.shop import Shop, ShopProduct
from models.database.user import User
@@ -198,22 +198,22 @@ class ShopService:
Created ShopProduct object
Raises:
ProductNotFoundException: If product not found
MarketplaceProductNotFoundException: If product not found
ShopProductAlreadyExistsException: If product already in shop
"""
try:
# Check if product exists
product = self._get_product_by_id_or_raise(db, shop_product.product_id)
marketplace_product = self._get_product_by_id_or_raise(db, shop_product.marketplace_product_id)
# Check if product already in shop
if self._product_in_shop(db, shop.id, product.id):
raise ShopProductAlreadyExistsException(shop.shop_code, shop_product.product_id)
if self._product_in_shop(db, shop.id, marketplace_product.id):
raise ShopProductAlreadyExistsException(shop.shop_code, shop_product.marketplace_product_id)
# Create shop-product association
new_shop_product = ShopProduct(
shop_id=shop.id,
product_id=product.id,
**shop_product.model_dump(exclude={"product_id"}),
marketplace_product_id=marketplace_product.id,
**shop_product.model_dump(exclude={"marketplace_product_id"}),
)
db.add(new_shop_product)
@@ -223,10 +223,10 @@ class ShopService:
# Load the product relationship
db.refresh(new_shop_product)
logger.info(f"Product {shop_product.product_id} added to shop {shop.shop_code}")
logger.info(f"MarketplaceProduct {shop_product.marketplace_product_id} added to shop {shop.shop_code}")
return new_shop_product
except (ProductNotFoundException, ShopProductAlreadyExistsException):
except (MarketplaceProductNotFoundException, ShopProductAlreadyExistsException):
db.rollback()
raise # Re-raise custom exceptions
except Exception as e:
@@ -322,20 +322,20 @@ class ShopService:
.first() is not None
)
def _get_product_by_id_or_raise(self, db: Session, product_id: str) -> Product:
def _get_product_by_id_or_raise(self, db: Session, marketplace_product_id: str) -> MarketplaceProduct:
"""Get product by ID or raise exception."""
product = db.query(Product).filter(Product.product_id == product_id).first()
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id).first()
if not product:
raise ProductNotFoundException(product_id)
raise MarketplaceProductNotFoundException(marketplace_product_id)
return product
def _product_in_shop(self, db: Session, shop_id: int, product_id: int) -> bool:
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)
.filter(
ShopProduct.shop_id == shop_id,
ShopProduct.product_id == product_id
ShopProduct.marketplace_product_id == marketplace_product_id
)
.first() is not None
)