Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.sqlhistory
.dolt
server/zip_codes.db
server/zip_codes.dbserver/__pycache__/
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules we need a newline here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I've fixed the .gitignore file to ensure the new entry is on its own line.

11 changes: 11 additions & 0 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ def get_nearest_zip(
):
return find_nearest(lat, lng)

@app.get("/id/{intId}", response_model=ZipCodeData)
def get_zip_by_id(intId: int):
cursor = state.db_connection.cursor()
cursor.execute("SELECT * FROM zip_codes WHERE id = ?", (intId,))
row = cursor.fetchone()

if not row:
raise HTTPException(status_code=404, detail="Zip code not found")

return dict(row)

@app.get("/{query}", response_model=ZipCodeData)
def get_zip_or_coords(query: str):
# Check if input looks like coordinates "lat,lng"
Expand Down
21 changes: 21 additions & 0 deletions server/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ def test_get_zips_invalid_params():
assert response.status_code == 400
assert "Invalid order" in response.json()["detail"]

def test_get_zip_by_id():
# First get a valid ID from the random endpoint
response = client.get("/random")
assert response.status_code == 200
random_zip_data = response.json()
valid_id = random_zip_data["id"]

# Query by that ID
response = client.get(f"/id/{valid_id}")
assert response.status_code == 200
data = response.json()
assert data["id"] == valid_id
assert data["zip"] == random_zip_data["zip"]

def test_get_zip_by_id_invalid():
# Assuming 0 or negative IDs don't exist, or a very large number
# Based on previous schema check, id is INTEGER PRIMARY KEY, usually starting at 1.
response = client.get("/id/0")
assert response.status_code == 404
assert response.json()["detail"] == "Zip code not found"

def test_get_zips_city_state_filter():
response = client.get("/zips?city_and_state_only=true")
assert response.status_code == 200
Expand Down