Skip to content

Commit

Permalink
refactor(server): Nest pagination details in API response json
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 18, 2024
1 parent 59716df commit c7c4c9c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,21 @@ app.get("/api/properties", async (c: Context) => {
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({

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

Expand Down

0 comments on commit c7c4c9c

Please sign in to comment.