Documentation de l’API (Database & CRUD)¶
Ce module gère la connexion à la base de données et les opérations CRUD.
Connexion¶
Database connection configuration using SQLAlchemy.
This module initializes the database engine and session used by the API. The application uses a local SQLite database stored in app_api/data/.
CRUD¶
CRUD operations for the Operation model with automatic result calculation.
Provides functions to insert, retrieve, update, and delete operations from the database.
- app_api.modules.crud.calculate_result(operation: str, a: float, b: float | None = None) float[source]¶
Calculate the result of a mathematical operation.
- Parameters:
operation (str) – Operation type (“add”, “sub”, “square”).
a (float) – First operand.
b (float | None) – Second operand (optional for “square”).
- Returns:
Result of the operation.
- Return type:
float
- app_api.modules.crud.create_data(db: Session, operation: str, a: float, b: float | None = None) Operation[source]¶
Insert a new operation into the database with computed result.
- Parameters:
db (Session) – Database session
operation (str) – Operation type
a (float) – First operand
b (float | None) – Second operand (optional)
- Returns:
The newly created Operation object
- Return type:
- app_api.modules.crud.delete_operation(db: Session, operation_id: int) bool[source]¶
Delete an operation from the database.
- Parameters:
db (Session) – Database session
operation_id (int) – ID of the operation to delete
- Returns:
True if deleted, False if not found
- Return type:
bool
- app_api.modules.crud.get_all_data(db: Session) list[Operation][source]¶
Retrieve all operations from the database.
- Parameters:
db (Session) – Database session
- Returns:
List of Operation objects
- Return type:
list[Operation]
- app_api.modules.crud.get_operation(db: Session, operation_id: int) Operation | None[source]¶
Retrieve a single operation by ID.
- Parameters:
db (Session) – Database session
operation_id (int) – ID of the operation
- Returns:
The operation object or None if not found
- Return type:
Operation | None
- app_api.modules.crud.update_operation(db: Session, operation_id: int, operation: str, a: float, b: float | None = None) Operation[source]¶
Update an existing operation and recalculate result.
- Parameters:
db (Session) – Database session
operation_id (int) – ID of the operation to update
operation (str) – New operation type
a (float) – New first operand
b (float | None) – New second operand (optional)
- Returns:
Updated operation object
- Return type:
- Raises:
ValueError – If operation ID does not exist