110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
from typing import List, Optional
|
|
import uuid
|
|
from sqlalchemy.orm import Session
|
|
from models.fruit import Fruit
|
|
from schemas.fruit import FruitCreate, FruitUpdate
|
|
|
|
def get_all_fruits(db: Session) -> List[Fruit]:
|
|
"""
|
|
Retrieves all fruits from the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
|
|
Returns:
|
|
List[Fruit]: A list of all fruit objects.
|
|
"""
|
|
return db.query(Fruit).all()
|
|
|
|
def get_fruit_by_id(db: Session, fruit_id: uuid.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 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.
|
|
|
|
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.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 updated data for the fruit.
|
|
|
|
Returns:
|
|
Optional[Fruit]: The updated fruit object if found, otherwise None.
|
|
"""
|
|
db_fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
|
if not db_fruit:
|
|
return None
|
|
|
|
update_data = fruit_data.dict(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_fruit, key, value)
|
|
|
|
db.add(db_fruit)
|
|
db.commit()
|
|
db.refresh(db_fruit)
|
|
return db_fruit
|
|
|
|
def delete_fruit(db: Session, fruit_id: uuid.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 otherwise.
|
|
"""
|
|
db_fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
|
if not db_fruit:
|
|
return False
|
|
|
|
db.delete(db_fruit)
|
|
db.commit()
|
|
return True
|
|
|
|
def search_fruits(db: Session, name: Optional[str] = None, description: Optional[str] = None) -> List[Fruit]:
|
|
"""
|
|
Searches for fruits based on name and/or description.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
name (Optional[str]): The name of the fruit to search for.
|
|
description (Optional[str]): The description of the fruit to search for.
|
|
|
|
Returns:
|
|
List[Fruit]: A list of fruit objects matching the search criteria.
|
|
"""
|
|
query = db.query(Fruit)
|
|
if name:
|
|
query = query.filter(Fruit.name.ilike(f"%{name}%"))
|
|
if description:
|
|
query = query.filter(Fruit.description.ilike(f"%{description}%"))
|
|
return query.all() |