feat: Generated endpoint endpoints/fruits.get.py via AI for Fruit with auto lint fixes

This commit is contained in:
Backend IM Bot 2025-04-12 01:20:03 +00:00
parent ae300b8039
commit d831b0922f
5 changed files with 137 additions and 53 deletions

View File

@ -0,0 +1,31 @@
"""create fruits table
Revision ID: b4f8c2e9d1a5
Revises: a2b3c4d5e6f7
Create Date: 2024-01-18 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
import uuid
# revision identifiers, used by Alembic.
revision = 'b4f8c2e9d1a5'
down_revision = 'a2b3c4d5e6f7'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'fruits',
sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=True),
sa.Column('is_active', sa.Boolean(), server_default=sa.text('true')),
sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now()),
)
op.create_index('ix_fruits_name', 'fruits', ['name'], unique=True)
def downgrade():
op.drop_index('ix_fruits_name')
op.drop_table('fruits')

View File

@ -0,0 +1,18 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List
from core.database import get_db
from schemas.fruit import FruitSchema
from helpers.fruit_helpers import get_all_fruits
router = APIRouter()
@router.get("/fruits", response_model=List[FruitSchema])
async def get_fruits(
skip: int = 0,
limit: int = 100,
active_only: bool = True,
db: Session = Depends(get_db)
):
fruits = get_all_fruits(db, skip=skip, limit=limit, active_only=active_only)
return fruits

View File

@ -1,93 +1,105 @@
from typing import List, Optional
from uuid import UUID
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from sqlalchemy import and_
from models.fruit import Fruit
from schemas.fruit import FruitCreate, FruitUpdate
def get_all_fruits(db: Session) -> List[Fruit]:
def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]:
"""
Retrieves all fruits from the database.
Retrieves all fruits from the database with optional pagination and active filtering.
Args:
db (Session): The database session.
db (Session): The database session
skip (int): Number of records to skip
limit (int): Maximum number of records to return
active_only (bool): If True, returns only active fruits
Returns:
List[Fruit]: A list of all fruit objects.
List[Fruit]: List of fruit objects
"""
return db.query(Fruit).all()
query = db.query(Fruit)
if active_only:
query = query.filter(Fruit.is_active)
return query.offset(skip).limit(limit).all()
def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
"""
Retrieves a single fruit by its ID.
Args:
db (Session): The database session.
fruit_id (UUID): The ID of the fruit to retrieve.
db (Session): The database session
fruit_id (UUID): The ID of the fruit to retrieve
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.id == fruit_id).first()
def create_fruit(db: Session, fruit_data: FruitCreate) -> Optional[Fruit]:
def get_fruit_by_name(db: Session, name: str) -> Optional[Fruit]:
"""
Retrieves a single fruit by its name.
Args:
db (Session): The database session
name (str): The name of the fruit to retrieve
Returns:
Optional[Fruit]: The fruit object if found, otherwise None
"""
return db.query(Fruit).filter(Fruit.name == name).first()
def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit:
"""
Creates a new fruit in the database.
Args:
db (Session): The database session.
fruit_data (FruitCreate): The data for the fruit to create.
db (Session): The database session
fruit_data (FruitCreate): The data for the fruit to create
Returns:
Optional[Fruit]: The newly created fruit object, or None if creation fails.
Fruit: The newly created fruit object
"""
try:
db_fruit = Fruit(**fruit_data.dict())
db.add(db_fruit)
db.commit()
db.refresh(db_fruit)
return db_fruit
except IntegrityError:
db.rollback()
return None
db_fruit = Fruit(**fruit_data.dict())
db.add(db_fruit)
db.commit()
db.refresh(db_fruit)
return db_fruit
def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Optional[Fruit]:
"""
Updates an existing fruit in the database.
Args:
db (Session): The database session.
fruit_id (UUID): The ID of the fruit to update.
fruit_data (FruitUpdate): The updated fruit data.
db (Session): The database session
fruit_id (UUID): The ID of the fruit to update
fruit_data (FruitUpdate): The update data for the fruit
Returns:
Optional[Fruit]: The updated fruit object if found and updated, otherwise None.
Optional[Fruit]: The updated fruit object if found, otherwise None
"""
db_fruit = get_fruit_by_id(db, fruit_id)
if not db_fruit:
return None
for key, value in fruit_data.dict(exclude_unset=True).items():
setattr(db_fruit, key, value)
update_data = fruit_data.dict(exclude_unset=True)
for field, value in update_data.items():
setattr(db_fruit, field, value)
try:
db.commit()
db.refresh(db_fruit)
return db_fruit
except IntegrityError:
db.rollback()
return None
db.commit()
db.refresh(db_fruit)
return db_fruit
def delete_fruit(db: Session, fruit_id: UUID) -> bool:
"""
Deletes a fruit from the database.
Args:
db (Session): The database session.
fruit_id (UUID): The ID of the fruit to delete.
db (Session): The database session
fruit_id (UUID): The ID of the fruit to delete
Returns:
bool: True if the fruit was deleted, False if not found.
bool: True if the fruit was deleted, False if not found
"""
db_fruit = get_fruit_by_id(db, fruit_id)
if not db_fruit:
@ -97,28 +109,43 @@ def delete_fruit(db: Session, fruit_id: UUID) -> bool:
db.commit()
return True
def get_fruit_names(db: Session) -> List[str]:
def soft_delete_fruit(db: Session, fruit_id: UUID) -> bool:
"""
Retrieves a list of all fruit names from the database.
Soft deletes a fruit by setting is_active to False.
Args:
db (Session): The database session.
db (Session): The database session
fruit_id (UUID): The ID of the fruit to soft delete
Returns:
List[str]: A list of fruit names.
bool: True if the fruit was soft deleted, False if not found
"""
fruits = db.query(Fruit.name).all()
return [fruit.name for fruit in fruits]
db_fruit = get_fruit_by_id(db, fruit_id)
if not db_fruit:
return False
db_fruit.is_active = False
db.commit()
return True
def get_fruit_by_name(db: Session, name: str) -> Optional[Fruit]:
def search_fruits(db: Session, search_term: str, active_only: bool = True) -> List[Fruit]:
"""
Retrieves a fruit by its name.
Searches fruits by name or description.
Args:
db (Session): The database session.
name (str): The name of the fruit to retrieve.
db (Session): The database session
search_term (str): The search term to filter by
active_only (bool): If True, returns only active fruits
Returns:
Optional[Fruit]: The fruit object if found, otherwise None.
List[Fruit]: List of matching fruit objects
"""
return db.query(Fruit).filter(Fruit.name == name).first()
filters = [
Fruit.name.ilike(f"%{search_term}%") |
Fruit.description.ilike(f"%{search_term}%")
]
if active_only:
filters.append(Fruit.is_active)
return db.query(Fruit).filter(and_(*filters)).all()

