ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

missing some zip centroids fall back (#323)

Co-authored-by: paul miller <paul.miller>

authored by

Paul Miller
paul miller
and committed by
GitHub
560b2797 6afef7a1

+43 -16
+16 -13
internal/locations/locations.go
··· 133 133 } 134 134 135 135 func (l *locationStorage) GetLocationsByZip(ctx context.Context, zipcode string) ([]Location, error) { 136 - requestedCentroid, hasRequestedCentroid := l.zipCentroids.ZipCentroidByZIP(zipcode) 137 - if !hasRequestedCentroid { 138 - slog.ErrorContext(ctx, "requested zip has no centroid; skipping distance filter and sort", "zip", zipcode) 139 - return nil, fmt.Errorf("invalid zip code %s. Can't find lat long", zipcode) 140 - } 141 136 142 137 results := make(chan []Location, len(l.client)) 143 138 errors := make(chan error, len(l.client)) ··· 166 161 allLocations = append(allLocations, result...) 167 162 } 168 163 164 + for _, loc := range allLocations { 165 + go func(loc Location) { 166 + if err := l.storeLocationIfMissing(loc); err != nil { 167 + slog.WarnContext(ctx, "failed to store location in cache", "location_id", loc.ID, "error", err) 168 + } 169 + }(loc) 170 + } 171 + 172 + requestedCentroid, hasRequestedCentroid := l.zipCentroids.ZipCentroidByZIP(zipcode) 173 + if !hasRequestedCentroid { 174 + //were missign zip codes. Make this an error later? 175 + slog.WarnContext(ctx, "requested zip has no centroid; returning unsorted locations without distance filter", "zip", zipcode) 176 + return allLocations, nil 177 + } 178 + 169 179 filtered := make([]Location, 0, len(allLocations)) 170 180 for _, loc := range allLocations { 171 181 if _, hasZipCentroid := l.zipCentroids.ZipCentroidByZIP(loc.ZipCode); !hasZipCentroid { ··· 183 193 allLocations = filtered 184 194 sortLocationsByDistanceFromCentroid(allLocations, requestedCentroid, l.zipCentroids) 185 195 186 - for _, loc := range allLocations { 187 - go func(loc Location) { 188 - if err := l.storeLocationIfMissing(loc); err != nil { 189 - slog.WarnContext(ctx, "failed to store location in cache", "location_id", loc.ID, "error", err) 190 - } 191 - }(loc) 192 - } 193 196 return allLocations, nil 194 197 } 195 198 ··· 336 339 } 337 340 if err := l.renderLocationsPage(w, ctx, zip, favoriteStore, currentUser != nil); err != nil { 338 341 slog.ErrorContext(ctx, "failed to render locations page", "zip", zip, "error", err) 339 - http.Error(w, "template error", http.StatusInternalServerError) 342 + http.Error(w, "Failed to render locations page. ", http.StatusInternalServerError) 340 343 } 341 344 }) 342 345 }
+27 -3
internal/locations/locations_test.go
··· 156 156 }) 157 157 158 158 server := newTestLocationServer(client) 159 - _, err := server.GetLocationsByZip(context.Background(), "not-a-zip") 160 - if err == nil { 161 - t.Fatalf("GetLocationsByZip should have errored") 159 + locs, err := server.GetLocationsByZip(context.Background(), "not-a-zip") 160 + if err != nil { 161 + t.Fatalf("GetLocationsByZip returned error: %v", err) 162 + } 163 + if got, want := []string{locs[0].ID, locs[1].ID}, []string{"first", "second"}; got[0] != want[0] || got[1] != want[1] { 164 + t.Fatalf("unexpected order: got %v want %v", got, want) 165 + } 166 + } 167 + 168 + func TestGetLocationsByZipReturnsUnfilteredResultsWhenRequestedZipCentroidMissing(t *testing.T) { 169 + client := newFakeLocationClient() 170 + client.setListResponse("94012", []Location{ 171 + {ID: "first", Name: "First", ZipCode: "00602"}, 172 + {ID: "second", Name: "Second", ZipCode: "98004"}, 173 + {ID: "third", Name: "Third", ZipCode: "00601"}, 174 + }) 175 + 176 + server := newTestLocationServer(client) 177 + locs, err := server.GetLocationsByZip(context.Background(), "94012") 178 + if err != nil { 179 + t.Fatalf("GetLocationsByZip returned error: %v", err) 180 + } 181 + if len(locs) != 3 { 182 + t.Fatalf("expected all locations to be returned, got %d", len(locs)) 183 + } 184 + if got, want := []string{locs[0].ID, locs[1].ID, locs[2].ID}, []string{"first", "second", "third"}; got[0] != want[0] || got[1] != want[1] || got[2] != want[2] { 185 + t.Fatalf("unexpected order: got %v want %v", got, want) 162 186 } 163 187 } 164 188