From d66406b2aede67c011e694368cc087f46fa89013 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 29 Apr 2025 18:10:43 +0000 Subject: [PATCH] feat: add new endpoint to retrieve apples by name --- .../20250429_181021_ca4393d0_update_apple.py | 30 ++++++ endpoints/rest.get.py | 15 +++ helpers/apple_helpers.py | 92 +++++++++++++++++++ models/apple.py | 13 +++ schemas/apple.py | 21 +++++ 5 files changed, 171 insertions(+) create mode 100644 alembic/versions/20250429_181021_ca4393d0_update_apple.py create mode 100644 helpers/apple_helpers.py create mode 100644 models/apple.py create mode 100644 schemas/apple.py diff --git a/alembic/versions/20250429_181021_ca4393d0_update_apple.py b/alembic/versions/20250429_181021_ca4393d0_update_apple.py new file mode 100644 index 0000000..550e769 --- /dev/null +++ b/alembic/versions/20250429_181021_ca4393d0_update_apple.py @@ -0,0 +1,30 @@ +"""create table for apples + +Revision ID: 5d7c16b9e3f4 +Revises: 9a7d63f4b8f2 +Create Date: 2023-05-16 12:34:56 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '5d7c16b9e3f4' +down_revision = '9a7d63f4b8f2' +branch_labels = None +depends_on = None + +def upgrade(): + op.create_table( + 'apples', + 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(), server_default=func.now()), + sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()), + sa.Index('ix_apples_name', 'name', unique=True) + ) + +def downgrade(): + op.drop_table('apples') \ No newline at end of file diff --git a/endpoints/rest.get.py b/endpoints/rest.get.py index e69de29..401c9ab 100644 --- a/endpoints/rest.get.py +++ b/endpoints/rest.get.py @@ -0,0 +1,15 @@ +from fastapi import APIRouter, Depends +from typing import List +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.apple import AppleSchema +from helpers.apple_helpers import get_apple_by_name + +router = APIRouter() + +@router.get("/rest", status_code=200, response_model=List[AppleSchema]) +async def get_apples_by_name(name: str, db: Session = Depends(get_db)): + apples = get_apple_by_name(db, name) + if not apples: + return [] + return [apples] \ No newline at end of file diff --git a/helpers/apple_helpers.py b/helpers/apple_helpers.py new file mode 100644 index 0000000..b98f918 --- /dev/null +++ b/helpers/apple_helpers.py @@ -0,0 +1,92 @@ +from typing import List, Optional +from sqlalchemy.orm import Session +from models.apple import Apple +from schemas.apple import AppleSchema, AppleCreate, AppleUpdate +from uuid import UUID + +def get_all_apples(db: Session) -> List[AppleSchema]: + """ + Retrieves all apples from the database. + + Args: + db (Session): The database session. + + Returns: + List[AppleSchema]: A list of all apple objects. + """ + apples = db.query(Apple).all() + return [AppleSchema.from_orm(apple) for apple in apples] + +def get_apple_by_name(db: Session, name: str) -> Optional[AppleSchema]: + """ + Retrieves an apple from the database by its name. + + Args: + db (Session): The database session. + name (str): The name of the apple to retrieve. + + Returns: + Optional[AppleSchema]: The apple object if found, otherwise None. + """ + apple = db.query(Apple).filter(Apple.name == name).first() + return AppleSchema.from_orm(apple) if apple else None + +def create_apple(db: Session, apple_data: AppleCreate) -> AppleSchema: + """ + Creates a new apple in the database. + + Args: + db (Session): The database session. + apple_data (AppleCreate): The data for the apple to create. + + Returns: + AppleSchema: The newly created apple object. + """ + db_apple = Apple(**apple_data.dict()) + db.add(db_apple) + db.commit() + db.refresh(db_apple) + return AppleSchema.from_orm(db_apple) + +def update_apple(db: Session, apple_id: UUID, apple_data: AppleUpdate) -> Optional[AppleSchema]: + """ + Updates an existing apple in the database. + + Args: + db (Session): The database session. + apple_id (UUID): The ID of the apple to update. + apple_data (AppleUpdate): The updated data for the apple. + + Returns: + Optional[AppleSchema]: The updated apple object if found, otherwise None. + """ + db_apple = db.query(Apple).filter(Apple.id == apple_id).first() + if not db_apple: + return None + + update_data = apple_data.dict(exclude_unset=True) + for key, value in update_data.items(): + setattr(db_apple, key, value) + + db.commit() + db.refresh(db_apple) + return AppleSchema.from_orm(db_apple) + +def delete_apple(db: Session, apple_id: UUID) -> bool: + """ + Deletes an apple from the database. + + Args: + db (Session): The database session. + apple_id (UUID): The ID of the apple to delete. + + Returns: + bool: True if the apple was successfully deleted, False otherwise. + """ + db_apple = db.query(Apple).filter(Apple.id == apple_id).first() + if not db_apple: + return False + + db.delete(db_apple) + db.commit() + return True \ No newline at end of file diff --git a/models/apple.py b/models/apple.py new file mode 100644 index 0000000..4d762a4 --- /dev/null +++ b/models/apple.py @@ -0,0 +1,13 @@ +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 Apple(Base): + __tablename__ = "apples" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False, unique=True, index=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/apple.py b/schemas/apple.py new file mode 100644 index 0000000..8296583 --- /dev/null +++ b/schemas/apple.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +from uuid import UUID + +class AppleBase(BaseModel): + name: str = Field(..., description="Name of the apple") + +class AppleCreate(AppleBase): + pass + +class AppleUpdate(AppleBase): + name: Optional[str] = Field(None, description="Name of the apple") + +class AppleSchema(AppleBase): + id: UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file