Application fully migrated to modular approach
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import HTTPException
|
||||
from datetime import datetime
|
||||
@@ -28,17 +29,26 @@ class ShopService:
|
||||
Raises:
|
||||
HTTPException: If shop code already exists
|
||||
"""
|
||||
# Check if shop code already exists
|
||||
existing_shop = db.query(Shop).filter(Shop.shop_code == shop_data.shop_code).first()
|
||||
# Normalize shop code to uppercase
|
||||
normalized_shop_code = shop_data.shop_code.upper()
|
||||
|
||||
# Check if shop code already exists (case-insensitive check against existing data)
|
||||
existing_shop = db.query(Shop).filter(
|
||||
func.upper(Shop.shop_code) == normalized_shop_code
|
||||
).first()
|
||||
|
||||
if existing_shop:
|
||||
raise HTTPException(status_code=400, detail="Shop code already exists")
|
||||
|
||||
# Create shop
|
||||
# Create shop with uppercase code
|
||||
shop_dict = shop_data.model_dump() # Fixed deprecated .dict() method
|
||||
shop_dict['shop_code'] = normalized_shop_code # Store as uppercase
|
||||
|
||||
new_shop = Shop(
|
||||
**shop_data.dict(),
|
||||
**shop_dict,
|
||||
owner_id=current_user.id,
|
||||
is_active=True,
|
||||
is_verified=(current_user.role == "admin") # Auto-verify if admin creates shop
|
||||
is_verified=(current_user.role == "admin")
|
||||
)
|
||||
|
||||
db.add(new_shop)
|
||||
@@ -106,7 +116,8 @@ class ShopService:
|
||||
Raises:
|
||||
HTTPException: If shop not found or access denied
|
||||
"""
|
||||
shop = db.query(Shop).filter(Shop.shop_code == shop_code.upper()).first()
|
||||
# Explicit type hint to help type checker shop: Optional[Shop]
|
||||
shop: Optional[Shop] = db.query(Shop).filter(func.upper(Shop.shop_code) == shop_code.upper()).first()
|
||||
if not shop:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
@@ -155,7 +166,7 @@ class ShopService:
|
||||
new_shop_product = ShopProduct(
|
||||
shop_id=shop.id,
|
||||
product_id=product.id,
|
||||
**shop_product.dict(exclude={'product_id'})
|
||||
**shop_product.model_dump(exclude={'product_id'})
|
||||
)
|
||||
|
||||
db.add(new_shop_product)
|
||||
|
||||
Reference in New Issue
Block a user