Skip to content

Commit

Permalink
fix: use float32 for querying as well, adapt validation and add test …
Browse files Browse the repository at this point in the history
…with float sqm
  • Loading branch information
rettetdemdativ committed Jun 21, 2024
1 parent ce3b3b0 commit b004e95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
24 changes: 15 additions & 9 deletions backend/cosmos/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,22 @@ func GetListingsQueryItemsPager(

for field, mapping := range fieldMappings {
if !reflect.ValueOf(mapping.value).IsNil() {
if field == "minHwgEnergyClass" || field == "minFgeeEnergyClass" {
ecStr, _ := (mapping.value).(*models.EnergyClass)
if *ecStr != models.EnergyClassG {
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, models.GetEnergyClasses()[:ecStr.GetIndex()+1])
switch mapping.value.(type) {
case *float32:
v := mapping.value.(*float32)
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, *v)
default:
if field == "minHwgEnergyClass" || field == "minFgeeEnergyClass" {
ecStr, _ := (mapping.value).(*models.EnergyClass)
if *ecStr != models.EnergyClassG {
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, models.GetEnergyClasses()[:ecStr.GetIndex()+1])
}
} else if field == "postalCodes" {
postalCodeStr := mapping.value.(*string)
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, strings.Split(*postalCodeStr, ","))
} else {
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, mapping.value)
}
} else if field == "postalCodes" {
postalCodeStr := mapping.value.(*string)
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, strings.Split(*postalCodeStr, ","))
} else {
addQueryParam(&sb, &queryParams, "@"+field, mapping.condition, mapping.value)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion backend/models/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"io"
"reflect"
"time"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -39,7 +40,12 @@ func enumFieldValidator[T StringEnum](fl validator.FieldLevel) bool {
func gtFieldIgnoreNilValidator(fl validator.FieldLevel) bool {
otherField := fl.Parent().FieldByName(fl.Param())
if !otherField.IsNil() {
return otherField.Elem().Int() <= fl.Field().Int()
switch fl.Field().Kind() {
case reflect.Int:
return otherField.Elem().Int() <= fl.Field().Int()
case reflect.Float32:
return otherField.Elem().Float() <= fl.Field().Float()
}
}
return true
}
Expand Down
22 changes: 11 additions & 11 deletions backend/models/listings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ type ListingsQuery struct {
HousingCooperative *string `json:"housingCooperative" validate:"omitempty"`
ProjectId *string `json:"projectId" validate:"omitempty"`
PostalCode *string `json:"postalCode" validate:"omitempty"`
RoomCount *int `json:"roomCount,string" validate:"omitempty,gt=0"`
MinRoomCount *int `json:"minRoomCount,string" validate:"omitempty,gt=0"`
MaxRoomCount *int `json:"maxRoomCount,string" validate:"omitempty,gt=0,gtfieldcustom=MinRoomCount"`
MinSquareMeters *int `json:"minSqm,string" validate:"omitempty,gt=0"`
MaxSquareMeters *int `json:"maxSqm,string" validate:"omitempty,gt=0,gtfieldcustom=MinSquareMeters"`
RoomCount *float32 `json:"roomCount,string" validate:"omitempty,gt=0"`
MinRoomCount *float32 `json:"minRoomCount,string" validate:"omitempty,gt=0"`
MaxRoomCount *float32 `json:"maxRoomCount,string" validate:"omitempty,gt=0,gtfieldcustom=MinRoomCount"`
MinSquareMeters *float32 `json:"minSqm,string" validate:"omitempty,gt=0"`
MaxSquareMeters *float32 `json:"maxSqm,string" validate:"omitempty,gt=0,gtfieldcustom=MinSquareMeters"`
AvailableFrom *string `json:"availableFrom" validate:"omitempty,datecustom"`
MinYearBuilt *int `json:"minYearBuilt,string" validate:"omitempty,gt=1900"`
MaxYearBuilt *int `json:"maxYearBuilt,string" validate:"omitempty,gt=1900,gtfieldcustom=MinYearBuilt"`
MinHwgEnergyClass *EnergyClass `json:"minHwgEnergyClass" validate:"omitempty,energycustom"`
MinFgeeEnergyClass *EnergyClass `json:"minFgeeEnergyClass" validate:"omitempty,energycustom"`
ListingType *ListingType `json:"listingType" validate:"omitempty,listingtypecustom"`
MinRentPricePerMonth *int `json:"minRentPrice,string" validate:"omitempty,gt=0"`
MaxRentPricePerMonth *int `json:"maxRentPrice,string" validate:"omitempty,gt=0,gtfieldcustom=MinRentPricePerMonth"`
MinCooperativeShare *int `json:"minCooperativeShare,string" validate:"omitempty,gt=0"`
MaxCooperativeShare *int `json:"maxCooperativeShare,string" validate:"omitempty,gt=0,gtfieldcustom=MinCooperativeShare"`
MinSalePrice *int `json:"minSalePrice,string" validate:"omitempty,gt=0"`
MaxSalePrice *int `json:"maxSalePrice,string" validate:"omitempty,gt=0,gtfieldcustom=MinSalePrice"`
MinRentPricePerMonth *float32 `json:"minRentPrice,string" validate:"omitempty,gt=0"`
MaxRentPricePerMonth *float32 `json:"maxRentPrice,string" validate:"omitempty,gt=0,gtfieldcustom=MinRentPricePerMonth"`
MinCooperativeShare *float32 `json:"minCooperativeShare,string" validate:"omitempty,gt=0"`
MaxCooperativeShare *float32 `json:"maxCooperativeShare,string" validate:"omitempty,gt=0,gtfieldcustom=MinCooperativeShare"`
MinSalePrice *float32 `json:"minSalePrice,string" validate:"omitempty,gt=0"`
MaxSalePrice *float32 `json:"maxSalePrice,string" validate:"omitempty,gt=0,gtfieldcustom=MinSalePrice"`
ContinuationToken *string `json:"continuationToken"`
PageSize *int `json:"pageSize,string" validate:"omitempty,gt=0,lte=30"`
SortBy *string
Expand Down
4 changes: 2 additions & 2 deletions backend/test/test_getListingsByCity.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ def test_get_listings_query_max_flat_size():

@pytest.mark.usefixtures("cosmos_db_setup_listings")
def test_get_listings_query_min_max_flat_size():
endpoint_url = f"{API_BASE_URL}/cities/vienna/listings?minSqm=80&maxSqm=100"
endpoint_url = f"{API_BASE_URL}/cities/vienna/listings?minSqm=80.5&maxSqm=100"
expected_ids = [
l["id"]
for l in LISTINGS_FIXTURE
if l["_partitionKey"] == "vienna" and 80 <= l["squareMeters"] <= 100
if l["_partitionKey"] == "vienna" and 80.5 <= l["squareMeters"] <= 100
]

response = requests.get(endpoint_url)
Expand Down

0 comments on commit b004e95

Please sign in to comment.