diff --git a/alembic/versions/20250430_093939_f8516f9e_update_banana.py b/alembic/versions/20250430_093939_f8516f9e_update_banana.py new file mode 100644 index 0000000..21a5521 --- /dev/null +++ b/alembic/versions/20250430_093939_f8516f9e_update_banana.py @@ -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') \ No newline at end of file diff --git a/endpoints/banana.post.py b/endpoints/banana.post.py index e69de29..eb2014f 100644 --- a/endpoints/banana.post.py +++ b/endpoints/banana.post.py @@ -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 \ No newline at end of file diff --git a/helpers/banana_helpers.py b/helpers/banana_helpers.py new file mode 100644 index 0000000..9277561 --- /dev/null +++ b/helpers/banana_helpers.py @@ -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) \ No newline at end of file diff --git a/models/banana.py b/models/banana.py new file mode 100644 index 0000000..1666ddf --- /dev/null +++ b/models/banana.py @@ -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()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..db12c92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,6 @@ sqlalchemy>=1.4.0 python-dotenv>=0.19.0 bcrypt>=3.2.0 alembic>=1.13.1 +jose +passlib +pydantic diff --git a/schemas/banana.py b/schemas/banana.py new file mode 100644 index 0000000..be3a13f --- /dev/null +++ b/schemas/banana.py @@ -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 \ No newline at end of file