Skip to content

Commit

Permalink
refactor: update intense queries to counts
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Sep 15, 2024
1 parent 71caab4 commit c6eb338
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 42 deletions.
13 changes: 6 additions & 7 deletions server/routes/geojsonSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ router.get("/", async (req, res) => {
}))
.filter(
({ properties: { propno } }) =>
!!place || +place.split(" ")[0] === propno
!!place && +place.split(" ")[0] === propno
)
let nearbyAddresses = []

Expand All @@ -70,7 +70,7 @@ router.get("/", async (req, res) => {
geoJSON = buildGeoJSONTemplate(targetAddress)
praxisDataType = "parcels-by-geocode:single-parcel"
} else if (targetAddress.length === 0 && nearbyAddresses.length > 0) {
geoJSON = { ...nearbyAddresses[0], code: zipcode }
geoJSON = { count: +nearbyAddresses[0].count, code: zipcode }
praxisDataType = "parcels-by-geocode:multiple-parcels"
} else {
geoJSON = buildGeoJSONTemplate([])
Expand All @@ -86,21 +86,20 @@ router.get("/", async (req, res) => {
PGDBQueryType: queries.GEOJSON_PARCELS_CODE,
})

geoJSON = pgData.data[0].jsonb_build_object
clientData = checkEmptyGeoJSON(geoJSON)
geoJSON = { count: +pgData.data[0].count, code }
clientData = geoJSON
praxisDataType = "parcels-by-code"
break

case "parcels-by-speculator":
pgData = await queries.queryPGDB({
ownid,
year,
// TODO: Check
PGDBQueryType: queries.GEOJSON_PARCELS_OWNID,
})

geoJSON = pgData.data[0].jsonb_build_object
clientData = checkEmptyGeoJSON(geoJSON)
geoJSON = { count: +pgData.data[0].count, ownid }
clientData = geoJSON
praxisDataType = "parcels-by-speculator"
break

Expand Down
37 changes: 2 additions & 35 deletions server/utils/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,11 @@ async function queryPGDB({
break

case GEOJSON_PARCELS_CODE:
query = SQL`SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(centroid, 6)::json,
'properties', to_jsonb(inputs) - 'centroid',
'centroid', ST_AsText(centroid)
) AS feature
FROM (
SELECT * FROM parcels
WHERE year = ${year}
AND propzip LIKE ${zipMatch}
) inputs
) features;`
query = SQL`SELECT COUNT(*) FROM parcels WHERE year = ${year} AND propzip LIKE ${zipMatch};`
break

// TODO: geom getting replaced
case GEOJSON_PARCELS_OWNID:
query = SQL`SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(centroid, 6)::json,
'properties', to_jsonb(inputs),
'centroid', ST_AsText(centroid)
) AS feature
FROM (
SELECT * FROM parcels
WHERE year = ${year}
AND own_id LIKE ${ownIdMatch}
) inputs
) features;`
query = SQL`SELECT COUNT(*) FROM parcels WHERE year = ${year} AND own_id LIKE ${ownIdMatch};`
break

case GEOJSON_PARCELS_CODE_OWNID:
Expand Down

0 comments on commit c6eb338

Please sign in to comment.