From 8b9f456c4a6ccabb26ff77179ddb942fe7b51c37 Mon Sep 17 00:00:00 2001 From: tylerslaton Date: Fri, 22 Mar 2024 12:03:57 -0400 Subject: [PATCH] feat: add pagination to search API and UI Signed-off-by: tylerslaton --- src/lib/db.ts | 22 +++++++++++----------- src/pages/search.vue | 33 ++++++++++++++++++++++++--------- src/server/api/search.get.ts | 6 ++++-- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/lib/db.ts b/src/lib/db.ts index 511e8aa..f792ffc 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -87,22 +87,22 @@ export async function removeToolForUrlIfExists(url: string): Promise { return toolEntry.content as Tool[] } -export async function getToolsForQuery(query: string): Promise> { - const toolEntries = await prisma.toolEntry.findMany({ where: { reference: { contains: query } } }) +export async function getToolsForQuery(query: string, page: number, pageSize: number): Promise<{ tools: Record, totalCount: number }> { + const skip = (page - 1) * pageSize + const toolEntries = await prisma.toolEntry.findMany({ + where: { reference: { contains: query } }, + take: pageSize, + skip: skip > 0 ? skip : undefined, + }) const tools: Record = {} for (const entry of toolEntries) { const parsedTool = entry.content as Tool[] - - for (const tool of parsedTool) { - if (tools[entry.reference]) { - tools[entry.reference].push(tool) - } else { - tools[entry.reference] = [tool] - } - } + tools[entry.reference] = tools[entry.reference] || [] + tools[entry.reference].push(...parsedTool) } - return tools + const totalCount = await prisma.toolEntry.count({ where: { reference: { contains: query } } }) + return { tools, totalCount } } diff --git a/src/pages/search.vue b/src/pages/search.vue index 26fae04..ac0546d 100644 --- a/src/pages/search.vue +++ b/src/pages/search.vue @@ -1,48 +1,58 @@