diff --git a/endpoints/create-fruit.post.py b/endpoints/create-fruit.post.py index 191a70a..b53f54c 100644 --- a/endpoints/create-fruit.post.py +++ b/endpoints/create-fruit.post.py @@ -1,30 +1,17 @@ -from fastapi import APIRouter, HTTPException, status -from typing import Dict -from pydantic import BaseModel - -from helpers.fruit_helpers import add_fruit, validate_fruit_data +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.fruit import FruitCreate, FruitSchema +from helpers.fruit_helpers import add_fruit 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) - ) \ No newline at end of file +@router.post("/create-fruit", status_code=status.HTTP_201_CREATED, response_model=FruitSchema) +async def create_fruit( + fruit: FruitCreate, + db: Session = Depends(get_db) +): + new_fruit = add_fruit(db=db, fruit=fruit) + if not new_fruit: + raise HTTPException(status_code=400, detail="Fruit could not be created") + return new_fruit \ No newline at end of file