feat: Generated endpoint endpoints/fruits.get.py via AI for Fruit and updated requirements.txt with auto lint fixes
This commit is contained in:
parent
848fc37f4f
commit
ee90463841
34
alembic/versions/20250414_204940_729e8b60_update_fruit.py
Normal file
34
alembic/versions/20250414_204940_729e8b60_update_fruit.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
"""create table for fruits
|
||||||
|
Revision ID: 2c8e7d9f9e4b
|
||||||
|
Revises: 6d5cd4bae0c6
|
||||||
|
Create Date: 2023-05-25 12:34:56
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2c8e7d9f9e4b'
|
||||||
|
down_revision = '6d5cd4bae0c6'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_name = "fruits"
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
table_name,
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_fruits_name'), table_name, ['name'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
table_name = "fruits"
|
||||||
|
op.drop_index(op.f('ix_fruits_name'), table_name=table_name)
|
||||||
|
op.drop_table(table_name)
|
@ -0,0 +1,12 @@
|
|||||||
|
from fastapi import APIRouter, status, Depends
|
||||||
|
from typing import List
|
||||||
|
from schemas.fruit import FruitSchema
|
||||||
|
from helpers.fruit_helpers import get_all_fruits
|
||||||
|
from db import get_db, Session
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/fruits", status_code=status.HTTP_200_OK, response_model=List[FruitSchema])
|
||||||
|
def get_fruits(db: Session = Depends(get_db)):
|
||||||
|
fruits = get_all_fruits(db)
|
||||||
|
return fruits
|
119
helpers/fruit_helpers.py
Normal file
119
helpers/fruit_helpers.py
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
import uuid
|
||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import or_
|
||||||
|
|
||||||
|
from models.fruit import Fruit
|
||||||
|
from schemas.fruit import FruitCreate, FruitUpdate
|
||||||
|
|
||||||
|
def get_all_fruits(db: Session) -> List[Fruit]:
|
||||||
|
"""
|
||||||
|
Retrieves all fruits from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Fruit]: A list of all fruit objects.
|
||||||
|
"""
|
||||||
|
return db.query(Fruit).all()
|
||||||
|
|
||||||
|
def get_fruit_by_id(db: Session, fruit_id: uuid.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.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Fruit]: The fruit object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
||||||
|
|
||||||
|
def get_fruits_by_name(db: Session, name: str) -> List[Fruit]:
|
||||||
|
"""
|
||||||
|
Retrieves fruits by their name (case-insensitive).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
name (str): The name of the fruits to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Fruit]: A list of fruit objects matching the name.
|
||||||
|
"""
|
||||||
|
return db.query(Fruit).filter(Fruit.name.ilike(f"%{name}%")).all()
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Fruit: The newly created fruit object.
|
||||||
|
"""
|
||||||
|
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.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 data for the fruit.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Fruit]: The updated fruit object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
db_fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
||||||
|
if not db_fruit:
|
||||||
|
return None
|
||||||
|
|
||||||
|
for field, value in fruit_data.dict(exclude_unset=True).items():
|
||||||
|
setattr(db_fruit, field, value)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_fruit)
|
||||||
|
return db_fruit
|
||||||
|
|
||||||
|
def delete_fruit(db: Session, fruit_id: uuid.UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes a fruit from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
fruit_id (UUID): The ID of the fruit to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the fruit was successfully deleted, False otherwise.
|
||||||
|
"""
|
||||||
|
db_fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
||||||
|
if not db_fruit:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(db_fruit)
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def search_fruits(db: Session, query: str) -> List[Fruit]:
|
||||||
|
"""
|
||||||
|
Searches for fruits by name or description.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
query (str): The search query string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Fruit]: A list of fruit objects matching the search query.
|
||||||
|
"""
|
||||||
|
return db.query(Fruit).filter(
|
||||||
|
or_(Fruit.name.ilike(f"%{query}%"), Fruit.description.ilike(f"%{query}%"))
|
||||||
|
).all()
|
14
models/fruit.py
Normal file
14
models/fruit.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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 Fruit(Base):
|
||||||
|
__tablename__ = "fruits"
|
||||||
|
|
||||||
|
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)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
23
schemas/fruit.py
Normal file
23
schemas/fruit.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class FruitBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the fruit")
|
||||||
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||||
|
|
||||||
|
class FruitCreate(FruitBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class FruitUpdate(FruitBase):
|
||||||
|
name: Optional[str] = Field(None, description="Name of the fruit")
|
||||||
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||||
|
|
||||||
|
class FruitSchema(FruitBase):
|
||||||
|
id: UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user