feat: Updated endpoint endpoints/names.post.py via AI
This commit is contained in:
parent
ee9e1d4c90
commit
23c2e63ae3
29
alembic/versions/20250412_175353_8ea4e5de_update_fruit.py
Normal file
29
alembic/versions/20250412_175353_8ea4e5de_update_fruit.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""create fruits table
|
||||||
|
Revision ID: 0003
|
||||||
|
Revises: 0002
|
||||||
|
Create Date: 2024-01-17 10:00:00.000000
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '0003'
|
||||||
|
down_revision = '0002'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'fruits',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True),
|
||||||
|
sa.Column('name', sa.String(255), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
op.create_index(op.f('ix_fruits_name'), 'fruits', ['name'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_index(op.f('ix_fruits_name'), table_name='fruits')
|
||||||
|
op.drop_table('fruits')
|
@ -1,20 +1,17 @@
|
|||||||
from fastapi import APIRouter, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from typing import List
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.fruit import FruitCreate, FruitSchema
|
||||||
|
from helpers.fruit_helpers import create_fruit
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/names", status_code=status.HTTP_200_OK)
|
@router.post("/names", status_code=status.HTTP_201_CREATED, response_model=FruitSchema)
|
||||||
async def get_fruit_names() -> List[str]:
|
async def create_name(
|
||||||
fruits = [
|
fruit: FruitCreate,
|
||||||
"Apple",
|
db: Session = Depends(get_db)
|
||||||
"Banana",
|
):
|
||||||
"Orange",
|
new_fruit = create_fruit(db=db, fruit=fruit)
|
||||||
"Mango",
|
if not new_fruit:
|
||||||
"Pineapple",
|
raise HTTPException(status_code=400, detail="Fruit could not be created")
|
||||||
"Grape",
|
return new_fruit
|
||||||
"Strawberry",
|
|
||||||
"Blueberry",
|
|
||||||
"Peach",
|
|
||||||
"Pear"
|
|
||||||
]
|
|
||||||
return fruits
|
|
@ -17,7 +17,7 @@ def get_all_fruits(db: Session) -> List[Fruit]:
|
|||||||
Returns:
|
Returns:
|
||||||
List[Fruit]: A list of all fruit objects.
|
List[Fruit]: A list of all fruit objects.
|
||||||
"""
|
"""
|
||||||
return db.query(Fruit).all()
|
return db.query(Fruit).order_by(Fruit.name).all()
|
||||||
|
|
||||||
|
|
||||||
def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
|
def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
|
||||||
@ -45,7 +45,7 @@ def get_fruit_by_name(db: Session, name: str) -> Optional[Fruit]:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[Fruit]: The fruit object if found, otherwise None.
|
Optional[Fruit]: The fruit object if found, otherwise None.
|
||||||
"""
|
"""
|
||||||
return db.query(Fruit).filter(Fruit.name == name).first()
|
return db.query(Fruit).filter(Fruit.name.ilike(f"%{name}%")).first()
|
||||||
|
|
||||||
|
|
||||||
def create_fruit(db: Session, fruit_data: FruitCreate) -> Optional[Fruit]:
|
def create_fruit(db: Session, fruit_data: FruitCreate) -> Optional[Fruit]:
|
||||||
@ -60,7 +60,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Optional[Fruit]:
|
|||||||
Optional[Fruit]: The newly created fruit object, or None if creation fails.
|
Optional[Fruit]: The newly created fruit object, or None if creation fails.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
db_fruit = Fruit(**fruit_data.dict())
|
db_fruit = Fruit(**fruit_data.dict(exclude_unset=True))
|
||||||
db.add(db_fruit)
|
db.add(db_fruit)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_fruit)
|
db.refresh(db_fruit)
|
||||||
@ -131,7 +131,7 @@ def validate_fruit_data(data: Dict[str, Any]) -> bool:
|
|||||||
"""
|
"""
|
||||||
if not data:
|
if not data:
|
||||||
return False
|
return False
|
||||||
if "name" not in data or not isinstance(data["name"], str) or len(data["name"]) < 1:
|
if "name" not in data or not isinstance(data["name"], str) or len(data["name"]) > 255:
|
||||||
return False
|
return False
|
||||||
if "description" in data and not isinstance(data["description"], (str, type(None))):
|
if "description" in data and not isinstance(data["description"], (str, type(None))):
|
||||||
return False
|
return False
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from sqlalchemy import Column, String, DateTime
|
from sqlalchemy import Column, String, DateTime, Text
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from core.database import Base
|
from core.database import Base
|
||||||
@ -8,8 +8,8 @@ class Fruit(Base):
|
|||||||
__tablename__ = "fruits"
|
__tablename__ = "fruits"
|
||||||
|
|
||||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
name = Column(String, unique=True, nullable=False, index=True)
|
name = Column(String(255), unique=True, nullable=False, index=True)
|
||||||
description = Column(String, nullable=True)
|
description = Column(Text, nullable=True)
|
||||||
|
|
||||||
created_at = Column(DateTime, default=func.now())
|
created_at = Column(DateTime, default=func.now())
|
||||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
@ -4,14 +4,14 @@ from datetime import datetime
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
class FruitBase(BaseModel):
|
class FruitBase(BaseModel):
|
||||||
name: str = Field(..., description="Name of the fruit")
|
name: str = Field(..., description="Name of the fruit", max_length=255)
|
||||||
description: Optional[str] = Field(None, description="Description of the fruit")
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||||
|
|
||||||
class FruitCreate(FruitBase):
|
class FruitCreate(FruitBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class FruitUpdate(BaseModel):
|
class FruitUpdate(BaseModel):
|
||||||
name: Optional[str] = Field(None, description="Name of the fruit")
|
name: Optional[str] = Field(None, description="Name of the fruit", max_length=255)
|
||||||
description: Optional[str] = Field(None, description="Description of the fruit")
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||||
|
|
||||||
class FruitSchema(FruitBase):
|
class FruitSchema(FruitBase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user