diff --git a/apps/algorithm/search/src/server.py b/apps/algorithm/search/src/server.py index 5c83e9bc..c733f981 100644 --- a/apps/algorithm/search/src/server.py +++ b/apps/algorithm/search/src/server.py @@ -89,6 +89,36 @@ class Listing(BaseModel): ) +def validate_search_params( + latitude: float, + longitude: float, + page: int, + limit: int, + minPrice: float, + maxPrice: float, +): + if abs(latitude) > 90: + raise HTTPException( + status_code=422, detail="latitude must be between -90 and 90" + ) + if abs(longitude) > 180: + raise HTTPException( + status_code=422, detail="longitude must be between -180 and 180" + ) + if page <= 0: + raise HTTPException(status_code=422, detail="page cannot be zero or negative") + if limit <= 0: + raise HTTPException(status_code=422, detail="limit cannot be zero or negative") + if minPrice is not None and minPrice < 0: + raise HTTPException(status_code=422, detail="minPrice cannot be negative") + if maxPrice is not None and maxPrice < 0: + raise HTTPException(status_code=422, detail="maxPrice cannot be negative") + if minPrice is not None and maxPrice is not None and minPrice > maxPrice: + raise HTTPException( + status_code=422, detail="minPrice cannot be greater than maxPrice" + ) + + @app.get("/api/search", response_model=List[ListingSummary]) async def search( authorization: str, @@ -104,30 +134,7 @@ async def search( sort: Sort = "RELEVANCE", ): try: - if abs(latitude) > 90: - raise HTTPException( - status_code=422, detail="latitude must be between -90 and 90" - ) - if abs(longitude) > 180: - raise HTTPException( - status_code=422, detail="longitude must be between -180 and 180" - ) - if page <= 0: - raise HTTPException( - status_code=422, detail="page cannot be zero or negative" - ) - if limit <= 0: - raise HTTPException( - status_code=422, detail="limit cannot be zero or negative" - ) - if minPrice is not None and minPrice < 0: - raise HTTPException(status_code=422, detail="minPrice cannot be negative") - if maxPrice is not None and maxPrice < 0: - raise HTTPException(status_code=422, detail="maxPrice cannot be negative") - if minPrice is not None and maxPrice is not None and minPrice > maxPrice: - raise HTTPException( - status_code=422, detail="minPrice cannot be greater than maxPrice" - ) + validate_search_params(latitude, longitude, page, limit, minPrice, maxPrice) INDEX = os.getenv("ES_INDEX", DEFAULT_INDEX)