138 lines
4.0 KiB
Python
138 lines
4.0 KiB
Python
# app/api/v1/shop.py
|
|
"""
|
|
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, Query
|
|
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,ShopResponse)
|
|
from models.schemas.product import (ProductCreate,ProductResponse)
|
|
from models.database.user import User
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.post("/shop", response_model=ShopResponse)
|
|
def create_shop(
|
|
shop_data: ShopCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Create a new shop (Protected)."""
|
|
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)
|
|
def get_shops(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=1000),
|
|
active_only: bool = Query(True),
|
|
verified_only: bool = Query(False),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get shops with filtering (Protected)."""
|
|
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)
|
|
|
|
|
|
@router.get("/shop/{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 = shop_service.get_shop_by_code(
|
|
db=db, shop_code=shop_code, current_user=current_user
|
|
)
|
|
return ShopResponse.model_validate(shop)
|
|
|
|
|
|
@router.post("/shop/{shop_code}/products", response_model=ProductResponse)
|
|
def add_product_to_shop(
|
|
shop_code: str,
|
|
product: ProductCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Add existing product to shop catalog with shop-specific settings (Protected)."""
|
|
# Get and verify shop (using existing dependency)
|
|
shop = get_user_shop(shop_code, current_user, db)
|
|
|
|
# Add product to shop
|
|
new_product = shop_service.add_product_to_shop(
|
|
db=db, shop=shop, product=product
|
|
)
|
|
|
|
# Return with product details
|
|
response = ProductResponse.model_validate(new_product)
|
|
response.marketplace_product = new_product.marketplace_product
|
|
return response
|
|
|
|
|
|
@router.get("/shop/{shop_code}/products")
|
|
def get_products(
|
|
shop_code: str,
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=1000),
|
|
active_only: bool = Query(True),
|
|
featured_only: bool = Query(False),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get products in shop catalog (Protected)."""
|
|
# Get shop
|
|
shop = shop_service.get_shop_by_code(
|
|
db=db, shop_code=shop_code, current_user=current_user
|
|
)
|
|
|
|
# Get shop products
|
|
vendor_products, total = shop_service.get_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 vp in vendor_products:
|
|
product_response = ProductResponse.model_validate(vp)
|
|
product_response.marketplace_product = vp.marketplace_product
|
|
products.append(product_response)
|
|
|
|
return {
|
|
"products": products,
|
|
"total": total,
|
|
"skip": skip,
|
|
"limit": limit,
|
|
"shop": ShopResponse.model_validate(shop),
|
|
}
|