From b000c6734a100fa4685e77beebebcd66292a5dc9 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: Tue, 3 Oct 2023 01:24:06 -0400 Subject: [PATCH] refactor(api): rename imports and refactor error messages --- server/api/story/[id]/index.delete.ts | 3 +- server/api/story/[id]/index.put.ts | 161 +++++++++++++------------- server/api/story/new.post.ts | 2 +- 3 files changed, 84 insertions(+), 82 deletions(-) diff --git a/server/api/story/[id]/index.delete.ts b/server/api/story/[id]/index.delete.ts index 50affc0..3002ef4 100644 --- a/server/api/story/[id]/index.delete.ts +++ b/server/api/story/[id]/index.delete.ts @@ -1,3 +1,4 @@ +import { messages } from "~/lib/server/constants"; import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; import { canDelete } from "~/lib/server/middlewareButNotReally/storyPrivileges"; import { Story } from "~/models/stories"; @@ -13,6 +14,6 @@ export default eventHandler(async (ev) => { } throw createError({ statusCode: 403, - message: "Forbidden", + message: messages[403], }); }); diff --git a/server/api/story/[id]/index.put.ts b/server/api/story/[id]/index.put.ts index 49e4c39..f5ae975 100644 --- a/server/api/story/[id]/index.put.ts +++ b/server/api/story/[id]/index.put.ts @@ -1,80 +1,81 @@ -import { Readable } from "stream"; -import { Document } from "mongoose"; -import { IStory, Story } from "~/models/stories"; -import { FormStory } from "~/lib/client/types/FormStory"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; -import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; -import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; -import { - bodyHandler, - getBucket, - modelFormChapter, - replaceOrUploadContent, -} from "~/lib/server/storyHelpers"; -import { countWords } from "~/lib/functions"; - -export default eventHandler(async (ev) => { - let os: - | (Document & - IStory & - Required<{ - _id: number; - }>) - | null = await storyQuerier(ev); - isLoggedIn(ev); - if (!canModify(ev, os)) { - throw createError({ - statusCode: 403, - message: "Forbidden", - }); - } - const body = await readBody(ev); - const update: Partial = { - title: body.title, - completed: body.completed, - chapters: [], - }; - for (const oc of os.chapters) { - let filename = `/stories/${oc.id}.txt`; - const bucket = getBucket(); - const curs = bucket.find({ filename }).limit(1); - for await (const d of curs) { - await bucket.delete(d._id); - } - } - for (const c of body.chapters) { - let idx = os.chapters.findIndex((k) => k.id === c.id); - const cont = await bodyHandler(c); - if (idx === -1) { - update.chapters!.push({ - ...modelFormChapter(c), - posted: new Date(Date.now()), - }); - } else { - update.chapters!.push({ - ...modelFormChapter(c), - id: os.chapters[idx].id, - posted: os.chapters[idx].posted, - }); - replaceOrUploadContent(os.chapters![idx].id, cont); - } - update.chapters![update.chapters!.length - 1].words = countWords(cont); - } - os = await Story.findOneAndUpdate( - { - _id: os._id, - }, - update, - { new: true }, - ); - if (!os) { - throw createError({ - statusCode: 500, - message: "Something went wrong.", - }); - } - return { - success: true, - data: os.toObject(), - }; -}); +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 isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; +import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; +import { + bodyHandler, + getBucket, + modelFormChapter, + replaceOrUploadContent, +} from "~/lib/server/storyHelpers"; +import { countWords } from "~/lib/functions"; +import { messages } from "~/lib/server/constants"; + +export default eventHandler(async (ev) => { + let os: + | (Document & + IStory & + Required<{ + _id: number; + }>) + | null = await storyQuerier(ev); + isLoggedIn(ev); + if (!canModify(ev, os)) { + throw createError({ + statusCode: 403, + message: messages[403], + }); + } + const body: FormStory = await readBody(ev); + const update: Partial = { + title: body.title, + completed: body.completed, + chapters: [], + }; + for (const oc of os.chapters) { + let filename = `/stories/${oc.id}.txt`; + const bucket = getBucket(); + const curs = bucket.find({ filename }).limit(1); + for await (const d of curs) { + await bucket.delete(d._id); + } + } + for (const c of body.chapters) { + let idx = os.chapters.findIndex((k) => k.id === c.id); + const cont = await bodyHandler(c); + if (idx === -1) { + update.chapters!.push({ + ...modelFormChapter(c), + posted: new Date(Date.now()), + }); + } else { + update.chapters!.push({ + ...modelFormChapter(c), + id: os.chapters[idx].id, + posted: os.chapters[idx].posted, + }); + replaceOrUploadContent(os.chapters![idx].id, cont); + } + update.chapters![update.chapters!.length - 1].words = countWords(cont); + } + os = await Story.findOneAndUpdate( + { + _id: os._id, + }, + update, + { new: true }, + ); + if (!os) { + throw createError({ + statusCode: 500, + message: "Something went wrong.", + }); + } + return { + success: true, + data: os.toObject(), + }; +}); diff --git a/server/api/story/new.post.ts b/server/api/story/new.post.ts index ac83b31..1ad789f 100644 --- a/server/api/story/new.post.ts +++ b/server/api/story/new.post.ts @@ -1,6 +1,6 @@ import { Readable } from "stream"; import san from "sanitize-html"; -import { FormStory } from "~/lib/client/types/FormStory"; +import { FormStory } from "~/lib/client/types/form/story"; import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; import { getBucket,