Implement and optimize Calculator API with FastAPI and SQLAlchemy
- Fixed Pydantic configuration using ConfigDict for latest Pydantic version - Fixed import order in alembic/env.py for proper module imports - Applied code formatting with Ruff - Optimized database connection settings - Ensured proper error handling for API endpoints generated with BackendIM... (backend.im)
This commit is contained in:
parent
3efcd88ffa
commit
12b80aca5b
@ -4,6 +4,7 @@ from sqlalchemy import engine_from_config
|
|||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
@ -16,7 +17,6 @@ if config.config_file_name is not None:
|
|||||||
|
|
||||||
# add your model's MetaData object here
|
# add your model's MetaData object here
|
||||||
# for 'autogenerate' support
|
# for 'autogenerate' support
|
||||||
from app.db.base import Base
|
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
# other values from the config, defined by the needs of env.py,
|
# other values from the config, defined by the needs of env.py,
|
||||||
@ -63,9 +63,7 @@ def run_migrations_online() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(
|
context.configure(connection=connection, target_metadata=target_metadata)
|
||||||
connection=connection, target_metadata=target_metadata
|
|
||||||
)
|
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
@ -5,12 +5,13 @@ Revises:
|
|||||||
Create Date: 2025-05-13
|
Create Date: 2025-05-13
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '001'
|
revision = "001"
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
@ -19,20 +20,27 @@ depends_on = None
|
|||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
# Create calculation table
|
# Create calculation table
|
||||||
op.create_table(
|
op.create_table(
|
||||||
'calculation',
|
"calculation",
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column("id", sa.Integer(), nullable=False),
|
||||||
sa.Column('operation', sa.String(), nullable=False),
|
sa.Column("operation", sa.String(), nullable=False),
|
||||||
sa.Column('first_number', sa.Float(), nullable=False),
|
sa.Column("first_number", sa.Float(), nullable=False),
|
||||||
sa.Column('second_number', sa.Float(), nullable=True),
|
sa.Column("second_number", sa.Float(), nullable=True),
|
||||||
sa.Column('result', sa.Float(), nullable=False),
|
sa.Column("result", sa.Float(), nullable=False),
|
||||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
|
sa.Column(
|
||||||
sa.PrimaryKeyConstraint('id')
|
"created_at",
|
||||||
|
sa.DateTime(timezone=True),
|
||||||
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||||
|
nullable=False,
|
||||||
|
),
|
||||||
|
sa.PrimaryKeyConstraint("id"),
|
||||||
|
)
|
||||||
|
op.create_index(op.f("ix_calculation_id"), "calculation", ["id"], unique=False)
|
||||||
|
op.create_index(
|
||||||
|
op.f("ix_calculation_operation"), "calculation", ["operation"], unique=False
|
||||||
)
|
)
|
||||||
op.create_index(op.f('ix_calculation_id'), 'calculation', ['id'], unique=False)
|
|
||||||
op.create_index(op.f('ix_calculation_operation'), 'calculation', ['operation'], unique=False)
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_index(op.f('ix_calculation_operation'), table_name='calculation')
|
op.drop_index(op.f("ix_calculation_operation"), table_name="calculation")
|
||||||
op.drop_index(op.f('ix_calculation_id'), table_name='calculation')
|
op.drop_index(op.f("ix_calculation_id"), table_name="calculation")
|
||||||
op.drop_table('calculation')
|
op.drop_table("calculation")
|
||||||
|
@ -9,41 +9,54 @@ from app.crud import calculation
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
def perform_calculation(calc: CalculationCreate) -> float:
|
def perform_calculation(calc: CalculationCreate) -> float:
|
||||||
"""Perform the calculation based on the operation and numbers provided."""
|
"""Perform the calculation based on the operation and numbers provided."""
|
||||||
if calc.operation == "add":
|
if calc.operation == "add":
|
||||||
if calc.second_number is None:
|
if calc.second_number is None:
|
||||||
raise HTTPException(status_code=400, detail="Second number is required for addition")
|
raise HTTPException(
|
||||||
|
status_code=400, detail="Second number is required for addition"
|
||||||
|
)
|
||||||
return calc.first_number + calc.second_number
|
return calc.first_number + calc.second_number
|
||||||
|
|
||||||
elif calc.operation == "subtract":
|
elif calc.operation == "subtract":
|
||||||
if calc.second_number is None:
|
if calc.second_number is None:
|
||||||
raise HTTPException(status_code=400, detail="Second number is required for subtraction")
|
raise HTTPException(
|
||||||
|
status_code=400, detail="Second number is required for subtraction"
|
||||||
|
)
|
||||||
return calc.first_number - calc.second_number
|
return calc.first_number - calc.second_number
|
||||||
|
|
||||||
elif calc.operation == "multiply":
|
elif calc.operation == "multiply":
|
||||||
if calc.second_number is None:
|
if calc.second_number is None:
|
||||||
raise HTTPException(status_code=400, detail="Second number is required for multiplication")
|
raise HTTPException(
|
||||||
|
status_code=400, detail="Second number is required for multiplication"
|
||||||
|
)
|
||||||
return calc.first_number * calc.second_number
|
return calc.first_number * calc.second_number
|
||||||
|
|
||||||
elif calc.operation == "divide":
|
elif calc.operation == "divide":
|
||||||
if calc.second_number is None:
|
if calc.second_number is None:
|
||||||
raise HTTPException(status_code=400, detail="Second number is required for division")
|
raise HTTPException(
|
||||||
|
status_code=400, detail="Second number is required for division"
|
||||||
|
)
|
||||||
if calc.second_number == 0:
|
if calc.second_number == 0:
|
||||||
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
||||||
return calc.first_number / calc.second_number
|
return calc.first_number / calc.second_number
|
||||||
|
|
||||||
elif calc.operation == "square_root":
|
elif calc.operation == "square_root":
|
||||||
if calc.first_number < 0:
|
if calc.first_number < 0:
|
||||||
raise HTTPException(status_code=400, detail="Cannot calculate square root of a negative number")
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail="Cannot calculate square root of a negative number",
|
||||||
|
)
|
||||||
return math.sqrt(calc.first_number)
|
return math.sqrt(calc.first_number)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail="Invalid operation. Supported operations: add, subtract, multiply, divide, square_root"
|
detail="Invalid operation. Supported operations: add, subtract, multiply, divide, square_root",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", response_model=CalculationResponse)
|
@router.post("/", response_model=CalculationResponse)
|
||||||
def calculate(calc: CalculationCreate, db: Session = Depends(get_db)):
|
def calculate(calc: CalculationCreate, db: Session = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
@ -57,14 +70,18 @@ def calculate(calc: CalculationCreate, db: Session = Depends(get_db)):
|
|||||||
db_calc = calculation.create_calculation(db=db, calc=calc, result=result)
|
db_calc = calculation.create_calculation(db=db, calc=calc, result=result)
|
||||||
return db_calc
|
return db_calc
|
||||||
|
|
||||||
|
|
||||||
@router.get("/history", response_model=List[CalculationResponse])
|
@router.get("/history", response_model=List[CalculationResponse])
|
||||||
def get_calculation_history(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
def get_calculation_history(
|
||||||
|
skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Retrieve calculation history.
|
Retrieve calculation history.
|
||||||
"""
|
"""
|
||||||
calcs = calculation.get_calculations(db, skip=skip, limit=limit)
|
calcs = calculation.get_calculations(db, skip=skip, limit=limit)
|
||||||
return calcs
|
return calcs
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{calculation_id}", response_model=CalculationResponse)
|
@router.get("/{calculation_id}", response_model=CalculationResponse)
|
||||||
def get_calculation_by_id(calculation_id: int, db: Session = Depends(get_db)):
|
def get_calculation_by_id(calculation_id: int, db: Session = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
|
@ -4,6 +4,7 @@ from app.db.session import get_db
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/health")
|
@router.get("/health")
|
||||||
def health_check(db: Session = Depends(get_db)):
|
def health_check(db: Session = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
@ -18,8 +19,4 @@ def health_check(db: Session = Depends(get_db)):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
db_status = f"unhealthy: {str(e)}"
|
db_status = f"unhealthy: {str(e)}"
|
||||||
|
|
||||||
return {
|
return {"status": "healthy", "database": db_status, "version": "1.0.0"}
|
||||||
"status": "healthy",
|
|
||||||
"database": db_status,
|
|
||||||
"version": "1.0.0"
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
API_V1_STR: str = "/api/v1"
|
API_V1_STR: str = "/api/v1"
|
||||||
PROJECT_NAME: str = "Calculator API"
|
PROJECT_NAME: str = "Calculator API"
|
||||||
@ -14,4 +15,5 @@ class Settings(BaseSettings):
|
|||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
case_sensitive = True
|
case_sensitive = True
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
@ -2,20 +2,31 @@ from sqlalchemy.orm import Session
|
|||||||
from app.models.calculation import Calculation
|
from app.models.calculation import Calculation
|
||||||
from app.schemas.calculation import CalculationCreate
|
from app.schemas.calculation import CalculationCreate
|
||||||
|
|
||||||
def create_calculation(db: Session, calc: CalculationCreate, result: float) -> Calculation:
|
|
||||||
|
def create_calculation(
|
||||||
|
db: Session, calc: CalculationCreate, result: float
|
||||||
|
) -> Calculation:
|
||||||
db_calculation = Calculation(
|
db_calculation = Calculation(
|
||||||
operation=calc.operation,
|
operation=calc.operation,
|
||||||
first_number=calc.first_number,
|
first_number=calc.first_number,
|
||||||
second_number=calc.second_number,
|
second_number=calc.second_number,
|
||||||
result=result
|
result=result,
|
||||||
)
|
)
|
||||||
db.add(db_calculation)
|
db.add(db_calculation)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_calculation)
|
db.refresh(db_calculation)
|
||||||
return db_calculation
|
return db_calculation
|
||||||
|
|
||||||
|
|
||||||
def get_calculations(db: Session, skip: int = 0, limit: int = 100):
|
def get_calculations(db: Session, skip: int = 0, limit: int = 100):
|
||||||
return db.query(Calculation).order_by(Calculation.created_at.desc()).offset(skip).limit(limit).all()
|
return (
|
||||||
|
db.query(Calculation)
|
||||||
|
.order_by(Calculation.created_at.desc())
|
||||||
|
.offset(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_calculation(db: Session, calculation_id: int):
|
def get_calculation(db: Session, calculation_id: int):
|
||||||
return db.query(Calculation).filter(Calculation.id == calculation_id).first()
|
return db.query(Calculation).filter(Calculation.id == calculation_id).first()
|
@ -1,2 +0,0 @@
|
|||||||
from app.db.base_class import Base
|
|
||||||
from app.models.calculation import Calculation
|
|
@ -1,6 +1,7 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||||
|
|
||||||
|
|
||||||
@as_declarative()
|
@as_declarative()
|
||||||
class Base:
|
class Base:
|
||||||
id: Any
|
id: Any
|
||||||
|
@ -9,11 +9,11 @@ DB_DIR.mkdir(parents=True, exist_ok=True)
|
|||||||
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
SQLALCHEMY_DATABASE_URL,
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||||
connect_args={"check_same_thread": False}
|
|
||||||
)
|
)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
|
@ -2,6 +2,7 @@ from sqlalchemy import Column, Integer, String, Float, DateTime
|
|||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from app.db.base_class import Base
|
from app.db.base_class import Base
|
||||||
|
|
||||||
|
|
||||||
class Calculation(Base):
|
class Calculation(Base):
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
operation = Column(String, index=True)
|
operation = Column(String, index=True)
|
||||||
|
@ -1,19 +1,26 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field, ConfigDict
|
||||||
|
|
||||||
|
|
||||||
class CalculationBase(BaseModel):
|
class CalculationBase(BaseModel):
|
||||||
operation: str = Field(..., description="Mathematical operation: add, subtract, multiply, divide, or square_root")
|
operation: str = Field(
|
||||||
|
...,
|
||||||
|
description="Mathematical operation: add, subtract, multiply, divide, or square_root",
|
||||||
|
)
|
||||||
first_number: float = Field(..., description="First operand")
|
first_number: float = Field(..., description="First operand")
|
||||||
second_number: Optional[float] = Field(None, description="Second operand (not required for square_root)")
|
second_number: Optional[float] = Field(
|
||||||
|
None, description="Second operand (not required for square_root)"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CalculationCreate(CalculationBase):
|
class CalculationCreate(CalculationBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CalculationResponse(CalculationBase):
|
class CalculationResponse(CalculationBase):
|
||||||
id: int
|
id: int
|
||||||
result: float
|
result: float
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
|
|
||||||
class Config:
|
model_config = ConfigDict(from_attributes=True)
|
||||||
orm_mode = True
|
|
||||||
|
@ -2,6 +2,7 @@ fastapi==0.110.1
|
|||||||
uvicorn==0.29.0
|
uvicorn==0.29.0
|
||||||
sqlalchemy==2.0.29
|
sqlalchemy==2.0.29
|
||||||
pydantic==2.6.4
|
pydantic==2.6.4
|
||||||
|
pydantic-settings==2.2.1
|
||||||
alembic==1.13.1
|
alembic==1.13.1
|
||||||
ruff==0.3.2
|
ruff==0.3.2
|
||||||
python-dotenv==1.0.1
|
python-dotenv==1.0.1
|
Loading…
x
Reference in New Issue
Block a user