From c15e257dea263de9ab563270813be3c0dd16bffa Mon Sep 17 00:00:00 2001 From: Helldragon67 Date: Tue, 11 Feb 2025 15:42:14 +0100 Subject: [PATCH] add trip time update --- backend/src/main.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/backend/src/main.py b/backend/src/main.py index 0c8c85f..9500f85 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -179,6 +179,45 @@ def get_landmark(landmark_uuid: str) -> Landmark: raise HTTPException(status_code=404, detail="Landmark not found") from exc +@app.post("/trip/recompute-time/{trip_uuid}/{removed_landmark_uuid}") +def update_trip_time(trip_uuid: str, removed_landmark_uuid: str) -> Trip: + """ + Updates the reaching times of a given trip when removing a landmark. + + Args: + landmark_uuid (str) : unique identifier for a Landmark. + + Returns: + (Landmark) : the corresponding Landmark. + """ + # First, fetch the trip in the cache. + try: + trip = cache_client.get(f'trip_{trip_uuid}') + except KeyError as exc: + raise HTTPException(status_code=404, detail='Trip not found') from exc + + landmarks = [] + next_uuid = trip.first_landmark_uuid + + # Extract landmarks + try : + while next_uuid is not None: + landmark = cache_client.get(f'landmark_{next_uuid}') + # Filter out the removed landmark. + if next_uuid != removed_landmark_uuid : + landmarks.append(landmark) + next_uuid = landmark.next_uuid # Prepare for the next iteration + except KeyError as exc: + raise HTTPException(status_code=404, detail=f'landmark {next_uuid} not found') from exc + + # Re-link every thing and compute times again + linked_tour = LinkedLandmarks(landmarks) + trip = Trip.from_linked_landmarks(linked_tour, cache_client) + + return trip + + + @app.post("/toilets/new") def get_toilets(location: tuple[float, float] = Query(...), radius: int = 500) -> list[Toilets] : """