scenario
create_scenario(token, scenario_data, db=Depends(get_db_session))
async
¶
Creates a new scenario and stores it in the database. The endpoint is protected and requires a valid token. It validates the ownership of the project before proceeding. This function adds a new scenario to the database and commits the changes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
Annotated[str, Depends(oauth2_scheme)]
|
A valid authentication token to verify the user's identity. |
required |
scenario_data
|
EnScenario
|
The data object containing the scenario details. |
required |
db
|
Session
|
The database session dependency for executing queries. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A response indicating the success of the scenario creation. |
Source code in backend/app/scenario/router.py
delete_scenario(token, scenario_id, db=Depends(get_db_session))
async
¶
Deletes a scenario by its ID for authorized users only. This endpoint ensures that the caller owns the specified scenario before performing the deletion. If the scenario exists and the authorization is confirmed, it is removed from the database, and a success message is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
Annotated[str, Depends(oauth2_scheme)]
|
Access token for authentication and authorization. |
required |
scenario_id
|
int
|
Unique identifier of the scenario to be deleted. |
required |
db
|
Session
|
Database session used for querying and deleting the scenario. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
MessageResponse confirming successful deletion. |
Source code in backend/app/scenario/router.py
read_scenario(scenario_id, token, db=Depends(get_db_session))
async
¶
Retrieve scenario details by scenario ID.
This endpoint allows an authenticated user to fetch information about a specific scenario based on its ID. It first validates the user's authentication and ownership of the scenario and associated project before retrieving the data. If the authentication or ownership checks fail, appropriate HTTP exceptions are raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scenario_id
|
int
|
ID of the scenario to fetch |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
Authentication token for the user |
required |
db
|
Session
|
Database session dependency. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
DataResponse
|
Response containing the scenario data |
Source code in backend/app/scenario/router.py
read_scenarios(project_id, token, db=Depends(get_db_session))
async
¶
Reads scenarios associated with a specific project based on the provided project ID. This route ensures that the user is both authenticated and authorized before retrieving the scenarios, which are fetched from the database using the project ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_id
|
int
|
The ID of the project whose scenarios need to be retrieved. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
OAuth2-compliant access token used for user authentication. |
required |
db
|
Session
|
The database session dependency is used to access database functions. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
DataResponse
|
A structured response containing the scenario data and a success flag. |
Raises:
Type | Description |
---|---|
HTTPException
|
If the user is not authenticated (HTTP 401). |
HTTPException
|
If the user is not authorized to access the project (HTTP 401). |
Source code in backend/app/scenario/router.py
update_scenario(token, scenario_id, scenario_data, db=Depends(get_db_session))
async
¶
Updates an existing scenario identified by its ID. This endpoint allows updating
specific fields of a scenario with new data provided in the scenario_data
object.
The function requires authentication and ownership validation to proceed. If the
provided token is invalid or the user is not authorized, the operation will not
be performed. Additionally, it checks whether the specified scenario exists in
the database before attempting any updates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
Annotated[str, Depends(oauth2_scheme)]
|
A bearer token for authorization that validates the user's access and ownership of the scenario. |
required |
scenario_id
|
int
|
The unique identifier of the scenario to be updated. |
required |
scenario_data
|
EnScenarioUpdate
|
Data for updating the specified scenario. Only fields that are included in this object and are allowed to be updated will be modified. |
required |
db
|
Session
|
A database session to interact with the scenario database. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A |
Raises:
Type | Description |
---|---|
HTTPException
|
Raised when the token is invalid, the user is unauthorized, the scenario does not exist in the database, or any other issue preventing the update operation occurs. |
Source code in backend/app/scenario/router.py
validate_scenario_owner(scenario_id, db, token)
¶
Validates whether the owner of a given scenario matches the user from the provided authentication token. This function ensures that the logged-in user has the authorization to access or modify the scenario.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scenario_id
|
int
|
ID of the scenario to validate ownership for. |
required |
db
|
Session
|
Database session for executing queries and retrieving data. Dependency injection. |
required |
token
|
str
|
Authentication token representing the logged-in user. |
required |
Returns:
Type | Description |
---|---|
tuple(bool, int, str)
|
A tuple containing three values: - A boolean indicating whether the user is the owner of the scenario. - An HTTP status code indicating the result of the validation. - A string message explaining the result (empty string if validation is successful). |