From 34ac25b929e1023a3612730e23ce7ed89d17dc8d Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 14 Apr 2025 15:49:47 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/create-fruit.post.py via AI for Fruit --- .../20250414_154929_3b5cba15_update_fruit.py | 29 +++++++++++ endpoints/create-fruit.post.py | 16 +++++++ helpers/fruit_helpers.py | 48 +++++++++++++++++++ models/fruit.py | 13 +++++ schemas/fruit.py | 21 ++++++++ 5 files changed, 127 insertions(+) create mode 100644 alembic/versions/20250414_154929_3b5cba15_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_154929_3b5cba15_update_fruit.py b/alembic/versions/20250414_154929_3b5cba15_update_fruit.py new file mode 100644 index 0000000..e25935c --- /dev/null +++ b/alembic/versions/20250414_154929_3b5cba15_update_fruit.py @@ -0,0 +1,29 @@ +"""create table for fruits +Revision ID: 3a7d6c1e86b4 +Revises: 0001 +Create Date: 2023-05-25 11:36:06.526501 +""" +from alembic import op +import sqlalchemy as sa +import uuid + +# revision identifiers, used by Alembic. +revision = '3a7d6c1e86b4' +down_revision = '0001' +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('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()), + sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now()) + ) + +def downgrade(): + table_name = "fruits" + op.drop_table(table_name) \ No newline at end of file diff --git a/endpoints/create-fruit.post.py b/endpoints/create-fruit.post.py index e69de29..69734b6 100644 --- a/endpoints/create-fruit.post.py +++ b/endpoints/create-fruit.post.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, status +from schemas.fruit import FruitCreate, FruitSchema +from helpers.fruit_helpers import create_fruit +from sqlalchemy.orm import Session +from fastapi import Depends +from core.database import get_db + +router = APIRouter() + +@router.post("/create-fruit", status_code=status.HTTP_201_CREATED, response_model=FruitSchema) +async def create_new_fruit( + fruit_data: FruitCreate, + db: Session = Depends(get_db) +): + new_fruit = create_fruit(db=db, fruit_data=fruit_data) + return new_fruit \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py new file mode 100644 index 0000000..bd27b1b --- /dev/null +++ b/helpers/fruit_helpers.py @@ -0,0 +1,48 @@ +from typing import List, Optional +from sqlalchemy.orm import Session +from sqlalchemy import func + +from models.fruit import Fruit +from schemas.fruit import FruitCreate + +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 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(name=fruit_data.name) + db.add(db_fruit) + db.commit() + db.refresh(db_fruit) + return db_fruit + +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(func.lower(Fruit.name) == name.lower()).first() \ No newline at end of file diff --git a/models/fruit.py b/models/fruit.py new file mode 100644 index 0000000..0137e5a --- /dev/null +++ b/models/fruit.py @@ -0,0 +1,13 @@ +from sqlalchemy import Column, String +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) + created_at = Column(func.now()) + updated_at = Column(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..11a1148 --- /dev/null +++ b/schemas/fruit.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +import uuid + +class FruitBase(BaseModel): + name: str = Field(..., description="Name of the fruit") + +class FruitCreate(FruitBase): + pass + +class FruitUpdate(FruitBase): + name: Optional[str] = Field(None, description="Name of the fruit") + +class FruitSchema(FruitBase): + id: uuid.UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file