Skip to content

Commit

Permalink
Refactored search parameter validation into function for cleanliness
Browse files Browse the repository at this point in the history
  • Loading branch information
GDeane committed Jun 19, 2024
1 parent 7e0eb44 commit 2fe2b70
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions apps/algorithm/search/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down

0 comments on commit 2fe2b70

Please sign in to comment.