View File

@ -1,4 +1,4 @@
from sqlalchemy import Column, String, DateTime
from sqlalchemy import Column, String, DateTime, Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
from core.database import Base
@ -9,5 +9,7 @@ class Fruit(Base):
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, unique=True, nullable=False, index=True)
description = Column(String, nullable=True)
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

View File

@ -4,13 +4,17 @@ from datetime import datetime
from uuid import UUID
class FruitBase(BaseModel):
name: str = Field(..., min_length=1, description="Name of the fruit")
name: str = Field(..., min_length=1, description="Fruit name")
description: Optional[str] = Field(None, description="Fruit description")
is_active: bool = Field(True, description="Whether the fruit is active")
class FruitCreate(FruitBase):
pass
class FruitUpdate(BaseModel):
name: Optional[str] = Field(None, min_length=1, description="Name of the fruit")
name: Optional[str] = Field(None, min_length=1, description="Fruit name")
description: Optional[str] = Field(None, description="Fruit description")
is_active: Optional[bool] = Field(None, description="Whether the fruit is active")
class FruitSchema(FruitBase):
id: UUID
@ -23,6 +27,8 @@ class FruitSchema(FruitBase):
"example": {
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"name": "Apple",
"description": "A sweet, edible fruit produced by an apple tree",
"is_active": True,
"created_at": "2023-01-01T12:00:00",
"updated_at": "2023-01-01T12:00:00"
}