diff --git a/alembic/versions/20250428_120722_52c67285_update_framework.py b/alembic/versions/20250428_120722_52c67285_update_framework.py new file mode 100644 index 0000000..5c95087 --- /dev/null +++ b/alembic/versions/20250428_120722_52c67285_update_framework.py @@ -0,0 +1,32 @@ +"""create table for Framework +Revision ID: 2c9a8b7f6d22 +Revises: 0001 +Create Date: 2023-05-23 10:53:23.951271 +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '2c9a8b7f6d22' +down_revision = '0001' +branch_labels = None +depends_on = None + +def upgrade(): + table_name = "frameworks" + + 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('language', sa.String(), 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()), + sa.Index('ix_frameworks_name', 'name', unique=True) + ) + +def downgrade(): + table_name = "frameworks" + op.drop_table(table_name) \ No newline at end of file diff --git a/endpoints/dave.get.py b/endpoints/dave.get.py index e69de29..ebe7409 100644 --- a/endpoints/dave.get.py +++ b/endpoints/dave.get.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, Depends +from typing import List +from schemas.framework import FrameworkSchema +from helpers.framework_helpers import get_random_framework +from sqlalchemy.orm import Session +from core.database import get_db + +router = APIRouter() + +@router.get("/dave", response_model=List[FrameworkSchema]) +async def get_random_framework_endpoint(db: Session = Depends(get_db)): + framework = get_random_framework(db) + if framework: + return [framework] + else: + return [] \ No newline at end of file diff --git a/helpers/framework_helpers.py b/helpers/framework_helpers.py new file mode 100644 index 0000000..b6f0689 --- /dev/null +++ b/helpers/framework_helpers.py @@ -0,0 +1,21 @@ +import random +from typing import Optional +from sqlalchemy.orm import Session +from models.framework import Framework +from schemas.framework import FrameworkSchema + +def get_random_framework(db: Session) -> Optional[FrameworkSchema]: + """ + Retrieves a random framework from the database. + + Args: + db (Session): The database session. + + Returns: + Optional[FrameworkSchema]: The randomly selected framework schema object, or None if no frameworks exist. + """ + frameworks = db.query(Framework).all() + if not frameworks: + return None + random_framework = random.choice(frameworks) + return FrameworkSchema.from_orm(random_framework) \ No newline at end of file diff --git a/models/framework.py b/models/framework.py new file mode 100644 index 0000000..a1088a4 --- /dev/null +++ b/models/framework.py @@ -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 Framework(Base): + __tablename__ = "frameworks" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False, unique=True, index=True) + language = Column(String, 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/framework.py b/schemas/framework.py new file mode 100644 index 0000000..6a9a10f --- /dev/null +++ b/schemas/framework.py @@ -0,0 +1,23 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +from uuid import UUID + +class FrameworkBase(BaseModel): + name: str = Field(..., description="Name of the framework") + language: str = Field(..., description="Programming language associated with the framework") + +class FrameworkCreate(FrameworkBase): + pass + +class FrameworkUpdate(FrameworkBase): + name: Optional[str] = Field(None, description="Name of the framework") + language: Optional[str] = Field(None, description="Programming language associated with the framework") + +class FrameworkSchema(FrameworkBase): + id: UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file