From 106f98cd29d629e3040e9cdbfa0d2defc47fd505 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sun, 13 Apr 2025 20:01:44 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/create-fruit.post.py via AI for Fruit --- endpoints/create-fruit.post.py | 14 +++++ helpers/generic_helpers.py | 110 +++++++++++++++++++++++++++++++++ models/fruit.py | 15 +++++ 3 files changed, 139 insertions(+) create mode 100644 helpers/generic_helpers.py create mode 100644 models/fruit.py diff --git a/endpoints/create-fruit.post.py b/endpoints/create-fruit.post.py index e69de29..666ecce 100644 --- a/endpoints/create-fruit.post.py +++ b/endpoints/create-fruit.post.py @@ -0,0 +1,14 @@ +from fastapi import APIRouter, HTTPException, status +from typing import Dict, Any +from helpers.fruit_helpers import create_fruit + +router = APIRouter() + +@router.post("/create-fruit", status_code=status.HTTP_201_CREATED) +async def create_new_fruit(fruit_data: Dict[str, Any]): + """Create a new fruit entry""" + try: + new_fruit = create_fruit(fruit_data) + return new_fruit + except ValueError as e: + raise HTTPException(status_code=400, detail=str(e)) \ No newline at end of file diff --git a/helpers/generic_helpers.py b/helpers/generic_helpers.py new file mode 100644 index 0000000..4b6fc35 --- /dev/null +++ b/helpers/generic_helpers.py @@ -0,0 +1,110 @@ +from typing import Dict, Any, List, Optional +import uuid +from datetime import datetime + +# In-memory store for fruits +_fruits_store: List[Dict[str, Any]] = [] + +def validate_fruit_data(fruit_data: Dict[str, Any]) -> bool: + """ + Validates fruit data before creation. + + Args: + fruit_data (Dict[str, Any]): The fruit data to validate. + + Returns: + bool: True if data is valid, False otherwise. + """ + required_fields = ['name', 'color', 'shape'] + + if not all(field in fruit_data for field in required_fields): + return False + + if not isinstance(fruit_data['name'], str) or len(fruit_data['name'].strip()) == 0: + return False + + if not isinstance(fruit_data['color'], str) or len(fruit_data['color'].strip()) == 0: + return False + + if not isinstance(fruit_data['shape'], str) or len(fruit_data['shape'].strip()) == 0: + return False + + return True + +def create_fruit(fruit_data: Dict[str, Any]) -> Dict[str, Any]: + """ + Creates a new fruit entry in the in-memory store. + + Args: + fruit_data (Dict[str, Any]): The fruit data containing name, color, and shape. + + Returns: + Dict[str, Any]: The created fruit data with generated ID and timestamp. + + Raises: + ValueError: If the fruit data is invalid. + """ + if not validate_fruit_data(fruit_data): + raise ValueError("Invalid fruit data. Name, color, and shape are required.") + + new_fruit = { + "id": str(uuid.uuid4()), + "name": fruit_data["name"].strip(), + "color": fruit_data["color"].strip(), + "shape": fruit_data["shape"].strip(), + "created_at": datetime.utcnow().isoformat() + } + + _fruits_store.append(new_fruit) + return new_fruit + +def get_all_fruits() -> List[Dict[str, Any]]: + """ + Retrieves all fruits from the in-memory store. + + Returns: + List[Dict[str, Any]]: List of all stored fruits. + """ + return _fruits_store + +def get_fruit_by_name(name: str) -> Optional[Dict[str, Any]]: + """ + Retrieves a fruit by its name. + + Args: + name (str): The name of the fruit to find. + + Returns: + Optional[Dict[str, Any]]: The fruit if found, None otherwise. + """ + name = name.strip().lower() + for fruit in _fruits_store: + if fruit["name"].lower() == name: + return fruit + return None + +def get_fruits_by_color(color: str) -> List[Dict[str, Any]]: + """ + Retrieves all fruits of a specific color. + + Args: + color (str): The color to filter by. + + Returns: + List[Dict[str, Any]]: List of fruits matching the color. + """ + color = color.strip().lower() + return [fruit for fruit in _fruits_store if fruit["color"].lower() == color] + +def get_fruits_by_shape(shape: str) -> List[Dict[str, Any]]: + """ + Retrieves all fruits of a specific shape. + + Args: + shape (str): The shape to filter by. + + Returns: + List[Dict[str, Any]]: List of fruits matching the shape. + """ + shape = shape.strip().lower() + return [fruit for fruit in _fruits_store if fruit["shape"].lower() == shape] \ No newline at end of file diff --git a/models/fruit.py b/models/fruit.py new file mode 100644 index 0000000..3087fd4 --- /dev/null +++ b/models/fruit.py @@ -0,0 +1,15 @@ +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, index=True) + color = Column(String, nullable=False) + shape = Column(String, nullable=False) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file