30 lines
909 B
Python
30 lines
909 B
Python
from fastapi import APIRouter, HTTPException, status
|
|
from typing import Dict
|
|
from pydantic import BaseModel
|
|
|
|
from helpers.fruit_helpers import add_fruit, validate_fruit_data
|
|
|
|
router = APIRouter()
|
|
|
|
class FruitCreate(BaseModel):
|
|
name: str
|
|
quantity: int
|
|
|
|
@router.post("/create-fruit", status_code=status.HTTP_201_CREATED, response_model=Dict[str, any])
|
|
async def create_fruit(fruit: FruitCreate):
|
|
"""Create a new fruit entry"""
|
|
try:
|
|
if not validate_fruit_data(fruit.name, fruit.quantity):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Invalid fruit data provided"
|
|
)
|
|
|
|
new_fruit = add_fruit(fruit.name, fruit.quantity)
|
|
return new_fruit
|
|
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e)
|
|
) |