From 2d1d6ce66fed279ba5c1f7cfe6b02d831d73c1f9 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Silva Date: Fri, 17 May 2024 16:27:33 +0100 Subject: [PATCH] Added API endpoint for listing coverage configuration parameters --- arpav_ppcv/webapp/api_v2/routers/coverages.py | 28 +++++++++++++++++ arpav_ppcv/webapp/api_v2/schemas/coverages.py | 31 ++++++++++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/arpav_ppcv/webapp/api_v2/routers/coverages.py b/arpav_ppcv/webapp/api_v2/routers/coverages.py index 72fdfc70..3f5f186b 100644 --- a/arpav_ppcv/webapp/api_v2/routers/coverages.py +++ b/arpav_ppcv/webapp/api_v2/routers/coverages.py @@ -37,6 +37,34 @@ logger = logging.getLogger(__name__) router = APIRouter() +@router.get( + "/configuration-parameters", + response_model=coverage_schemas.ConfigurationParameterList +) +async def list_configuration_parameters( + request: Request, + db_session: Annotated[Session, Depends(dependencies.get_db_session)], + list_params: Annotated[dependencies.CommonListFilterParameters, Depends()], +): + """List configuration parameters.""" + config_params, filtered_total = db.list_configuration_parameters( + db_session, + limit=list_params.limit, + offset=list_params.offset, + include_total=True + ) + _, unfiltered_total = db.list_configuration_parameters( + db_session, limit=1, offset=0, include_total=True + ) + return coverage_schemas.ConfigurationParameterList.from_items( + config_params, + request, + limit=list_params.limit, + offset=list_params.offset, + filtered_total=filtered_total, + unfiltered_total=unfiltered_total + ) + @router.get( "/coverage-configurations", diff --git a/arpav_ppcv/webapp/api_v2/schemas/coverages.py b/arpav_ppcv/webapp/api_v2/schemas/coverages.py index 9b6fb3e5..f7594f9a 100644 --- a/arpav_ppcv/webapp/api_v2/schemas/coverages.py +++ b/arpav_ppcv/webapp/api_v2/schemas/coverages.py @@ -9,9 +9,30 @@ from ....schemas import coverages as app_models -class ForecastModelScenario(pydantic.BaseModel): +class ConfigurationParameterValueEmbeddedInConfigurationParameter(pydantic.BaseModel): name: str - code: str + description: str + + +class ConfigurationParameterReadListItem(pydantic.BaseModel): + name: str + description: str + allowed_values: list[ConfigurationParameterValueEmbeddedInConfigurationParameter] + + @classmethod + def from_db_instance( + cls, + instance: app_models.ConfigurationParameter, + request: Request, + ): + return cls( + **instance.model_dump(), + allowed_values=[ + ConfigurationParameterValueEmbeddedInConfigurationParameter( + **pv.model_dump() + ) for pv in instance.allowed_values + ] + ) class ConfigurationParameterPossibleValueRead(pydantic.BaseModel): @@ -92,8 +113,10 @@ class CoverageIdentifierList(WebResourceList): path_operation_name = "list_coverage_identifiers" -class ForecastModelScenarioList(WebResourceList): - items: list[ForecastModelScenario] +class ConfigurationParameterList(WebResourceList): + items: list[ConfigurationParameterReadListItem] + list_item_type = ConfigurationParameterReadListItem + path_operation_name = "list_configuration_parameters" class TimeSeriesItem(pydantic.BaseModel):