Skip to content

Commit

Permalink
Soft story
Browse files Browse the repository at this point in the history
  • Loading branch information
elucherini committed Dec 23, 2024
1 parent 7bebd53 commit dbcfb1e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions backend/api/routers/soft_story_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..tags import Tags
from sqlalchemy.orm import Session
from backend.database.session import get_db
from geoalchemy2 import functions as geo_func
from backend.api.schemas.soft_story_schemas import (
SoftStoryFeature,
SoftStoryFeatureCollection,
Expand Down Expand Up @@ -39,3 +40,22 @@ async def get_soft_stories(db: Session = Depends(get_db)):

features = [SoftStoryFeature.from_sqlalchemy_model(story) for story in soft_stories]
return SoftStoryFeatureCollection(type="FeatureCollection", features=features)


@router.get("/is-soft-story", response_model=bool)
async def is_soft_story(lat: float, lon: float, db: Session = Depends(get_db)):
"""
Check if a point is a soft story property.
Args:
lat (float): Latitude of the point.
lon (float): Longitude of the point.
db (Session): The database session dependency.
Returns:
bool: True if the point is a soft story property, False otherwise.
"""
query = db.query(SoftStoryProperty).filter(
SoftStoryProperty.point == geo_func.ST_GeomFromText(f"POINT({lon} {lat})",4326)
)
return db.query(query.exists()).scalar()
13 changes: 12 additions & 1 deletion backend/api/tests/test_soft_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

# Will the .. be stable?
from ..main import app
from ..schemas.geo import Polygon


@pytest.fixture
Expand Down Expand Up @@ -41,3 +40,15 @@ def test_get_soft_story(client):
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False

def test_is_soft_story(client):
lat, lon = [-122.446575165, 37.766034349]
response = client.get(f"/api/soft-story/is-soft-story?lat={lat}&lon={lon}")
assert response.status_code == 200
assert response.json() # True

# These should not be soft stories
wrong_lat, wrong_lon = [0.0, 0.0]
response = client.get(f"/api/soft-story/is-soft-story?lat={wrong_lat}&lon={wrong_lon}")
assert response.status_code == 200
assert not response.json() # False

0 comments on commit dbcfb1e

Please sign in to comment.