From 088232f75070bdfc79a47b593406876802703c42 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: Mon, 11 Dec 2023 21:29:46 -0500 Subject: [PATCH] refactor(api & server/utils): refactor imports of db helpers create an index file which exports the functions we need in one accessible place --- lib/server/dbHelpers/index.ts | 3 +++ server/api/band/[id]/stories.get.ts | 2 +- server/api/latest.get.ts | 2 +- server/api/review/[revid]/index.get.ts | 3 ++- server/api/story/[id]/[chapter]/index.get.ts | 4 ++-- server/api/story/[id]/[chapter]/index.put.ts | 2 +- server/api/story/[id]/[chapter]/reviews/index.get.ts | 7 +++---- server/api/story/[id]/[chapter]/reviews/index.post.ts | 2 +- server/api/story/[id]/full.get.ts | 5 ++--- server/api/story/[id]/index.delete.ts | 2 +- server/api/story/[id]/index.get.ts | 4 ++-- server/api/story/[id]/index.put.ts | 2 +- server/api/user/[id]/stories.get.ts | 7 +++---- 13 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 lib/server/dbHelpers/index.ts diff --git a/lib/server/dbHelpers/index.ts b/lib/server/dbHelpers/index.ts new file mode 100644 index 0000000..3fcad22 --- /dev/null +++ b/lib/server/dbHelpers/index.ts @@ -0,0 +1,3 @@ +export { default as chapterTransformer } from "./chapterTransformer"; +export { default as listQuerier } from "./listQuerier"; +export { default as storyQuerier } from "./storyQuerier"; diff --git a/server/api/band/[id]/stories.get.ts b/server/api/band/[id]/stories.get.ts index 251817a..2e6f86a 100644 --- a/server/api/band/[id]/stories.get.ts +++ b/server/api/band/[id]/stories.get.ts @@ -1,4 +1,4 @@ -import listQuerier from "~/lib/server/dbHelpers/listQuerier"; +import { listQuerier } from "~/lib/server/dbHelpers"; import { Band } from "~/models/band"; import { Story } from "~/models/stories"; diff --git a/server/api/latest.get.ts b/server/api/latest.get.ts index 5dc848e..4e5bef7 100644 --- a/server/api/latest.get.ts +++ b/server/api/latest.get.ts @@ -1,4 +1,4 @@ -import listQuerier from "~/lib/server/dbHelpers/listQuerier"; +import { listQuerier } from "~/lib/server/dbHelpers"; import { Story } from "~/models/stories"; import { log } from "~/lib/server/logger"; diff --git a/server/api/review/[revid]/index.get.ts b/server/api/review/[revid]/index.get.ts index e178ee1..3c1db74 100644 --- a/server/api/review/[revid]/index.get.ts +++ b/server/api/review/[revid]/index.get.ts @@ -1,8 +1,9 @@ import { messages } from "~/lib/server/constants"; import { Review } from "~/models/stories/review"; +import isIdNan from "~/lib/server/middlewareButNotReally/isIdNan"; export default eventHandler(async (ev) => { - const revid = parseInt(getRouterParam(ev, "revid")!); + const revid = isIdNan(ev); const r = await Review.findById(revid) .populate("author", "username _id") .exec(); diff --git a/server/api/story/[id]/[chapter]/index.get.ts b/server/api/story/[id]/[chapter]/index.get.ts index 5528c97..0c8d522 100644 --- a/server/api/story/[id]/[chapter]/index.get.ts +++ b/server/api/story/[id]/[chapter]/index.get.ts @@ -1,5 +1,5 @@ -import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { chapterTransformer } from "~/lib/server/dbHelpers"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; export default eventHandler(async (ev) => { diff --git a/server/api/story/[id]/[chapter]/index.put.ts b/server/api/story/[id]/[chapter]/index.put.ts index c6e1b7d..8ecea81 100644 --- a/server/api/story/[id]/[chapter]/index.put.ts +++ b/server/api/story/[id]/[chapter]/index.put.ts @@ -1,7 +1,7 @@ import { FormChapter } from "~/lib/client/types/form/story"; import { countWords } from "~/lib/functions"; import { messages } from "~/lib/server/constants"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; import { replaceOrUploadContent, bodyHandler } from "~/lib/server/storyHelpers"; diff --git a/server/api/story/[id]/[chapter]/reviews/index.get.ts b/server/api/story/[id]/[chapter]/reviews/index.get.ts index 87fb071..2987b82 100644 --- a/server/api/story/[id]/[chapter]/reviews/index.get.ts +++ b/server/api/story/[id]/[chapter]/reviews/index.get.ts @@ -1,11 +1,10 @@ -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import { Review } from "~/models/stories/review"; export default eventHandler(async (ev) => { let story = await storyQuerier(ev); - let comments = await Review.find({ + return await Review.find({ whichChapter: story.chapters[ev.context.chapterIndex || 0].id, replyingTo: null, - }); - return comments; + }).exec(); }); diff --git a/server/api/story/[id]/[chapter]/reviews/index.post.ts b/server/api/story/[id]/[chapter]/reviews/index.post.ts index 03d6a1f..1fcfc64 100644 --- a/server/api/story/[id]/[chapter]/reviews/index.post.ts +++ b/server/api/story/[id]/[chapter]/reviews/index.post.ts @@ -1,5 +1,5 @@ import san from "sanitize-html"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; import { Story } from "~/models/stories"; import { Review } from "~/models/stories/review"; diff --git a/server/api/story/[id]/full.get.ts b/server/api/story/[id]/full.get.ts index 35a1219..77fb26d 100644 --- a/server/api/story/[id]/full.get.ts +++ b/server/api/story/[id]/full.get.ts @@ -1,6 +1,5 @@ -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; -import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer"; -import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; +import { storyQuerier } from "~/lib/server/dbHelpers"; +import { chapterTransformer } from "~/lib/server/dbHelpers"; import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; import { messages } from "~/lib/server/constants"; diff --git a/server/api/story/[id]/index.delete.ts b/server/api/story/[id]/index.delete.ts index 3002ef4..2107737 100644 --- a/server/api/story/[id]/index.delete.ts +++ b/server/api/story/[id]/index.delete.ts @@ -1,5 +1,5 @@ import { messages } from "~/lib/server/constants"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import { canDelete } from "~/lib/server/middlewareButNotReally/storyPrivileges"; import { Story } from "~/models/stories"; diff --git a/server/api/story/[id]/index.get.ts b/server/api/story/[id]/index.get.ts index 9246678..a98743c 100644 --- a/server/api/story/[id]/index.get.ts +++ b/server/api/story/[id]/index.get.ts @@ -1,7 +1,7 @@ -import queryStory from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; export default eventHandler(async (ev) => { - const story = await queryStory(ev); + const story = await storyQuerier(ev); let chrs = await storyCheck(ev, story, 0); if (chrs != null) { throw createError(chrs); diff --git a/server/api/story/[id]/index.put.ts b/server/api/story/[id]/index.put.ts index 7a29bdc..e5a2d18 100644 --- a/server/api/story/[id]/index.put.ts +++ b/server/api/story/[id]/index.put.ts @@ -2,7 +2,7 @@ import { Readable } from "stream"; import { Document } from "mongoose"; import { IStory, Story } from "~/models/stories"; import { FormStory } from "~/lib/client/types/form/story"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import { storyQuerier } from "~/lib/server/dbHelpers"; import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; import { diff --git a/server/api/user/[id]/stories.get.ts b/server/api/user/[id]/stories.get.ts index bfe578d..b73c0d4 100644 --- a/server/api/user/[id]/stories.get.ts +++ b/server/api/user/[id]/stories.get.ts @@ -1,11 +1,10 @@ -import listQuerier from "~/lib/server/dbHelpers/listQuerier"; +import { listQuerier } from "~/lib/server/dbHelpers"; export default cachedEventHandler(async (ev) => { const id = parseInt(getRouterParam(ev, "id")!); let s = await listQuerier({ author: { $in: [id] } }, ev.context, ev); - const t = s.stories.map((a) => a.toObject()); return { - stories: t, - total: t.length, + stories: s.stories, + total: s.stories.length, }; });