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

@@ -16,8 +16,8 @@ from sqlalchemy.orm import Session
from app.api.deps import get_current_user, get_user_shop
from app.core.database import get_db
from app.services.shop_service import shop_service
from models.schemas.shop import (ShopCreate, ShopListResponse, ShopProductCreate,
ShopProductResponse, ShopResponse)
from models.schemas.shop import (ShopCreate, ShopListResponse,ShopResponse)
from models.schemas.product import (ProductCreate,ProductResponse)
from models.database.user import User
router = APIRouter()
@@ -72,10 +72,10 @@ def get_shop(
return ShopResponse.model_validate(shop)
@router.post("/shop/{shop_code}/products", response_model=ShopProductResponse)
@router.post("/shop/{shop_code}/products", response_model=ProductResponse)
def add_product_to_shop(
shop_code: str,
shop_product: ShopProductCreate,
product: ProductCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
@@ -84,18 +84,18 @@ def add_product_to_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
new_product = shop_service.add_product_to_shop(
db=db, shop=shop, product=product
)
# Return with product details
response = ShopProductResponse.model_validate(new_shop_product)
response.product = new_shop_product.product
response = ProductResponse.model_validate(new_product)
response.marketplace_product = new_product.marketplace_product
return response
@router.get("/shop/{shop_code}/products")
def get_shop_products(
def get_products(
shop_code: str,
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=1000),
@@ -111,7 +111,7 @@ def get_shop_products(
)
# Get shop products
shop_products, total = shop_service.get_shop_products(
vendor_products, total = shop_service.get_products(
db=db,
shop=shop,
current_user=current_user,
@@ -123,9 +123,9 @@ def get_shop_products(
# Format response
products = []
for sp in shop_products:
product_response = ShopProductResponse.model_validate(sp)
product_response.product = sp.product
for vp in vendor_products:
product_response = ProductResponse.model_validate(vp)
product_response.marketplace_product = vp.marketplace_product
products.append(product_response)
return {