From 45783c60a957bd464cc4dc222f6036f59beff32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Wed, 6 Dec 2023 21:31:50 -0500 Subject: [PATCH] refactor(api/utils): update listQuerier helper make it return an object containing a sliced array and a total --- lib/server/dbHelpers/listQuerier.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/server/dbHelpers/listQuerier.ts b/lib/server/dbHelpers/listQuerier.ts index 4049993..20046b6 100644 --- a/lib/server/dbHelpers/listQuerier.ts +++ b/lib/server/dbHelpers/listQuerier.ts @@ -1,9 +1,19 @@ import { Band } from "~/models/band"; import { Challenge } from "~/models/challenges/gen"; -import { Story } from "~/models/stories"; +import { IStory, Story } from "~/models/stories"; import { log } from "../logger"; +import { H3Event, EventHandlerRequest } from "h3"; -export default async function (query, context, limit?: number, sort?) { +export default async function ( + query, + context, + ev: H3Event, + limit: number = 0, + sort?, +): Promise<{ stories: IStory[]; total: number }> { + const q = getQuery(ev); + let skipAmt = limit * (parseInt((q.page as string) || "1") - 1) - 1; + if (skipAmt < 0) skipAmt = 0; query["chapters.hidden"] = false; if (context.currentUser) { if (!query.author) query.author = {}; @@ -14,7 +24,6 @@ export default async function (query, context, limit?: number, sort?) { query["ficmas"] = { $nin: context.ficmasarray_raw.map((a) => a._id), }; - log.debug(query, { label: "list query" }); let stories = await Story.find(query, null) .collation({ locale: "en" }) .sort(sort ? sort : { "chapters.posted": -1 }) @@ -26,7 +35,11 @@ export default async function (query, context, limit?: number, sort?) { .populate("chapters.bands") .populate({ path: "challenge", model: Challenge }) .populate("author", "username _id") - .limit(limit || Infinity) .exec(); - return stories.filter((a) => a.author != null); + let oro = stories.filter((a) => a.author != null); + log.debug(JSON.stringify(query), { label: "list query" }); + return { + total: oro.length, + stories: oro.slice(skipAmt, limit == 0 ? oro.length : skipAmt + limit), + }; }