diff --git a/server.js b/server.js index e1ad1c4..c569ddb 100644 --- a/server.js +++ b/server.js @@ -62,11 +62,14 @@ app.get("/api", function (request, reply) { }); // This API returns a list of the assets in the database (SW plugins and themes). +// It also returns the number of pages in the database. // It can take a `?page=x` argument to display a different page, with a limit of 20 assets per page. app.get("/api/catalog-assets", async (request, reply) => { try { const page = parseInt(request.query.page, 10) || 1; // default to page 1 + const totalItems = await catalog_assets.count(); + if (page < 1) { reply.status(400).send({ error: "Page must be a positive number!" }); return; @@ -95,18 +98,7 @@ app.get("/api/catalog-assets", async (request, reply) => { return acc; }, {}); - reply.send({ assets }); - } catch (error) { - reply.status(500).send({ error: "There was an error" }); - } -}); - -// This API returns the total number of pages in the database. -app.get("/api/catalog-pages", async (request, reply) => { - try { - const totalItems = await catalog_assets.count(); - - reply.send({ pages: Math.ceil(totalItems / 20) }); + reply.send({ assets, pages: Math.ceil(totalItems / 20) }); } catch (error) { reply.status(500).send({ error: "There was an error" }); } diff --git a/src/pages/catalog/[...page].astro b/src/pages/catalog/[...page].astro index a5e291e..bcbf220 100644 --- a/src/pages/catalog/[...page].astro +++ b/src/pages/catalog/[...page].astro @@ -4,7 +4,7 @@ import CatalogCard from "../../components/catalog/CatalogCard.svelte"; const { page } = Astro.params; -const response = await fetch(new URL("/api/catalog-pages/", Astro.url)); +const response = await fetch(new URL("/api/catalog-assets/", Astro.url)); const assets_json = await response.json(); const next_page = parseInt(page!) + 1; @@ -68,6 +68,7 @@ const last_page = assets_json.pages; type="number" id="pagnation_input" placeholder="..." + transition:persist /> {/* The last page. If the user is on this page, don't show it. */} { @@ -83,14 +84,12 @@ const last_page = assets_json.pages; -