diff --git a/server/api/story/[id]/[chapter]/index.get.ts b/server/api/story/[id]/[chapter]/index.get.ts index 4b2dcca..cd529c8 100644 --- a/server/api/story/[id]/[chapter]/index.get.ts +++ b/server/api/story/[id]/[chapter]/index.get.ts @@ -1,12 +1,12 @@ -import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; -import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; - -export default eventHandler(async (ev) => { - const story = await storyQuerier(ev); - const chres = await storyCheck(ev, story); - if (chres != null) { - throw createError(chres); - } - return await chapterTransformer(story, ev); -}); +import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer"; +import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; + +export default eventHandler(async (ev) => { + const story = await storyQuerier(ev); + const chres = await storyCheck(ev, story); + if (chres != null) { + throw createError(chres); + } + return await chapterTransformer(story, ev); +}); diff --git a/server/api/story/[id]/[chapter]/index.put.ts b/server/api/story/[id]/[chapter]/index.put.ts index fa63b2b..06f4bb4 100644 --- a/server/api/story/[id]/[chapter]/index.put.ts +++ b/server/api/story/[id]/[chapter]/index.put.ts @@ -1,56 +1,56 @@ -import { FormChapter } from "~/lib/client/types/FormStory"; -import { countWords } from "~/lib/functions"; -import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; -import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; -import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; -import { replaceContent, bodyHandler } from "~/lib/server/storyHelpers"; -import { Story } from "~/models/stories"; - -export default eventHandler(async (ev) => { - isLoggedIn(ev); - const story = await storyQuerier(ev); - if (!canModify(ev, story)) { - throw createError({ - statusCode: 403, - message: "Forbidden", - }); - } - const body = await readBody(ev); - const cc: FormChapter = body.chapters[0]; - const cid = story.chapters[ev.context.chapterIndex].id; - const content = await bodyHandler(cc); - await replaceContent(cid!, content); - let ns; - try { - ns = await Story.findOneAndUpdate( - { - "chapters.id": cid, - }, - { - $set: { - "chapters.$.title": cc.chapterTitle, - "chapters.$.summary": cc.summary, - "chapters.$.characters": cc.characters, - "chapters.$.relationships": Array.from(new Set(cc.relationships)), - "chapters.$.bands": cc.bands, - "chapters.$.nsfw": !!cc.nsfw, - "chapters.$.notes": cc.notes, - "chapters.$.words": countWords(content), - "chapters.$.genre": cc.genre, - "chapters.$.loggedInOnly": !!cc.loggedInOnly, - }, - }, - { new: true }, - ); - } catch (e: any) { - throw createError({ - statusCode: 500, - message: e.toString(), - }); - } - return { - data: ns, - message: "Chapter updated", - succes: true, - }; -}); +import { FormChapter } from "~/lib/client/types/FormStory"; +import { countWords } from "~/lib/functions"; +import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; +import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; +import { canModify } from "~/lib/server/middlewareButNotReally/storyPrivileges"; +import { replaceOrUploadContent, bodyHandler } from "~/lib/server/storyHelpers"; +import { Story } from "~/models/stories"; + +export default eventHandler(async (ev) => { + isLoggedIn(ev); + const story = await storyQuerier(ev); + if (!canModify(ev, story)) { + throw createError({ + statusCode: 403, + message: "Forbidden", + }); + } + const body = await readBody(ev); + const cc: FormChapter = body.chapters[0]; + const cid = story.chapters[ev.context.chapterIndex].id; + const content = await bodyHandler(cc); + await replaceOrUploadContent(cid!, content); + let ns; + try { + ns = await Story.findOneAndUpdate( + { + "chapters.id": cid, + }, + { + $set: { + "chapters.$.title": cc.chapterTitle, + "chapters.$.summary": cc.summary, + "chapters.$.characters": cc.characters, + "chapters.$.relationships": Array.from(new Set(cc.relationships)), + "chapters.$.bands": cc.bands, + "chapters.$.nsfw": !!cc.nsfw, + "chapters.$.notes": cc.notes, + "chapters.$.words": countWords(content), + "chapters.$.genre": cc.genre, + "chapters.$.loggedInOnly": !!cc.loggedInOnly, + }, + }, + { new: true }, + ); + } catch (e: any) { + throw createError({ + statusCode: 500, + message: e.toString(), + }); + } + return { + data: ns, + message: "Chapter updated", + succes: true, + }; +});