19 lines
656 B
Python
19 lines
656 B
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
from core.database import get_db
|
|
from schemas.fruit import FruitSchema
|
|
from helpers.fruit_helpers import get_fruit_by_name, sanitize_fruit_name
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/get-fruit-by-name", response_model=Optional[FruitSchema])
|
|
async def get_fruit_by_name_endpoint(
|
|
name: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
sanitized_name = sanitize_fruit_name(name)
|
|
fruit = get_fruit_by_name(db=db, name=sanitized_name)
|
|
if not fruit:
|
|
raise HTTPException(status_code=404, detail="Fruit not found")
|
|
return fruit |