feat: Generated endpoint endpoints/single.get.py via AI for Flower with auto lint fixes
This commit is contained in:
parent
ed89030801
commit
f39db59c52
35
alembic/versions/20250419_113440_45a43505_update_flower.py
Normal file
35
alembic/versions/20250419_113440_45a43505_update_flower.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
"""create table for flowers
|
||||||
|
|
||||||
|
Revision ID: 2b6e1c8c0e21
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-24 14:47:49.120340
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2b6e1c8c0e21'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'flowers',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=func.uuid_func()),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
sa.Column('species', sa.String(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()),
|
||||||
|
sa.Index('ix_flowers_name', 'name'),
|
||||||
|
sa.Index('ix_flowers_species', 'species')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('flowers')
|
@ -0,0 +1,12 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from typing import List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.flower import FlowerSchema
|
||||||
|
from helpers.flower_helpers import get_all_flowers
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/single", status_code=200, response_model=List[FlowerSchema])
|
||||||
|
async def get_flowers(db: Session = Depends(get_db)):
|
||||||
|
return get_all_flowers(db)
|
116
helpers/flower_helpers.py
Normal file
116
helpers/flower_helpers.py
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import and_
|
||||||
|
from models.flower import Flower
|
||||||
|
from schemas.flower import FlowerCreate, FlowerUpdate
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
def get_all_flowers(db: Session) -> List[Flower]:
|
||||||
|
"""
|
||||||
|
Retrieves all flowers from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Flower]: A list of all flower objects.
|
||||||
|
"""
|
||||||
|
return db.query(Flower).all()
|
||||||
|
|
||||||
|
def get_flower_by_id(db: Session, flower_id: uuid.UUID) -> Optional[Flower]:
|
||||||
|
"""
|
||||||
|
Retrieves a single flower by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
flower_id (UUID): The ID of the flower to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Flower]: The flower object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Flower).filter(Flower.id == flower_id).first()
|
||||||
|
|
||||||
|
def get_flowers_by_name_and_species(db: Session, name: Optional[str] = None, species: Optional[str] = None) -> List[Flower]:
|
||||||
|
"""
|
||||||
|
Retrieves flowers based on name and/or species.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
name (Optional[str]): The name of the flower to filter by.
|
||||||
|
species (Optional[str]): The species of the flower to filter by.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Flower]: A list of flower objects matching the filters.
|
||||||
|
"""
|
||||||
|
filters = []
|
||||||
|
if name:
|
||||||
|
filters.append(Flower.name == name)
|
||||||
|
if species:
|
||||||
|
filters.append(Flower.species == species)
|
||||||
|
|
||||||
|
query = db.query(Flower)
|
||||||
|
if filters:
|
||||||
|
query = query.filter(and_(*filters))
|
||||||
|
|
||||||
|
return query.all()
|
||||||
|
|
||||||
|
def create_flower(db: Session, flower_data: FlowerCreate) -> Flower:
|
||||||
|
"""
|
||||||
|
Creates a new flower in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
flower_data (FlowerCreate): The data for the flower to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Flower: The newly created flower object.
|
||||||
|
"""
|
||||||
|
db_flower = Flower(**flower_data.dict())
|
||||||
|
db.add(db_flower)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_flower)
|
||||||
|
return db_flower
|
||||||
|
|
||||||
|
def update_flower(db: Session, flower_id: uuid.UUID, flower_data: FlowerUpdate) -> Optional[Flower]:
|
||||||
|
"""
|
||||||
|
Updates an existing flower in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
flower_id (UUID): The ID of the flower to update.
|
||||||
|
flower_data (FlowerUpdate): The updated data for the flower.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Flower]: The updated flower object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
db_flower = db.query(Flower).filter(Flower.id == flower_id).first()
|
||||||
|
if not db_flower:
|
||||||
|
return None
|
||||||
|
|
||||||
|
update_data = flower_data.dict(exclude_unset=True)
|
||||||
|
for key, value in update_data.items():
|
||||||
|
setattr(db_flower, key, value)
|
||||||
|
|
||||||
|
db.add(db_flower)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_flower)
|
||||||
|
return db_flower
|
||||||
|
|
||||||
|
def delete_flower(db: Session, flower_id: uuid.UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes a flower from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
flower_id (UUID): The ID of the flower to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the flower was deleted, False otherwise.
|
||||||
|
"""
|
||||||
|
db_flower = db.query(Flower).filter(Flower.id == flower_id).first()
|
||||||
|
if not db_flower:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(db_flower)
|
||||||
|
db.commit()
|
||||||
|
return True
|
15
models/flower.py
Normal file
15
models/flower.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy import Column, String, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from core.database import Base
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class Flower(Base):
|
||||||
|
__tablename__ = "flowers"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
|
species = Column(String, nullable=False, index=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
25
schemas/flower.py
Normal file
25
schemas/flower.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class FlowerBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the flower")
|
||||||
|
species: str = Field(..., description="Species of the flower")
|
||||||
|
|
||||||
|
class FlowerCreate(FlowerBase):
|
||||||
|
description: Optional[str] = Field(None, description="Description of the flower")
|
||||||
|
|
||||||
|
class FlowerUpdate(FlowerBase):
|
||||||
|
name: Optional[str] = Field(None, description="Name of the flower")
|
||||||
|
species: Optional[str] = Field(None, description="Species of the flower")
|
||||||
|
description: Optional[str] = Field(None, description="Description of the flower")
|
||||||
|
|
||||||
|
class FlowerSchema(FlowerBase):
|
||||||
|
id: UUID
|
||||||
|
description: Optional[str] = Field(None, description="Description of the flower")
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user