diff --git a/endpoints/fruits.get.py b/endpoints/fruits.get.py index 9467b43..7361aad 100644 --- a/endpoints/fruits.get.py +++ b/endpoints/fruits.get.py @@ -1,19 +1,12 @@ -from fastapi import APIRouter, Depends -from sqlalchemy.orm import Session +from fastapi import APIRouter from typing import List -from core.database import get_db from schemas.fruit import FruitSchema from helpers.fruit_helpers import get_all_fruits router = APIRouter() -@router.get("/fruits", response_model=List[FruitSchema]) -async def get_fruits( - skip: int = 0, - limit: int = 100, - sort_by: str = "created_at", - sort_desc: bool = True, - db: Session = Depends(get_db) -): - fruits = get_all_fruits(db, skip=skip, limit=limit, sort_by=sort_by, sort_desc=sort_desc) +@router.get("/fruits", status_code=200, response_model=List[FruitSchema]) +async def get_fruits(): + """Get all fruits""" + fruits = get_all_fruits() return fruits \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py index 4e43b61..a84a155 100644 --- a/helpers/fruit_helpers.py +++ b/helpers/fruit_helpers.py @@ -1,38 +1,66 @@ from typing import List, Optional, Dict, Any from uuid import UUID from sqlalchemy.orm import Session -from sqlalchemy import desc +from datetime import datetime from models.fruit import Fruit -from schemas.fruit import FruitCreate, FruitUpdate +from schemas.fruit import FruitCreate, FruitUpdate, FruitSchema def get_all_fruits( - db: Session, skip: int = 0, limit: int = 100, sort_by: str = "created_at", sort_desc: bool = True -) -> List[Fruit]: +) -> List[FruitSchema]: """ - Retrieves all fruits from the database with pagination and sorting. + Returns a static list of fruits with pagination and sorting. Args: - db (Session): The database session skip (int): Number of records to skip limit (int): Maximum number of records to return sort_by (str): Field to sort by sort_desc (bool): Sort in descending order if True Returns: - List[Fruit]: List of fruit objects + List[FruitSchema]: List of fruit objects """ - query = db.query(Fruit) + # Static list of fruits + static_fruits = [ + { + "id": UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479"), + "name": "Apple", + "description": "A sweet and crunchy fruit", + "created_at": datetime(2023, 1, 1, 12, 0), + "updated_at": datetime(2023, 1, 1, 12, 0) + }, + { + "id": UUID("f47ac10b-58cc-4372-a567-0e02b2c3d480"), + "name": "Banana", + "description": "A yellow tropical fruit", + "created_at": datetime(2023, 1, 2, 12, 0), + "updated_at": datetime(2023, 1, 2, 12, 0) + }, + { + "id": UUID("f47ac10b-58cc-4372-a567-0e02b2c3d481"), + "name": "Orange", + "description": "A citrus fruit rich in vitamin C", + "created_at": datetime(2023, 1, 3, 12, 0), + "updated_at": datetime(2023, 1, 3, 12, 0) + } + ] - if hasattr(Fruit, sort_by): - order_criteria = desc(getattr(Fruit, sort_by)) if sort_desc else getattr(Fruit, sort_by) - query = query.order_by(order_criteria) + # Convert dictionary items to FruitSchema objects + fruits = [FruitSchema(**fruit) for fruit in static_fruits] - return query.offset(skip).limit(limit).all() + # Sort the fruits + if sort_by in FruitSchema.__fields__: + fruits.sort( + key=lambda x: getattr(x, sort_by), + reverse=sort_desc + ) + + # Apply pagination + return fruits[skip:skip + limit] def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]: """ diff --git a/schemas/fruit.py b/schemas/fruit.py index f3a6423..03f780b 100644 --- a/schemas/fruit.py +++ b/schemas/fruit.py @@ -20,7 +20,7 @@ class FruitSchema(FruitBase): updated_at: datetime class Config: - orm_mode = True + from_attributes = True schema_extra = { "example": { "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",