
- 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)
94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
import math
|
|
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.schemas.calculation import CalculationCreate, CalculationResponse
|
|
from app.crud import calculation
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def perform_calculation(calc: CalculationCreate) -> float:
|
|
"""Perform the calculation based on the operation and numbers provided."""
|
|
if calc.operation == "add":
|
|
if calc.second_number is None:
|
|
raise HTTPException(
|
|
status_code=400, detail="Second number is required for addition"
|
|
)
|
|
return calc.first_number + calc.second_number
|
|
|
|
elif calc.operation == "subtract":
|
|
if calc.second_number is None:
|
|
raise HTTPException(
|
|
status_code=400, detail="Second number is required for subtraction"
|
|
)
|
|
return calc.first_number - calc.second_number
|
|
|
|
elif calc.operation == "multiply":
|
|
if calc.second_number is None:
|
|
raise HTTPException(
|
|
status_code=400, detail="Second number is required for multiplication"
|
|
)
|
|
return calc.first_number * calc.second_number
|
|
|
|
elif calc.operation == "divide":
|
|
if calc.second_number is None:
|
|
raise HTTPException(
|
|
status_code=400, detail="Second number is required for division"
|
|
)
|
|
if calc.second_number == 0:
|
|
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
|
return calc.first_number / calc.second_number
|
|
|
|
elif calc.operation == "square_root":
|
|
if calc.first_number < 0:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Cannot calculate square root of a negative number",
|
|
)
|
|
return math.sqrt(calc.first_number)
|
|
|
|
else:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Invalid operation. Supported operations: add, subtract, multiply, divide, square_root",
|
|
)
|
|
|
|
|
|
@router.post("/", response_model=CalculationResponse)
|
|
def calculate(calc: CalculationCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Perform a calculation and store it in the database.
|
|
|
|
- **operation**: add, subtract, multiply, divide, or square_root
|
|
- **first_number**: First operand
|
|
- **second_number**: Second operand (not required for square_root)
|
|
"""
|
|
result = perform_calculation(calc)
|
|
db_calc = calculation.create_calculation(db=db, calc=calc, result=result)
|
|
return db_calc
|
|
|
|
|
|
@router.get("/history", response_model=List[CalculationResponse])
|
|
def get_calculation_history(
|
|
skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Retrieve calculation history.
|
|
"""
|
|
calcs = calculation.get_calculations(db, skip=skip, limit=limit)
|
|
return calcs
|
|
|
|
|
|
@router.get("/{calculation_id}", response_model=CalculationResponse)
|
|
def get_calculation_by_id(calculation_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Retrieve a specific calculation by ID.
|
|
"""
|
|
db_calc = calculation.get_calculation(db, calculation_id=calculation_id)
|
|
if db_calc is None:
|
|
raise HTTPException(status_code=404, detail="Calculation not found")
|
|
return db_calc
|