From a291ce1ee4eb24a762356cc9e650e71ba0026d81 Mon Sep 17 00:00:00 2001 From: casulit Date: Fri, 18 Oct 2024 13:28:35 +0800 Subject: [PATCH] feat(server): Add endpoint to get property details by ID --- server.ts | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/server.ts b/server.ts index 5a0a23d..aa442d8 100644 --- a/server.ts +++ b/server.ts @@ -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 } });