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 fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from typing import Dict
|
from sqlalchemy.orm import Session
|
||||||
from pydantic import BaseModel
|
from core.database import get_db
|
||||||
|
from schemas.fruit import FruitCreate, FruitSchema
|
||||||
from helpers.fruit_helpers import add_fruit, validate_fruit_data
|
from helpers.fruit_helpers import add_fruit
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class FruitCreate(BaseModel):
|
@router.post("/create-fruit", status_code=status.HTTP_201_CREATED, response_model=FruitSchema)
|
||||||
name: str
|
async def create_fruit(
|
||||||
quantity: int
|
fruit: FruitCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
@router.post("/create-fruit", status_code=status.HTTP_201_CREATED, response_model=Dict[str, any])
|
):
|
||||||
async def create_fruit(fruit: FruitCreate):
|
new_fruit = add_fruit(db=db, fruit=fruit)
|
||||||
"""Create a new fruit entry"""
|
if not new_fruit:
|
||||||
try:
|
raise HTTPException(status_code=400, detail="Fruit could not be created")
|
||||||
if not validate_fruit_data(fruit.name, fruit.quantity):
|
return new_fruit
|
||||||
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)
|
|
||||||
)
|
|
Loading…
x
Reference in New Issue
Block a user