refactor(api): update function calls

This commit is contained in:
parent ff4b94d913
commit d056dac72b
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 68 additions and 68 deletions

@ -1,12 +1,12 @@
import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer"; import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer";
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier"; import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck"; import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck";
export default eventHandler(async (ev) => { export default eventHandler(async (ev) => {
const story = await storyQuerier(ev); const story = await storyQuerier(ev);
const chres = await storyCheck(ev, story); const chres = await storyCheck(ev, story);
if (chres != null) { if (chres != null) {
throw createError(chres); throw createError(chres);
} }
return await chapterTransformer(story, ev); return await chapterTransformer(story, ev);
}); });

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