Refactoring code for modular approach
This commit is contained in:
0
app/services/__init__.py
Normal file
0
app/services/__init__.py
Normal file
0
app/services/import_service.py
Normal file
0
app/services/import_service.py
Normal file
79
app/services/product_service.py
Normal file
79
app/services/product_service.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from models.database_models import Product
|
||||
from models.api_models import ProductCreate
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
from typing import Optional, List
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProductService:
|
||||
def __init__(self):
|
||||
self.gtin_processor = GTINProcessor()
|
||||
self.price_processor = PriceProcessor()
|
||||
|
||||
def create_product(self, db: Session, product_data: ProductCreate) -> Product:
|
||||
"""Create a new product with validation"""
|
||||
# Process and validate GTIN if provided
|
||||
if product_data.gtin:
|
||||
normalized_gtin = self.gtin_processor.normalize(product_data.gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("Invalid GTIN format")
|
||||
product_data.gtin = normalized_gtin
|
||||
|
||||
# Process price if provided
|
||||
if product_data.price:
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(product_data.price)
|
||||
if parsed_price:
|
||||
product_data.price = parsed_price
|
||||
product_data.currency = currency
|
||||
|
||||
# Set default marketplace if not provided
|
||||
if not product_data.marketplace:
|
||||
product_data.marketplace = "Letzshop"
|
||||
|
||||
db_product = Product(**product_data.dict())
|
||||
db.add(db_product)
|
||||
db.commit()
|
||||
db.refresh(db_product)
|
||||
|
||||
logger.info(f"Created product {db_product.product_id}")
|
||||
return db_product
|
||||
|
||||
def get_products_with_filters(
|
||||
self,
|
||||
db: Session,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
brand: Optional[str] = None,
|
||||
category: Optional[str] = None,
|
||||
marketplace: Optional[str] = None,
|
||||
search: Optional[str] = None
|
||||
) -> tuple[List[Product], int]:
|
||||
"""Get products with filtering and pagination"""
|
||||
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 marketplace:
|
||||
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
|
||||
if search:
|
||||
search_term = f"%{search}%"
|
||||
query = query.filter(
|
||||
(Product.title.ilike(search_term)) |
|
||||
(Product.description.ilike(search_term)) |
|
||||
(Product.marketplace.ilike(search_term))
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
products = query.offset(skip).limit(limit).all()
|
||||
|
||||
return products, total
|
||||
|
||||
|
||||
# Create service instance
|
||||
product_service = ProductService()
|
||||
0
app/services/stock_service.py
Normal file
0
app/services/stock_service.py
Normal file
Reference in New Issue
Block a user