Skip to content

Commit

Permalink
refactor(server.ts): Optimize SQL param handling, add pagination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 18, 2024
1 parent f54353c commit 59716df
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
20 changes: 11 additions & 9 deletions config/deno-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function cleanSpecialCharacters(input: string): string {
// Remove emojis and other special characters
const cleanedString = encodedString.replace(
/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F1E0}-\u{1F1FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/gu,
""
"",
);

// Remove extra whitespace
Expand Down Expand Up @@ -133,7 +133,7 @@ export async function listenQueue(kv: Deno.Kv) {
Object.keys(msg.data.dataLayer.attributes).length < 5
) {
throw new Error(
"Attributes are missing, undefined, or have fewer than 5 properties"
"Attributes are missing, undefined, or have fewer than 5 properties",
);
}

Expand All @@ -142,7 +142,7 @@ export async function listenQueue(kv: Deno.Kv) {
typeof msg.data.dataLayer.location !== "object"
) {
throw new Error(
"Location is missing, undefined, or not an object"
"Location is missing, undefined, or not an object",
);
}

Expand All @@ -152,7 +152,7 @@ export async function listenQueue(kv: Deno.Kv) {
const images = msg.data.images as { src: string }[];
const isCondominium =
msg.data.dataLayer.attributes.attribute_set_name ===
"Condominium";
"Condominium";
const isHouse =
msg.data.dataLayer.attributes.attribute_set_name === "House";
const isWarehouse =
Expand All @@ -173,8 +173,8 @@ export async function listenQueue(kv: Deno.Kv) {
};

const price = msg.data.dataLayer?.attributes?.price;
const priceFormatted =
msg.data.dataLayer?.attributes?.price_formatted;
const priceFormatted = msg.data.dataLayer?.attributes
?.price_formatted;

await transaction.queryArray({
args: [price, priceFormatted, listing.id],
Expand Down Expand Up @@ -252,8 +252,9 @@ export async function listenQueue(kv: Deno.Kv) {
const productOwnerName = msg.data.dataLayer?.product_owner_name;
const location: Location = msg.data.dataLayer.location;
const dataLayerAttributes = msg.data.dataLayer.attributes;
const offerTypeId =
dataLayerAttributes.offer_type === "Rent" ? 2 : 1;
const offerTypeId = dataLayerAttributes.offer_type === "Rent"
? 2
: 1;
const sellerIsTrusted = dataLayerAttributes?.seller_is_trusted;
const locationData = await getLocation(transaction, {
...location,
Expand Down Expand Up @@ -364,7 +365,8 @@ export async function listenQueue(kv: Deno.Kv) {
offerTypeId,
newProperty,
],
text: `INSERT INTO Listing (title, url, project_name, description, is_scraped, address, price_formatted, price, offer_type_id, property_id)
text:
`INSERT INTO Listing (title, url, project_name, description, is_scraped, address, price_formatted, price, offer_type_id, property_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id`,
});

Expand Down
46 changes: 40 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ app.get("/api/properties", async (c: Context) => {
const sqlParams = [
parseInt(query.property_type_id),
parseInt(query.listing_type_id),
parseInt(query.page_size),
offset,
];

// Initialize the parameter counter for dynamic parameter numbering
let paramCounter = 5;
let paramCounter = 3;

// Function to add a new condition to the WHERE clause
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -103,7 +101,7 @@ app.get("/api/properties", async (c: Context) => {
console.log({ sqlWhereClause, sqlParams, nextParamCounter: paramCounter });

const postgres = await client.queryObject({
args: sqlParams,
args: [...sqlParams, parseInt(query.page_size), offset],
text: `
SELECT
l.id AS listing_id,
Expand Down Expand Up @@ -169,10 +167,46 @@ app.get("/api/properties", async (c: Context) => {
LEFT JOIN Listing_Area ar ON p.listing_area_id = ar.id
WHERE
${sqlWhereClause}
ORDER BY l.id DESC LIMIT $3 OFFSET $4;
ORDER BY l.id DESC LIMIT $${paramCounter} OFFSET $${paramCounter + 1};
`,
});

const recordCount = await client.queryObject({
args: sqlParams,
text: `
SELECT COUNT(*)::integer
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
${sqlWhereClause}
`,
});
return c.json(postgres.rows);

const counterResult = recordCount.rows[0] as { count: number };
const totalListingRecords = counterResult.count;
const pageNo = parseInt(query.page);
const pageSize = parseInt(query.page_size);

const totalPages = Math.ceil(totalListingRecords / pageSize);
const nextPage = pageNo < totalPages ? pageNo + 1 : null;
const previousPage = pageNo > 1 ? pageNo - 1 : null;

return c.json({
data: postgres.rows,
total: totalListingRecords,
page: pageNo,
page_size: pageSize,
total_pages: totalPages,
next_page: nextPage,
previous_page: previousPage
});
});

app.post("/", async (c: Context) => {
Expand Down

0 comments on commit 59716df

Please sign in to comment.