feat: add fruit endpoints to get all fruits and single fruit, with sorting capabilities ✅ (auto-linted)
This commit is contained in:
parent
b44bfcde69
commit
cf810bf4f4
33
alembic/versions/20250429_201921_c2003b42_update_fruit.py
Normal file
33
alembic/versions/20250429_201921_c2003b42_update_fruit.py
Normal file
@ -0,0 +1,33 @@
|
||||
"""create table for fruits
|
||||
|
||||
Revision ID: 2c9a83e4f6b3
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-22 12:00:00
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
import uuid
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '2c9a83e4f6b3'
|
||||
down_revision = '0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'fruits',
|
||||
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('price', sa.Integer(), 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())
|
||||
)
|
||||
op.create_index(op.f('ix_fruits_name'), 'fruits', ['name'], unique=True)
|
||||
|
||||
def downgrade():
|
||||
op.drop_index(op.f('ix_fruits_name'), table_name='fruits')
|
||||
op.drop_table('fruits')
|
@ -0,0 +1,25 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from typing import List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.fruit import FruitSchema
|
||||
from helpers.fruit_helpers import get_all_fruits, get_fruit_by_id
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/fruits", status_code=status.HTTP_200_OK, response_model=List[FruitSchema])
|
||||
def get_fruits(
|
||||
order_by: Optional[str] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return get_all_fruits(db, order_by)
|
||||
|
||||
@router.get("/fruits/{fruit_id}", status_code=status.HTTP_200_OK, response_model=FruitSchema)
|
||||
def get_fruit(
|
||||
fruit_id: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
fruit = get_fruit_by_id(db, fruit_id)
|
||||
if not fruit:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Fruit not found")
|
||||
return fruit
|
96
helpers/fruit_helpers.py
Normal file
96
helpers/fruit_helpers.py
Normal file
@ -0,0 +1,96 @@
|
||||
from typing import List, Optional
|
||||
from uuid import UUID
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import asc, desc
|
||||
from models.fruit import Fruit
|
||||
from schemas.fruit import FruitCreate, FruitUpdate
|
||||
|
||||
def get_all_fruits(db: Session, order_by: Optional[str] = None) -> List[Fruit]:
|
||||
"""
|
||||
Retrieves all fruits from the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
order_by (Optional[str]): The field to order the results by ('asc' or 'desc').
|
||||
|
||||
Returns:
|
||||
List[Fruit]: A list of all fruit objects.
|
||||
"""
|
||||
query = db.query(Fruit)
|
||||
if order_by == 'asc':
|
||||
query = query.order_by(asc(Fruit.name))
|
||||
elif order_by == 'desc':
|
||||
query = query.order_by(desc(Fruit.name))
|
||||
return query.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.
|
||||
|
||||
Returns:
|
||||
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) -> 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, 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) -> 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
|
15
models/fruit.py
Normal file
15
models/fruit.py
Normal file
@ -0,0 +1,15 @@
|
||||
from sqlalchemy import Column, String, Integer, 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)
|
||||
price = Column(Integer, nullable=False)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
@ -7,3 +7,6 @@ sqlalchemy>=1.4.0
|
||||
python-dotenv>=0.19.0
|
||||
bcrypt>=3.2.0
|
||||
alembic>=1.13.1
|
||||
jose
|
||||
passlib
|
||||
pydantic
|
||||
|
25
schemas/fruit.py
Normal file
25
schemas/fruit.py
Normal file
@ -0,0 +1,25 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
class FruitBase(BaseModel):
|
||||
name: str = Field(..., description="Name of the fruit")
|
||||
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||
price: int = Field(..., description="Price 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")
|
||||
price: Optional[int] = Field(None, description="Price of the fruit")
|
||||
|
||||
class FruitSchema(FruitBase):
|
||||
id: uuid.UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user