code quality run
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import HTTPException
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import List, Optional, Tuple, Dict, Any
|
||||
|
||||
from models.database_models import User, Shop, Product, ShopProduct
|
||||
from models.api_models import ShopCreate, ShopProductCreate
|
||||
from models.database_models import Product, Shop, ShopProduct, User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -14,7 +15,9 @@ logger = logging.getLogger(__name__)
|
||||
class ShopService:
|
||||
"""Service class for shop operations following the application's service pattern"""
|
||||
|
||||
def create_shop(self, db: Session, shop_data: ShopCreate, current_user: User) -> Shop:
|
||||
def create_shop(
|
||||
self, db: Session, shop_data: ShopCreate, current_user: User
|
||||
) -> Shop:
|
||||
"""
|
||||
Create a new shop
|
||||
|
||||
@@ -33,39 +36,43 @@ class ShopService:
|
||||
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()
|
||||
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 with uppercase code
|
||||
shop_dict = shop_data.model_dump() # Fixed deprecated .dict() method
|
||||
shop_dict['shop_code'] = normalized_shop_code # Store as uppercase
|
||||
shop_dict["shop_code"] = normalized_shop_code # Store as uppercase
|
||||
|
||||
new_shop = Shop(
|
||||
**shop_dict,
|
||||
owner_id=current_user.id,
|
||||
is_active=True,
|
||||
is_verified=(current_user.role == "admin")
|
||||
is_verified=(current_user.role == "admin"),
|
||||
)
|
||||
|
||||
db.add(new_shop)
|
||||
db.commit()
|
||||
db.refresh(new_shop)
|
||||
|
||||
logger.info(f"New shop created: {new_shop.shop_code} by {current_user.username}")
|
||||
logger.info(
|
||||
f"New shop created: {new_shop.shop_code} by {current_user.username}"
|
||||
)
|
||||
return new_shop
|
||||
|
||||
def get_shops(
|
||||
self,
|
||||
db: Session,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
verified_only: bool = False
|
||||
self,
|
||||
db: Session,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
verified_only: bool = False,
|
||||
) -> Tuple[List[Shop], int]:
|
||||
"""
|
||||
Get shops with filtering
|
||||
@@ -86,8 +93,8 @@ class ShopService:
|
||||
# Non-admin users can only see active and verified shops, plus their own
|
||||
if current_user.role != "admin":
|
||||
query = query.filter(
|
||||
(Shop.is_active == True) &
|
||||
((Shop.is_verified == True) | (Shop.owner_id == current_user.id))
|
||||
(Shop.is_active == True)
|
||||
& ((Shop.is_verified == True) | (Shop.owner_id == current_user.id))
|
||||
)
|
||||
else:
|
||||
# Admin can apply filters
|
||||
@@ -117,22 +124,25 @@ class ShopService:
|
||||
HTTPException: If shop not found or access denied
|
||||
"""
|
||||
# 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()
|
||||
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")
|
||||
|
||||
# Non-admin users can only see active verified shops or their own shops
|
||||
if current_user.role != "admin":
|
||||
if not shop.is_active or (not shop.is_verified and shop.owner_id != current_user.id):
|
||||
if not shop.is_active or (
|
||||
not shop.is_verified and shop.owner_id != current_user.id
|
||||
):
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
return shop
|
||||
|
||||
def add_product_to_shop(
|
||||
self,
|
||||
db: Session,
|
||||
shop: Shop,
|
||||
shop_product: ShopProductCreate
|
||||
self, db: Session, shop: Shop, shop_product: ShopProductCreate
|
||||
) -> ShopProduct:
|
||||
"""
|
||||
Add existing product to shop catalog with shop-specific settings
|
||||
@@ -149,24 +159,35 @@ class ShopService:
|
||||
HTTPException: If product not found or already in shop
|
||||
"""
|
||||
# Check if product exists
|
||||
product = db.query(Product).filter(Product.product_id == shop_product.product_id).first()
|
||||
product = (
|
||||
db.query(Product)
|
||||
.filter(Product.product_id == shop_product.product_id)
|
||||
.first()
|
||||
)
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found in marketplace catalog")
|
||||
raise HTTPException(
|
||||
status_code=404, detail="Product not found in marketplace catalog"
|
||||
)
|
||||
|
||||
# Check if product already in shop
|
||||
existing_shop_product = db.query(ShopProduct).filter(
|
||||
ShopProduct.shop_id == shop.id,
|
||||
ShopProduct.product_id == product.id
|
||||
).first()
|
||||
existing_shop_product = (
|
||||
db.query(ShopProduct)
|
||||
.filter(
|
||||
ShopProduct.shop_id == shop.id, ShopProduct.product_id == product.id
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_shop_product:
|
||||
raise HTTPException(status_code=400, detail="Product already in shop catalog")
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Product already in shop catalog"
|
||||
)
|
||||
|
||||
# Create shop-product association
|
||||
new_shop_product = ShopProduct(
|
||||
shop_id=shop.id,
|
||||
product_id=product.id,
|
||||
**shop_product.model_dump(exclude={'product_id'})
|
||||
**shop_product.model_dump(exclude={"product_id"}),
|
||||
)
|
||||
|
||||
db.add(new_shop_product)
|
||||
@@ -180,14 +201,14 @@ class ShopService:
|
||||
return new_shop_product
|
||||
|
||||
def get_shop_products(
|
||||
self,
|
||||
db: Session,
|
||||
shop: Shop,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
featured_only: bool = False
|
||||
self,
|
||||
db: Session,
|
||||
shop: Shop,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
featured_only: bool = False,
|
||||
) -> Tuple[List[ShopProduct], int]:
|
||||
"""
|
||||
Get products in shop catalog with filtering
|
||||
@@ -239,10 +260,14 @@ class ShopService:
|
||||
|
||||
def product_in_shop(self, db: Session, shop_id: int, product_id: int) -> bool:
|
||||
"""Check if product is already in shop"""
|
||||
return db.query(ShopProduct).filter(
|
||||
ShopProduct.shop_id == shop_id,
|
||||
ShopProduct.product_id == product_id
|
||||
).first() is not None
|
||||
return (
|
||||
db.query(ShopProduct)
|
||||
.filter(
|
||||
ShopProduct.shop_id == shop_id, ShopProduct.product_id == product_id
|
||||
)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
def is_shop_owner(self, shop: Shop, user: User) -> bool:
|
||||
"""Check if user is shop owner"""
|
||||
|
||||
Reference in New Issue
Block a user