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

20 lines
723 B
Python

from fastapi import APIRouter, Depends, HTTPException
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,
shape: str = None,
db: Session = Depends(get_db)
):
normalized_color = normalize_color_name(color)
fruits = get_fruits_by_color(db=db, color=normalized_color, shape=shape)
if not fruits:
raise HTTPException(status_code=404, detail=f"No fruits found with color {color}")
return fruits