From dbcfb1efdc435fac12f482ae3de953332eb21d6a Mon Sep 17 00:00:00 2001 From: Eli Lucherini Date: Sun, 22 Dec 2024 20:26:51 -0800 Subject: [PATCH] Soft story --- backend/api/routers/soft_story_api.py | 20 ++++++++++++++++++++ backend/api/tests/test_soft_story.py | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/backend/api/routers/soft_story_api.py b/backend/api/routers/soft_story_api.py index deeafa90..cbced4d3 100644 --- a/backend/api/routers/soft_story_api.py +++ b/backend/api/routers/soft_story_api.py @@ -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, @@ -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() \ No newline at end of file diff --git a/backend/api/tests/test_soft_story.py b/backend/api/tests/test_soft_story.py index 2f9591d6..87cb59c2 100644 --- a/backend/api/tests/test_soft_story.py +++ b/backend/api/tests/test_soft_story.py @@ -7,7 +7,6 @@ # Will the .. be stable? from ..main import app -from ..schemas.geo import Polygon @pytest.fixture @@ -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 \ No newline at end of file