feat: Generated endpoint endpoints/fruits.get.py via AI for Fruit with auto lint fixes
This commit is contained in:
parent
ae300b8039
commit
d831b0922f
31
alembic/versions/20250412_011924_cc73e45a_update_fruit.py
Normal file
31
alembic/versions/20250412_011924_cc73e45a_update_fruit.py
Normal 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')
|
@ -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
|
@ -1,93 +1,105 @@
|
|||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy import and_
|
||||||
from models.fruit import Fruit
|
from models.fruit import Fruit
|
||||||
from schemas.fruit import FruitCreate, FruitUpdate
|
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:
|
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:
|
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]:
|
def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
|
||||||
"""
|
"""
|
||||||
Retrieves a single fruit by its ID.
|
Retrieves a single fruit by its ID.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
fruit_id (UUID): The ID of the fruit to retrieve.
|
fruit_id (UUID): The ID of the fruit to retrieve
|
||||||
|
|
||||||
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.id == fruit_id).first()
|
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.
|
Creates a new fruit in the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
fruit_data (FruitCreate): The data for the fruit to create.
|
fruit_data (FruitCreate): The data for the fruit to create
|
||||||
|
|
||||||
Returns:
|
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_fruit = Fruit(**fruit_data.dict())
|
db.add(db_fruit)
|
||||||
db.add(db_fruit)
|
db.commit()
|
||||||
db.commit()
|
db.refresh(db_fruit)
|
||||||
db.refresh(db_fruit)
|
return db_fruit
|
||||||
return db_fruit
|
|
||||||
except IntegrityError:
|
|
||||||
db.rollback()
|
|
||||||
return None
|
|
||||||
|
|
||||||
def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Optional[Fruit]:
|
def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Optional[Fruit]:
|
||||||
"""
|
"""
|
||||||
Updates an existing fruit in the database.
|
Updates an existing fruit in the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
fruit_id (UUID): The ID of the fruit to update.
|
fruit_id (UUID): The ID of the fruit to update
|
||||||
fruit_data (FruitUpdate): The updated fruit data.
|
fruit_data (FruitUpdate): The update data for the fruit
|
||||||
|
|
||||||
Returns:
|
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)
|
db_fruit = get_fruit_by_id(db, fruit_id)
|
||||||
if not db_fruit:
|
if not db_fruit:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
for key, value in fruit_data.dict(exclude_unset=True).items():
|
update_data = fruit_data.dict(exclude_unset=True)
|
||||||
setattr(db_fruit, key, value)
|
for field, value in update_data.items():
|
||||||
|
setattr(db_fruit, field, value)
|
||||||
|
|
||||||
try:
|
db.commit()
|
||||||
db.commit()
|
db.refresh(db_fruit)
|
||||||
db.refresh(db_fruit)
|
return db_fruit
|
||||||
return db_fruit
|
|
||||||
except IntegrityError:
|
|
||||||
db.rollback()
|
|
||||||
return None
|
|
||||||
|
|
||||||
def delete_fruit(db: Session, fruit_id: UUID) -> bool:
|
def delete_fruit(db: Session, fruit_id: UUID) -> bool:
|
||||||
"""
|
"""
|
||||||
Deletes a fruit from the database.
|
Deletes a fruit from the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
fruit_id (UUID): The ID of the fruit to delete.
|
fruit_id (UUID): The ID of the fruit to delete
|
||||||
|
|
||||||
Returns:
|
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)
|
db_fruit = get_fruit_by_id(db, fruit_id)
|
||||||
if not db_fruit:
|
if not db_fruit:
|
||||||
@ -97,28 +109,43 @@ def delete_fruit(db: Session, fruit_id: UUID) -> bool:
|
|||||||
db.commit()
|
db.commit()
|
||||||
return True
|
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:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
|
fruit_id (UUID): The ID of the fruit to soft delete
|
||||||
|
|
||||||
Returns:
|
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()
|
db_fruit = get_fruit_by_id(db, fruit_id)
|
||||||
return [fruit.name for fruit in fruits]
|
if not db_fruit:
|
||||||
|
return False
|
||||||
|
|
||||||
def get_fruit_by_name(db: Session, name: str) -> Optional[Fruit]:
|
db_fruit.is_active = False
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
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:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session
|
||||||
name (str): The name of the fruit to retrieve.
|
search_term (str): The search term to filter by
|
||||||
|
active_only (bool): If True, returns only active fruits
|
||||||
|
|
||||||
Returns:
|
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()
|
@ -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.dialects.postgresql import UUID
|
||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from core.database import Base
|
from core.database import Base
|
||||||
@ -9,5 +9,7 @@ class Fruit(Base):
|
|||||||
|
|
||||||
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, unique=True, nullable=False, index=True)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
|
is_active = Column(Boolean, default=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,13 +4,17 @@ from datetime import datetime
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
class FruitBase(BaseModel):
|
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):
|
class FruitCreate(FruitBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class FruitUpdate(BaseModel):
|
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):
|
class FruitSchema(FruitBase):
|
||||||
id: UUID
|
id: UUID
|
||||||
@ -23,6 +27,8 @@ class FruitSchema(FruitBase):
|
|||||||
"example": {
|
"example": {
|
||||||
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||||
"name": "Apple",
|
"name": "Apple",
|
||||||
|
"description": "A sweet, edible fruit produced by an apple tree",
|
||||||
|
"is_active": True,
|
||||||
"created_at": "2023-01-01T12:00:00",
|
"created_at": "2023-01-01T12:00:00",
|
||||||
"updated_at": "2023-01-01T12:00:00"
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user