Refactoring code for modular approach
This commit is contained in:
@@ -11,15 +11,12 @@ from models.database_models import User, Product, Stock
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
from app.services.product_service import product_service
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize processors
|
||||
gtin_processor = GTINProcessor()
|
||||
price_processor = PriceProcessor()
|
||||
|
||||
|
||||
# Enhanced Product Routes with Marketplace Support
|
||||
@router.get("/products", response_model=ProductListResponse)
|
||||
@@ -37,41 +34,28 @@ def get_products(
|
||||
):
|
||||
"""Get products with advanced filtering including marketplace and shop (Protected)"""
|
||||
|
||||
query = db.query(Product)
|
||||
|
||||
# Apply filters
|
||||
if brand:
|
||||
query = query.filter(Product.brand.ilike(f"%{brand}%"))
|
||||
if category:
|
||||
query = query.filter(Product.google_product_category.ilike(f"%{category}%"))
|
||||
if availability:
|
||||
query = query.filter(Product.availability == availability)
|
||||
if marketplace:
|
||||
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
|
||||
if search:
|
||||
# Search in title, description, and marketplace
|
||||
search_term = f"%{search}%"
|
||||
query = query.filter(
|
||||
(Product.title.ilike(search_term)) |
|
||||
(Product.description.ilike(search_term)) |
|
||||
(Product.marketplace.ilike(search_term)) |
|
||||
(Product.shop_name.ilike(search_term))
|
||||
try:
|
||||
products, total = product_service.get_products_with_filters(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
brand=brand,
|
||||
category=category,
|
||||
availability=availability,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name,
|
||||
search=search
|
||||
)
|
||||
|
||||
# Get total count for pagination
|
||||
total = query.count()
|
||||
|
||||
# Apply pagination
|
||||
products = query.offset(skip).limit(limit).all()
|
||||
|
||||
return ProductListResponse(
|
||||
products=products,
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
)
|
||||
return ProductListResponse(
|
||||
products=products,
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting products: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.post("/products", response_model=ProductResponse)
|
||||
@@ -82,68 +66,54 @@ def create_product(
|
||||
):
|
||||
"""Create a new product with validation and marketplace support (Protected)"""
|
||||
|
||||
# Check if product_id already exists
|
||||
existing = db.query(Product).filter(Product.product_id == product.product_id).first()
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Product with this ID already exists")
|
||||
try:
|
||||
# Check if product_id already exists
|
||||
existing = product_service.get_product_by_id(db, product.product_id)
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Product with this ID already exists")
|
||||
|
||||
# Process and validate GTIN if provided
|
||||
if product.gtin:
|
||||
normalized_gtin = gtin_processor.normalize(product.gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
product.gtin = normalized_gtin
|
||||
db_product = product_service.create_product(db, product)
|
||||
|
||||
# Process price if provided
|
||||
if product.price:
|
||||
parsed_price, currency = price_processor.parse_price_currency(product.price)
|
||||
if parsed_price:
|
||||
product.price = parsed_price
|
||||
product.currency = currency
|
||||
logger.info(
|
||||
f"Created product {db_product.product_id} for marketplace {db_product.marketplace}, "
|
||||
f"shop {db_product.shop_name}")
|
||||
return db_product
|
||||
|
||||
# Set default marketplace if not provided
|
||||
if not product.marketplace:
|
||||
product.marketplace = "Letzshop"
|
||||
|
||||
db_product = Product(**product.dict())
|
||||
db.add(db_product)
|
||||
db.commit()
|
||||
db.refresh(db_product)
|
||||
|
||||
logger.info(
|
||||
f"Created product {db_product.product_id} for marketplace {db_product.marketplace}, "
|
||||
f"shop {db_product.shop_name}")
|
||||
return db_product
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating product: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.get("/products/{product_id}", response_model=ProductDetailResponse)
|
||||
def get_product(product_id: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
def get_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get product with stock information (Protected)"""
|
||||
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
try:
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
# Get stock information if GTIN exists
|
||||
stock_info = None
|
||||
if product.gtin:
|
||||
stock_entries = db.query(Stock).filter(Stock.gtin == product.gtin).all()
|
||||
if stock_entries:
|
||||
total_quantity = sum(entry.quantity for entry in stock_entries)
|
||||
locations = [
|
||||
StockLocationResponse(location=entry.location, quantity=entry.quantity)
|
||||
for entry in stock_entries
|
||||
]
|
||||
stock_info = StockSummaryResponse(
|
||||
gtin=product.gtin,
|
||||
total_quantity=total_quantity,
|
||||
locations=locations
|
||||
)
|
||||
# Get stock information if GTIN exists
|
||||
stock_info = None
|
||||
if product.gtin:
|
||||
stock_info = product_service.get_stock_info(db, product.gtin)
|
||||
|
||||
return ProductDetailResponse(
|
||||
product=product,
|
||||
stock_info=stock_info
|
||||
)
|
||||
return ProductDetailResponse(
|
||||
product=product,
|
||||
stock_info=stock_info
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting product {product_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.put("/products/{product_id}", response_model=ProductResponse)
|
||||
@@ -155,35 +125,21 @@ def update_product(
|
||||
):
|
||||
"""Update product with validation and marketplace support (Protected)"""
|
||||
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
try:
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
# Update fields
|
||||
update_data = product_update.dict(exclude_unset=True)
|
||||
updated_product = product_service.update_product(db, product_id, product_update)
|
||||
return updated_product
|
||||
|
||||
# Validate GTIN if being updated
|
||||
if "gtin" in update_data and update_data["gtin"]:
|
||||
normalized_gtin = gtin_processor.normalize(update_data["gtin"])
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
update_data["gtin"] = normalized_gtin
|
||||
|
||||
# Process price if being updated
|
||||
if "price" in update_data and update_data["price"]:
|
||||
parsed_price, currency = price_processor.parse_price_currency(update_data["price"])
|
||||
if parsed_price:
|
||||
update_data["price"] = parsed_price
|
||||
update_data["currency"] = currency
|
||||
|
||||
for key, value in update_data.items():
|
||||
setattr(product, key, value)
|
||||
|
||||
product.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(product)
|
||||
|
||||
return product
|
||||
except HTTPException:
|
||||
raise
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating product {product_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.delete("/products/{product_id}")
|
||||
@@ -194,18 +150,21 @@ def delete_product(
|
||||
):
|
||||
"""Delete product and associated stock (Protected)"""
|
||||
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
try:
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
# Delete associated stock entries if GTIN exists
|
||||
if product.gtin:
|
||||
db.query(Stock).filter(Stock.gtin == product.gtin).delete()
|
||||
product_service.delete_product(db, product_id)
|
||||
|
||||
db.delete(product)
|
||||
db.commit()
|
||||
return {"message": "Product and associated stock deleted successfully"}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting product {product_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
return {"message": "Product and associated stock deleted successfully"}
|
||||
|
||||
# Export with streaming for large datasets (Protected)
|
||||
@router.get("/export-csv")
|
||||
@@ -217,45 +176,27 @@ async def export_csv(
|
||||
):
|
||||
"""Export products as CSV with streaming and marketplace filtering (Protected)"""
|
||||
|
||||
def generate_csv():
|
||||
# Stream CSV generation for memory efficiency
|
||||
yield "product_id,title,description,link,image_link,availability,price,currency,brand,gtin,marketplace,shop_name\n"
|
||||
try:
|
||||
def generate_csv():
|
||||
return product_service.generate_csv_export(
|
||||
db=db,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name
|
||||
)
|
||||
|
||||
batch_size = 1000
|
||||
offset = 0
|
||||
filename = "products_export"
|
||||
if marketplace:
|
||||
filename += f"_{marketplace}"
|
||||
if shop_name:
|
||||
filename += f"_{shop_name}"
|
||||
filename += ".csv"
|
||||
|
||||
while True:
|
||||
query = db.query(Product)
|
||||
return StreamingResponse(
|
||||
generate_csv(),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
)
|
||||
|
||||
# Apply marketplace filters
|
||||
if marketplace:
|
||||
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
|
||||
|
||||
products = query.offset(offset).limit(batch_size).all()
|
||||
if not products:
|
||||
break
|
||||
|
||||
for product in products:
|
||||
# Create CSV row with marketplace fields
|
||||
row = (f'"{product.product_id}","{product.title or ""}","{product.description or ""}",'
|
||||
f'"{product.link or ""}","{product.image_link or ""}","{product.availability or ""}",'
|
||||
f'"{product.price or ""}","{product.currency or ""}","{product.brand or ""}",'
|
||||
f'"{product.gtin or ""}","{product.marketplace or ""}","{product.shop_name or ""}"\n')
|
||||
yield row
|
||||
|
||||
offset += batch_size
|
||||
|
||||
filename = "products_export"
|
||||
if marketplace:
|
||||
filename += f"_{marketplace}"
|
||||
if shop_name:
|
||||
filename += f"_{shop_name}"
|
||||
filename += ".csv"
|
||||
|
||||
return StreamingResponse(
|
||||
generate_csv(),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error exporting CSV: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
Reference in New Issue
Block a user