From ee904638414f8d1aca98cc94cc931cd3faa36938 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 14 Apr 2025 20:50:10 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/fruits.get.py via AI for Fruit and updated requirements.txt with auto lint fixes --- .../20250414_204940_729e8b60_update_fruit.py | 34 +++++ endpoints/fruits.get.py | 12 ++ helpers/fruit_helpers.py | 119 ++++++++++++++++++ models/fruit.py | 14 +++ schemas/fruit.py | 23 ++++ 5 files changed, 202 insertions(+) create mode 100644 alembic/versions/20250414_204940_729e8b60_update_fruit.py create mode 100644 helpers/fruit_helpers.py create mode 100644 models/fruit.py create mode 100644 schemas/fruit.py diff --git a/alembic/versions/20250414_204940_729e8b60_update_fruit.py b/alembic/versions/20250414_204940_729e8b60_update_fruit.py new file mode 100644 index 0000000..94db1c1 --- /dev/null +++ b/alembic/versions/20250414_204940_729e8b60_update_fruit.py @@ -0,0 +1,34 @@ +"""create table for fruits +Revision ID: 2c8e7d9f9e4b +Revises: 6d5cd4bae0c6 +Create Date: 2023-05-25 12:34:56 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '2c8e7d9f9e4b' +down_revision = '6d5cd4bae0c6' +branch_labels = None +depends_on = None + +def upgrade(): + table_name = "fruits" + + op.create_table( + table_name, + sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())), + sa.Column('name', sa.String(), nullable=False, unique=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('created_at', sa.DateTime(), server_default=func.now()), + sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()) + ) + op.create_index(op.f('ix_fruits_name'), table_name, ['name'], unique=True) + +def downgrade(): + table_name = "fruits" + op.drop_index(op.f('ix_fruits_name'), table_name=table_name) + op.drop_table(table_name) \ No newline at end of file diff --git a/endpoints/fruits.get.py b/endpoints/fruits.get.py index e69de29..a74b38d 100644 --- a/endpoints/fruits.get.py +++ b/endpoints/fruits.get.py @@ -0,0 +1,12 @@ +from fastapi import APIRouter, status, Depends +from typing import List +from schemas.fruit import FruitSchema +from helpers.fruit_helpers import get_all_fruits +from db import get_db, Session + +router = APIRouter() + +@router.get("/fruits", status_code=status.HTTP_200_OK, response_model=List[FruitSchema]) +def get_fruits(db: Session = Depends(get_db)): + fruits = get_all_fruits(db) + return fruits \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py new file mode 100644 index 0000000..b6189c5 --- /dev/null +++ b/helpers/fruit_helpers.py @@ -0,0 +1,119 @@ +import uuid +from typing import List, Optional +from sqlalchemy.orm import Session +from sqlalchemy import or_ + +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 get_fruits_by_name(db: Session, name: str) -> List[Fruit]: + """ + Retrieves fruits by their name (case-insensitive). + + Args: + db (Session): The database session. + name (str): The name of the fruits to retrieve. + + Returns: + List[Fruit]: A list of fruit objects matching the name. + """ + return db.query(Fruit).filter(Fruit.name.ilike(f"%{name}%")).all() + +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 + + for field, value in fruit_data.dict(exclude_unset=True).items(): + setattr(db_fruit, field, value) + + 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 successfully 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, query: str) -> List[Fruit]: + """ + Searches for fruits by name or description. + + Args: + db (Session): The database session. + query (str): The search query string. + + Returns: + List[Fruit]: A list of fruit objects matching the search query. + """ + return db.query(Fruit).filter( + or_(Fruit.name.ilike(f"%{query}%"), Fruit.description.ilike(f"%{query}%")) + ).all() \ No newline at end of file diff --git a/models/fruit.py b/models/fruit.py new file mode 100644 index 0000000..1385452 --- /dev/null +++ b/models/fruit.py @@ -0,0 +1,14 @@ +from sqlalchemy import Column, String, DateTime +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class Fruit(Base): + __tablename__ = "fruits" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False, unique=True, index=True) + description = Column(String, nullable=True) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/schemas/fruit.py b/schemas/fruit.py new file mode 100644 index 0000000..e4e730a --- /dev/null +++ b/schemas/fruit.py @@ -0,0 +1,23 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +from uuid import UUID + +class FruitBase(BaseModel): + name: str = Field(..., description="Name of the fruit") + description: Optional[str] = Field(None, description="Description of the fruit") + +class FruitCreate(FruitBase): + pass + +class FruitUpdate(FruitBase): + name: Optional[str] = Field(None, description="Name of the fruit") + description: Optional[str] = Field(None, description="Description of the fruit") + +class FruitSchema(FruitBase): + id: UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file