Add helper functions for Shoe
This commit is contained in:
parent
33e66b5a33
commit
0c11d38f8b
98
helpers/shoe_helpers.py
Normal file
98
helpers/shoe_helpers.py
Normal file
@ -0,0 +1,98 @@
|
||||
from typing import List, Optional, Dict, Union
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import desc
|
||||
from models.shoe import Shoe
|
||||
from schemas.shoe import ShoeCreate, ShoeUpdate
|
||||
|
||||
def get_shoes_by_name(db: Session, name: str) -> List[Shoe]:
|
||||
"""
|
||||
Get shoes by name with case-insensitive search.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
name: Name to search for
|
||||
|
||||
Returns:
|
||||
List of matching Shoe objects
|
||||
"""
|
||||
return db.query(Shoe).filter(Shoe.name.ilike(f'%{name}%')).all()
|
||||
|
||||
def get_shoes_by_brand_and_size(db: Session, brand: str, size: int) -> List[Shoe]:
|
||||
"""
|
||||
Get shoes filtered by brand and size.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
brand: Brand name to filter
|
||||
size: Shoe size to filter
|
||||
|
||||
Returns:
|
||||
List of matching Shoe objects
|
||||
"""
|
||||
return db.query(Shoe).filter(
|
||||
Shoe.brand == brand,
|
||||
Shoe.size == size
|
||||
).all()
|
||||
|
||||
def validate_shoe_data(shoe_data: Union[ShoeCreate, ShoeUpdate]) -> Dict[str, str]:
|
||||
"""
|
||||
Validate shoe data before creation/update.
|
||||
|
||||
Args:
|
||||
shoe_data: Shoe data to validate
|
||||
|
||||
Returns:
|
||||
Dict with error message if validation fails, empty dict if valid
|
||||
"""
|
||||
errors = {}
|
||||
|
||||
if shoe_data.size <= 0:
|
||||
errors["size"] = "Size must be positive"
|
||||
|
||||
if shoe_data.price and shoe_data.price < 0:
|
||||
errors["price"] = "Price cannot be negative"
|
||||
|
||||
if shoe_data.purchase_date and shoe_data.purchase_date > datetime.now():
|
||||
errors["purchase_date"] = "Purchase date cannot be in the future"
|
||||
|
||||
return errors
|
||||
|
||||
def get_shoes_by_condition(db: Session, condition: str) -> List[Shoe]:
|
||||
"""
|
||||
Get shoes filtered by condition.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
condition: Condition to filter by
|
||||
|
||||
Returns:
|
||||
List of matching Shoe objects
|
||||
"""
|
||||
return db.query(Shoe).filter(Shoe.condition == condition).all()
|
||||
|
||||
def calculate_collection_value(db: Session) -> Dict[str, Union[int, List[Dict[str, Any]]]]:
|
||||
"""
|
||||
Calculate total value of shoe collection and get price breakdown.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
Dict containing total value and price breakdown by brand
|
||||
"""
|
||||
shoes = db.query(Shoe).all()
|
||||
total_value = sum(shoe.price for shoe in shoes if shoe.price)
|
||||
|
||||
brand_breakdown = {}
|
||||
for shoe in shoes:
|
||||
if shoe.price:
|
||||
brand_breakdown[shoe.brand] = brand_breakdown.get(shoe.brand, 0) + shoe.price
|
||||
|
||||
return {
|
||||
"total_value": total_value,
|
||||
"brand_breakdown": [
|
||||
{"brand": brand, "value": value}
|
||||
for brand, value in brand_breakdown.items()
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user