Automated Action 3d02858340 Add divisibility by 3 functionality
- Add is_divisible_by_3 field to database model and schema
- Create migration script for the new field
- Update existing endpoints to check divisibility by 3
- Add dedicated endpoints for divisibility by 3
- Update README with new endpoint documentation
- Fix linting issues with ruff
2025-05-17 15:52:39 +00:00

172 lines
4.6 KiB
Python

from fastapi import APIRouter, Path as PathParam, Depends
from sqlalchemy.orm import Session
from typing import List
from app.database.connection import get_db
from app.models.number import NumberCheck
from app.schemas.number import NumberRequest, DivisibilityResponse, NumberCheckResponse
router = APIRouter()
@router.get("/", summary="API Root")
def read_root():
"""Welcome message for the API."""
return {"message": "Welcome to the Number Divisibility API"}
@router.get(
"/divisibility/{number}",
response_model=DivisibilityResponse,
summary="Check divisibility (GET)",
)
def check_divisibility(
number: int = PathParam(..., description="The number to check divisibility for"),
db: Session = Depends(get_db),
):
"""
Check if a number is divisible by 2 and by 3.
Returns a JSON response with the number and booleans indicating if it's divisible by 2 and by 3.
"""
is_divisible_by_2 = number % 2 == 0
is_divisible_by_3 = number % 3 == 0
# Log the check in the database
number_check = NumberCheck(
number=number,
is_divisible_by_2=is_divisible_by_2,
is_divisible_by_3=is_divisible_by_3,
)
db.add(number_check)
db.commit()
db.refresh(number_check)
return {
"number": number,
"is_divisible_by_2": is_divisible_by_2,
"is_divisible_by_3": is_divisible_by_3,
}
@router.post(
"/divisibility",
response_model=DivisibilityResponse,
summary="Check divisibility (POST)",
)
def check_divisibility_post(request: NumberRequest, db: Session = Depends(get_db)):
"""
Check if a number is divisible by 2 and by 3 using POST request.
Returns a JSON response with the number and booleans indicating if it's divisible by 2 and by 3.
"""
is_divisible_by_2 = request.number % 2 == 0
is_divisible_by_3 = request.number % 3 == 0
# Log the check in the database
number_check = NumberCheck(
number=request.number,
is_divisible_by_2=is_divisible_by_2,
is_divisible_by_3=is_divisible_by_3,
)
db.add(number_check)
db.commit()
db.refresh(number_check)
return {
"number": request.number,
"is_divisible_by_2": is_divisible_by_2,
"is_divisible_by_3": is_divisible_by_3,
}
@router.get(
"/history", response_model=List[NumberCheckResponse], summary="Check history"
)
def get_check_history(db: Session = Depends(get_db)):
"""
Get the history of number divisibility checks.
Returns a list of all number checks.
"""
return db.query(NumberCheck).all()
@router.get(
"/divisibility/by3/{number}",
response_model=DivisibilityResponse,
summary="Check divisibility by 3 (GET)",
)
def check_divisibility_by3(
number: int = PathParam(..., description="The number to check divisibility for"),
db: Session = Depends(get_db),
):
"""
Check if a number is divisible by 3.
Returns a JSON response with the number and booleans indicating if it's divisible by 2 and by 3.
"""
is_divisible_by_2 = number % 2 == 0
is_divisible_by_3 = number % 3 == 0
# Log the check in the database
number_check = NumberCheck(
number=number,
is_divisible_by_2=is_divisible_by_2,
is_divisible_by_3=is_divisible_by_3,
)
db.add(number_check)
db.commit()
db.refresh(number_check)
return {
"number": number,
"is_divisible_by_2": is_divisible_by_2,
"is_divisible_by_3": is_divisible_by_3,
}
@router.post(
"/divisibility/by3",
response_model=DivisibilityResponse,
summary="Check divisibility by 3 (POST)",
)
def check_divisibility_by3_post(request: NumberRequest, db: Session = Depends(get_db)):
"""
Check if a number is divisible by 3 using POST request.
Returns a JSON response with the number and booleans indicating if it's divisible by 2 and by 3.
"""
is_divisible_by_2 = request.number % 2 == 0
is_divisible_by_3 = request.number % 3 == 0
# Log the check in the database
number_check = NumberCheck(
number=request.number,
is_divisible_by_2=is_divisible_by_2,
is_divisible_by_3=is_divisible_by_3,
)
db.add(number_check)
db.commit()
db.refresh(number_check)
return {
"number": request.number,
"is_divisible_by_2": is_divisible_by_2,
"is_divisible_by_3": is_divisible_by_3,
}
@router.get("/health", summary="Health check")
def health_check():
"""
Health check endpoint to verify the API is running.
Returns a JSON response with status information.
"""
return {
"status": "healthy",
"api_version": "1.0.0",
"service": "Number Divisibility API",
}