Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, Depends, HTTPException, Query | |
| from sqlalchemy.orm import Session | |
| from typing import List, Dict, Any | |
| from app.db.base import get_db | |
| from app.services.external_catalog import ExternalCatalogService | |
| from app.schemas.auth import AuthResponse | |
| router = APIRouter(prefix="/external", tags=["External Data"]) | |
| async def search_external_products( | |
| query: str = Query(..., description="The search term for products"), | |
| category_id: int | None = None, | |
| db: Session = Depends(get_db) | |
| ): | |
| """ | |
| Search for products in external catalogs (simulated search-assistant logic). | |
| """ | |
| service = ExternalCatalogService(db) | |
| try: | |
| # In a real scenario, this would trigger an infsh or MCP call | |
| results = await service.fetch_real_products(query) | |
| return AuthResponse( | |
| isSuccess=True, | |
| value={"results": results, "query": query}, | |
| statusCode=200 | |
| ) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def import_product( | |
| external_data: Dict[str, Any], | |
| category_id: int, | |
| db: Session = Depends(get_db) | |
| ): | |
| """ | |
| Import a product from external JSON data. | |
| """ | |
| service = ExternalCatalogService(db) | |
| try: | |
| product = await service.create_product_from_external(external_data, category_id) | |
| db.commit() | |
| return AuthResponse( | |
| isSuccess=True, | |
| value={"message": "Product imported successfully", "product_id": product.id}, | |
| statusCode=201 | |
| ) | |
| except Exception as e: | |
| db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |