Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion backend/routes/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, request
from flask_login import login_required, current_user

api_bp = Blueprint('api', __name__)
Expand All @@ -13,3 +13,37 @@ def health_check():
def get_profile():
"""Get current user profile"""
return jsonify(current_user.to_dict())


# --- New endpoint: Nearby Charging Stations ---
@api_bp.route('/nearby_stations')
def nearby_stations():
"""Return a list of nearby charging stations for given lat/lng (mock data)"""
lat = request.args.get('lat')
lng = request.args.get('lng')
try:
if lat is None or lng is None:
raise ValueError("Missing lat/lng parameters")
lat = float(lat)
lng = float(lng)
except (ValueError, TypeError):
return jsonify({"error": "Invalid or missing lat/lng parameters"}), 400

# Mock data for demonstration
stations = [
{
"id": 1,
"name": "ChargeBnB Station Downtown",
"lat": lat + 0.001,
"lng": lng + 0.001,
"address": "123 Main St, Cityville"
},
{
"id": 2,
"name": "ChargeBnB Station Uptown",
"lat": lat - 0.001,
"lng": lng - 0.001,
"address": "456 Oak Ave, Cityville"
}
]
return jsonify({"stations": stations})
32 changes: 32 additions & 0 deletions backend/tests/test_nearby_stations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

def test_nearby_stations_success(client):
"""Should return a list of nearby charging stations for valid coordinates"""
response = client.get('/api/nearby_stations?lat=37.7749&lng=-122.4194')
assert response.status_code == 200
data = response.get_json()
assert isinstance(data, dict)
assert 'stations' in data
assert isinstance(data['stations'], list)
# Optionally check structure of a station
if data['stations']:
station = data['stations'][0]
assert 'id' in station
assert 'name' in station
assert 'lat' in station
assert 'lng' in station
assert 'address' in station

def test_nearby_stations_missing_params(client):
"""Should return 400 if lat/lng params are missing"""
response = client.get('/api/nearby_stations')
assert response.status_code == 400
data = response.get_json()
assert 'error' in data

def test_nearby_stations_invalid_params(client):
"""Should return 400 for invalid lat/lng values"""
response = client.get('/api/nearby_stations?lat=abc&lng=xyz')
assert response.status_code == 400
data = response.get_json()
assert 'error' in data
Loading