3rd-project-7hltbg/endpoints/get-fruit-by-color.get.py

23 lines
799 B
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from core.database import get_db
from schemas.fruit import FruitSchema
from helpers.fruit_helpers import get_fruits_by_color, normalize_color_name
router = APIRouter()
@router.get("/get-fruit-by-color", response_model=List[FruitSchema])
async def get_fruits_by_color_endpoint(
color: str,
db: Session = Depends(get_db)
):
"""Get fruits by their color"""
normalized_color = normalize_color_name(color)
fruits = get_fruits_by_color(db=db, color=normalized_color)
if not fruits:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No fruits found with color: {normalized_color}"
)
return fruits