feat: add banana creation endpoint and related model, schema, migration
This commit is contained in:
parent
c8981fd267
commit
13af1cfc2e
32
alembic/versions/20250430_093939_f8516f9e_update_banana.py
Normal file
32
alembic/versions/20250430_093939_f8516f9e_update_banana.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"""create table for bananas
|
||||||
|
|
||||||
|
Revision ID: 2d7c4f9e8a1c
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-24 15:28:05.964682
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2d7c4f9e8a1c'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'bananas',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
sa.Column('quantity', 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())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('bananas')
|
@ -0,0 +1,16 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from helpers.banana_helpers import create_banana
|
||||||
|
from schemas.banana import BananaCreate, BananaSchema
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/banana", status_code=status.HTTP_201_CREATED, response_model=BananaSchema)
|
||||||
|
async def create_new_banana(
|
||||||
|
banana: BananaCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
new_banana = create_banana(db=db, banana_data=banana)
|
||||||
|
return new_banana
|
48
helpers/banana_helpers.py
Normal file
48
helpers/banana_helpers.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from typing import Optional, List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.banana import Banana
|
||||||
|
from schemas.banana import BananaCreate, BananaSchema
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
def get_banana_by_id(db: Session, banana_id: UUID) -> Optional[Banana]:
|
||||||
|
"""
|
||||||
|
Retrieves a banana by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
banana_id (UUID): The ID of the banana to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Banana]: The banana object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Banana).filter(Banana.id == banana_id).first()
|
||||||
|
|
||||||
|
def get_all_bananas(db: Session) -> List[BananaSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves all bananas from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[BananaSchema]: A list of all banana objects.
|
||||||
|
"""
|
||||||
|
bananas = db.query(Banana).all()
|
||||||
|
return [BananaSchema.from_orm(banana) for banana in bananas]
|
||||||
|
|
||||||
|
def create_banana(db: Session, banana_data: BananaCreate) -> BananaSchema:
|
||||||
|
"""
|
||||||
|
Creates a new banana in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
banana_data (BananaCreate): The data for the banana to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BananaSchema: The newly created banana object.
|
||||||
|
"""
|
||||||
|
db_banana = Banana(**banana_data.dict())
|
||||||
|
db.add(db_banana)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_banana)
|
||||||
|
return BananaSchema.from_orm(db_banana)
|
15
models/banana.py
Normal file
15
models/banana.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 Banana(Base):
|
||||||
|
__tablename__ = "bananas"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
|
quantity = 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
|
python-dotenv>=0.19.0
|
||||||
bcrypt>=3.2.0
|
bcrypt>=3.2.0
|
||||||
alembic>=1.13.1
|
alembic>=1.13.1
|
||||||
|
jose
|
||||||
|
passlib
|
||||||
|
pydantic
|
||||||
|
25
schemas/banana.py
Normal file
25
schemas/banana.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class BananaBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the banana")
|
||||||
|
description: Optional[str] = Field(None, description="Description of the banana")
|
||||||
|
quantity: int = Field(..., description="Quantity of bananas")
|
||||||
|
|
||||||
|
class BananaCreate(BananaBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BananaUpdate(BananaBase):
|
||||||
|
name: Optional[str] = Field(None, description="Name of the banana")
|
||||||
|
description: Optional[str] = Field(None, description="Description of the banana")
|
||||||
|
quantity: Optional[int] = Field(None, description="Quantity of bananas")
|
||||||
|
|
||||||
|
class BananaSchema(BananaBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user