from typing import List, Optional from uuid import UUID from sqlalchemy.orm import Session from sqlalchemy import and_ from models.fruit import Fruit from schemas.fruit import FruitCreate, FruitUpdate def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]: """ Retrieves all fruits from the database with optional pagination and active filtering. Returns full fruit objects including color and ripeness status. Args: db (Session): The database session skip (int): Number of records to skip limit (int): Maximum number of records to return active_only (bool): If True, returns only active fruits Returns: List[Fruit]: List of fruit objects """ query = db.query(Fruit) if active_only: query = query.filter(Fruit.is_active) return query.offset(skip).limit(limit).all() def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]: """ Retrieves a single fruit by its ID. Args: db (Session): The database session fruit_id (UUID): The ID of the fruit to retrieve Returns: Optional[Fruit]: The fruit object if found, otherwise None """ return db.query(Fruit).filter(Fruit.id == fruit_id).first() def get_fruit_by_name(db: Session, name: str) -> Optional[Fruit]: """ Retrieves a single fruit by its name. Args: db (Session): The database session name (str): The name of the fruit to retrieve Returns: Optional[Fruit]: The fruit object if found, otherwise None """ return db.query(Fruit).filter(Fruit.name == name).first() def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit: """ Creates a new fruit in the database. Args: db (Session): The database session fruit_data (FruitCreate): The data for the fruit to create, including color and ripeness Returns: Fruit: The newly created fruit object """ db_fruit = Fruit(**fruit_data.dict()) db.add(db_fruit) db.commit() db.refresh(db_fruit) return db_fruit def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Optional[Fruit]: """ Updates an existing fruit in the database. Args: db (Session): The database session fruit_id (UUID): The ID of the fruit to update fruit_data (FruitUpdate): The update data for the fruit, including color and ripeness Returns: Optional[Fruit]: The updated fruit object if found, otherwise None """ db_fruit = get_fruit_by_id(db, fruit_id) if not db_fruit: return None update_data = fruit_data.dict(exclude_unset=True) for field, value in update_data.items(): setattr(db_fruit, field, value) db.commit() db.refresh(db_fruit) return db_fruit def delete_fruit(db: Session, fruit_id: UUID) -> bool: """ Deletes a fruit from the database. Args: db (Session): The database session fruit_id (UUID): The ID of the fruit to delete Returns: bool: True if the fruit was deleted, False if not found """ db_fruit = get_fruit_by_id(db, fruit_id) if not db_fruit: return False db.delete(db_fruit) db.commit() return True def soft_delete_fruit(db: Session, fruit_id: UUID) -> bool: """ Soft deletes a fruit by setting is_active to False. Args: db (Session): The database session fruit_id (UUID): The ID of the fruit to soft delete Returns: bool: True if the fruit was soft deleted, False if not found """ db_fruit = get_fruit_by_id(db, fruit_id) if not db_fruit: return False db_fruit.is_active = False db.commit() return True def search_fruits(db: Session, search_term: str, active_only: bool = True) -> List[Fruit]: """ Searches fruits by name or description. Args: db (Session): The database session search_term (str): The search term to filter by active_only (bool): If True, returns only active fruits Returns: List[Fruit]: List of matching fruit objects """ filters = [ Fruit.name.ilike(f"%{search_term}%") | Fruit.description.ilike(f"%{search_term}%") ] if active_only: filters.append(Fruit.is_active) return db.query(Fruit).filter(and_(*filters)).all()