Skip to content

Commit

Permalink
🔄 refactor get_weekly_trips and get_passenger_name to improve respons…
Browse files Browse the repository at this point in the history
…e handling and error reporting
  • Loading branch information
LokoMoloko98 committed Feb 1, 2025
1 parent 466385a commit aaee0da
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions swift-lift-trips-table-ops/swift-lift-trips-table-ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ def get_weekly_trips(passenger_id, target_date):
KeyConditionExpression=Key('passenger_id').eq(passenger_id) &
Key('trip_date_time').between(start_of_week_iso, end_of_week_iso)
)
# Check if Items exists and has at least one item
if response.get('Items') and len(response['Items']) > 0:
# Get the first (and should be only) item's passenger_name
return response['Items'][0]['passenger_name']
return None

return response.get('Items', [])

except Exception as e:
print(f"Error getting passenger name: {e}")
return None
print(f"An error occurred: {e}")
return []

def get_passenger_name(passenger_id):
"""
Expand All @@ -76,8 +72,9 @@ def get_passenger_name(passenger_id):
response = users_table.query(
KeyConditionExpression=Key('passenger_id').eq(passenger_id)
)
if 'Items' in response:
return response['Items']['passenger_name']
if response.get('Items') and len(response['Items']) > 0:
return response['Items'][0]['passenger_name']
return None
except Exception as e:
print(f"Error getting passenger name: {e}")
return None
Expand Down

0 comments on commit aaee0da

Please sign in to comment.