Skip to content

Commit 8655b57

Browse files
MaxBetzDLRNXXR
andauthored
Add functions for update of description (#20)
* Add functions for update of description * Apply suggestions from code review Co-authored-by: Moritz Zeumer <25636783+NXXR@users.noreply.github.com> * change endpoint name to avoid overlap --------- Co-authored-by: Moritz Zeumer <25636783+NXXR@users.noreply.github.com> Co-authored-by: Zeumer, Moritz <moritz.zeumer@dlr.de>
1 parent d60b632 commit 8655b57

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

api/src/api/app/apis/scenarios_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ async def import_scenario_data(
127127
log.info(f'PUT /scenarios/{scenarioId} received...')
128128
return await controller.import_scenario_data(scenarioId, file)
129129

130+
@router.put(
131+
"/scenarios/{scenarioId}/description",
132+
responses={
133+
200: {"model": ReducedScenario, "description": "Updated description of scenario."},
134+
},
135+
tags=["Scenarios"],
136+
response_model_by_alias=True,
137+
)
138+
async def update_scenario_description(
139+
scenarioId: StrictStr = Path(..., description="UUID of the scenario"),
140+
description: StrictStr = Body(..., description="New description for the scenario")
141+
) -> ReducedScenario:
142+
"""Update description of a scenario."""
143+
log.info(f'PUT /scenarios/{scenarioId} received...')
144+
return await controller.update_scenario_description(scenarioId, description)
130145

131146
@router.get(
132147
"/scenarios",

api/src/api/app/controller/scenario_controller.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
group_get_all,
4141
compartment_get_all,
4242
node_get_by_list,
43-
datapoint_update_all_by_scenario
43+
datapoint_update_all_by_scenario,
44+
scenario_update_description
4445
)
4546

4647
class LookupObject:
@@ -283,4 +284,12 @@ async def _read_percentiles(
283284
percentile=percentile,
284285
value=value
285286
))
286-
return datapoints
287+
return datapoints
288+
289+
async def update_scenario_description(
290+
self,
291+
scenarioId: StrictStr,
292+
description: StrictStr,
293+
) -> ReducedScenario:
294+
"""Update description of a scenario."""
295+
return scenario_update_description(scenarioId, description)

api/src/api/app/db/tasks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,29 @@ def datapoint_update_all_by_scenario(
661661
session.add(scenario)
662662
session.commit()
663663
return
664+
665+
666+
def scenario_update_description(
667+
scenarioId: StrictStr,
668+
description: StrictStr
669+
) -> ID:
670+
query = select(db.Scenario).where(db.Scenario.id == scenarioId)
671+
with next(get_session()) as session:
672+
scenario: db.Scenario = session.exec(query).one_or_none()
673+
if not scenario:
674+
raise HTTPException(status_code=404, detail='A scenario with this ID does not exist')
675+
676+
scenario.description = description
677+
session.add(scenario)
678+
session.commit()
679+
session.refresh(scenario)
680+
return ReducedScenario(
681+
id=str(scenario.id),
682+
name=scenario.name,
683+
description=scenario.description,
684+
startDate=scenario.startDate,
685+
endDate=scenario.endDate,
686+
percentiles=[int(perc) for perc in scenario.percentiles.split(',')],
687+
timestampSubmitted=scenario.timestampSubmitted,
688+
timestampSimulated=scenario.timestampSimulated,
689+
)

0 commit comments

Comments
 (0)