feat: Add new endpoints/dave.get.py endpoint for Framework 🚀 📦 with updated dependencies

This commit is contained in:
Backend IM Bot 2025-04-28 12:07:33 +00:00
parent 2c7c5b0d63
commit 767afa1511
6 changed files with 109 additions and 0 deletions

View File

@ -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)

View File

@ -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 []

View File

@ -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)

14
models/framework.py Normal file
View File

@ -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())

View File

@ -7,3 +7,6 @@ sqlalchemy>=1.4.0
python-dotenv>=0.19.0
bcrypt>=3.2.0
alembic>=1.13.1
jose
passlib
pydantic

23
schemas/framework.py Normal file
View File

@ -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