Skip to content

Commit

Permalink
feat(server): Add endpoint to get property details by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 18, 2024
1 parent 029018c commit a291ce1
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,87 @@ app.get("/api/properties", async (c: Context) => {
});
});

app.get("/api/properties/:id", async (c: Context) => {
using client = await dbPool.connect();
const id = c.req.param("id");

if (!id) {
return c.json({ error: "Property ID is required" }, 400);
}

const property = await client.queryObject({
args: [id],
text: `
SELECT
l.id AS listing_id,
l.title,
l.url,
l.project_name,
l.description,
l.is_scraped,
l.price,
l.price_formatted,
p.id AS property_id,
p.user_id,
p.floor_size,
p.lot_size,
p.building_size,
p.ceiling_height,
p.no_of_bedrooms,
p.no_of_bathrooms,
p.no_of_parking_spaces,
p.longitude,
p.latitude,
p.year_built,
p.primary_image_url,
p.images,
p.amenities,
p.property_features,
p.indoor_features,
p.outdoor_features,
p.ai_generated_description,
p.ai_generated_basic_features,
pt.type_name AS property_type_name,
lt.type_name AS listing_type_name,
wt.type_name AS warehouse_type_name,
l.address AS listing_address,
rg.region AS listing_region_name,
ct.city AS listing_city_name,
ar.area AS listing_area_name,
p.created_at AS property_created_at,
p.updated_at AS property_updated_at,
l.created_at AS listing_created_at,
l.updated_at AS listing_updated_at,
-- Price change log as an array ordered by latest changes
(
SELECT json_agg(
json_build_object(
'id', pcl.id,
'old_price', pcl.old_price,
'new_price', pcl.new_price,
'change_timestamp', pcl.change_timestamp
) ORDER BY pcl.change_timestamp DESC
)
FROM Price_Change_Log pcl
WHERE pcl.listing_id = l.id
) AS price_change_log
FROM
Listing l
JOIN Property p ON l.property_id = p.id
LEFT JOIN Property_Type pt ON p.property_type_id = pt.property_type_id
LEFT JOIN Listing_Type lt ON l.offer_type_id = lt.listing_type_id
LEFT JOIN Warehouse_Type wt ON p.warehouse_type_id = wt.warehouse_type_id
LEFT JOIN Listing_Region rg ON p.listing_region_id = rg.id
LEFT JOIN Listing_City ct ON p.listing_city_id = ct.id
LEFT JOIN Listing_Area ar ON p.listing_area_id = ar.id
WHERE
l.id = $1
`,
});

return c.json({ data: property });
});

app.post("/", async (c: Context) => {
const data = await c.req.json();
await sendMessage({ kv, data, options: { delay: 5000 } });
Expand Down

0 comments on commit a291ce1

Please sign in to comment.