feat: add new endpoint to retrieve apples by name
This commit is contained in:
parent
3f79643d3b
commit
d66406b2ae
30
alembic/versions/20250429_181021_ca4393d0_update_apple.py
Normal file
30
alembic/versions/20250429_181021_ca4393d0_update_apple.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"""create table for apples
|
||||||
|
|
||||||
|
Revision ID: 5d7c16b9e3f4
|
||||||
|
Revises: 9a7d63f4b8f2
|
||||||
|
Create Date: 2023-05-16 12:34:56
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '5d7c16b9e3f4'
|
||||||
|
down_revision = '9a7d63f4b8f2'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'apples',
|
||||||
|
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('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()),
|
||||||
|
sa.Index('ix_apples_name', 'name', unique=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('apples')
|
@ -0,0 +1,15 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from typing import List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.apple import AppleSchema
|
||||||
|
from helpers.apple_helpers import get_apple_by_name
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/rest", status_code=200, response_model=List[AppleSchema])
|
||||||
|
async def get_apples_by_name(name: str, db: Session = Depends(get_db)):
|
||||||
|
apples = get_apple_by_name(db, name)
|
||||||
|
if not apples:
|
||||||
|
return []
|
||||||
|
return [apples]
|
92
helpers/apple_helpers.py
Normal file
92
helpers/apple_helpers.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.apple import Apple
|
||||||
|
from schemas.apple import AppleSchema, AppleCreate, AppleUpdate
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
def get_all_apples(db: Session) -> List[AppleSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves all apples from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[AppleSchema]: A list of all apple objects.
|
||||||
|
"""
|
||||||
|
apples = db.query(Apple).all()
|
||||||
|
return [AppleSchema.from_orm(apple) for apple in apples]
|
||||||
|
|
||||||
|
def get_apple_by_name(db: Session, name: str) -> Optional[AppleSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves an apple from the database by its name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
name (str): The name of the apple to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[AppleSchema]: The apple object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
apple = db.query(Apple).filter(Apple.name == name).first()
|
||||||
|
return AppleSchema.from_orm(apple) if apple else None
|
||||||
|
|
||||||
|
def create_apple(db: Session, apple_data: AppleCreate) -> AppleSchema:
|
||||||
|
"""
|
||||||
|
Creates a new apple in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
apple_data (AppleCreate): The data for the apple to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
AppleSchema: The newly created apple object.
|
||||||
|
"""
|
||||||
|
db_apple = Apple(**apple_data.dict())
|
||||||
|
db.add(db_apple)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_apple)
|
||||||
|
return AppleSchema.from_orm(db_apple)
|
||||||
|
|
||||||
|
def update_apple(db: Session, apple_id: UUID, apple_data: AppleUpdate) -> Optional[AppleSchema]:
|
||||||
|
"""
|
||||||
|
Updates an existing apple in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
apple_id (UUID): The ID of the apple to update.
|
||||||
|
apple_data (AppleUpdate): The updated data for the apple.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[AppleSchema]: The updated apple object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
db_apple = db.query(Apple).filter(Apple.id == apple_id).first()
|
||||||
|
if not db_apple:
|
||||||
|
return None
|
||||||
|
|
||||||
|
update_data = apple_data.dict(exclude_unset=True)
|
||||||
|
for key, value in update_data.items():
|
||||||
|
setattr(db_apple, key, value)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_apple)
|
||||||
|
return AppleSchema.from_orm(db_apple)
|
||||||
|
|
||||||
|
def delete_apple(db: Session, apple_id: UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes an apple from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
apple_id (UUID): The ID of the apple to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the apple was successfully deleted, False otherwise.
|
||||||
|
"""
|
||||||
|
db_apple = db.query(Apple).filter(Apple.id == apple_id).first()
|
||||||
|
if not db_apple:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(db_apple)
|
||||||
|
db.commit()
|
||||||
|
return True
|
13
models/apple.py
Normal file
13
models/apple.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
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 Apple(Base):
|
||||||
|
__tablename__ = "apples"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
21
schemas/apple.py
Normal file
21
schemas/apple.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class AppleBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the apple")
|
||||||
|
|
||||||
|
class AppleCreate(AppleBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AppleUpdate(AppleBase):
|
||||||
|
name: Optional[str] = Field(None, description="Name of the apple")
|
||||||
|
|
||||||
|
class AppleSchema(AppleBase):
|
||||||
|
id: UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user