feat: Updated endpoint endpoints/create-fruit.post.py via AI
This commit is contained in:
parent
7f5d260bff
commit
9fd9439f6f
@ -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)
|
||||
)
|
||||
@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
|
Loading…
x
Reference in New Issue
Block a user