refactor(api): rename imports and refactor error messages
This commit is contained in:
parent
08c49e4bdd
commit
b000c6734a
@ -1,3 +1,4 @@
|
|||||||
|
import { messages } from "~/lib/server/constants";
|
||||||
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
|
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
|
||||||
import { canDelete } from "~/lib/server/middlewareButNotReally/storyPrivileges";
|
import { canDelete } from "~/lib/server/middlewareButNotReally/storyPrivileges";
|
||||||
import { Story } from "~/models/stories";
|
import { Story } from "~/models/stories";
|
||||||
@ -13,6 +14,6 @@ export default eventHandler(async (ev) => {
|
|||||||
}
|
}
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 403,
|
statusCode: 403,
|
||||||
message: "Forbidden",
|
message: messages[403],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,80 +1,81 @@
|
|||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
import { Document } from "mongoose";
|
import { Document } from "mongoose";
|
||||||
import { IStory, Story } from "~/models/stories";
|
import { IStory, Story } from "~/models/stories";
|
||||||
import { FormStory } from "~/lib/client/types/FormStory";
|
import { FormStory } from "~/lib/client/types/form/story";
|
||||||
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 {
|
import {
|
||||||
bodyHandler,
|
bodyHandler,
|
||||||
getBucket,
|
getBucket,
|
||||||
modelFormChapter,
|
modelFormChapter,
|
||||||
replaceOrUploadContent,
|
replaceOrUploadContent,
|
||||||
} from "~/lib/server/storyHelpers";
|
} from "~/lib/server/storyHelpers";
|
||||||
import { countWords } from "~/lib/functions";
|
import { countWords } from "~/lib/functions";
|
||||||
|
import { messages } from "~/lib/server/constants";
|
||||||
export default eventHandler(async (ev) => {
|
|
||||||
let os:
|
export default eventHandler(async (ev) => {
|
||||||
| (Document<unknown, {}, IStory> &
|
let os:
|
||||||
IStory &
|
| (Document<unknown, {}, IStory> &
|
||||||
Required<{
|
IStory &
|
||||||
_id: number;
|
Required<{
|
||||||
}>)
|
_id: number;
|
||||||
| null = await storyQuerier(ev);
|
}>)
|
||||||
isLoggedIn(ev);
|
| null = await storyQuerier(ev);
|
||||||
if (!canModify(ev, os)) {
|
isLoggedIn(ev);
|
||||||
throw createError({
|
if (!canModify(ev, os)) {
|
||||||
statusCode: 403,
|
throw createError({
|
||||||
message: "Forbidden",
|
statusCode: 403,
|
||||||
});
|
message: messages[403],
|
||||||
}
|
});
|
||||||
const body = await readBody<FormStory>(ev);
|
}
|
||||||
const update: Partial<IStory> = {
|
const body: FormStory = await readBody<FormStory>(ev);
|
||||||
title: body.title,
|
const update: Partial<IStory> = {
|
||||||
completed: body.completed,
|
title: body.title,
|
||||||
chapters: [],
|
completed: body.completed,
|
||||||
};
|
chapters: [],
|
||||||
for (const oc of os.chapters) {
|
};
|
||||||
let filename = `/stories/${oc.id}.txt`;
|
for (const oc of os.chapters) {
|
||||||
const bucket = getBucket();
|
let filename = `/stories/${oc.id}.txt`;
|
||||||
const curs = bucket.find({ filename }).limit(1);
|
const bucket = getBucket();
|
||||||
for await (const d of curs) {
|
const curs = bucket.find({ filename }).limit(1);
|
||||||
await bucket.delete(d._id);
|
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);
|
for (const c of body.chapters) {
|
||||||
const cont = await bodyHandler(c);
|
let idx = os.chapters.findIndex((k) => k.id === c.id);
|
||||||
if (idx === -1) {
|
const cont = await bodyHandler(c);
|
||||||
update.chapters!.push({
|
if (idx === -1) {
|
||||||
...modelFormChapter(c),
|
update.chapters!.push({
|
||||||
posted: new Date(Date.now()),
|
...modelFormChapter(c),
|
||||||
});
|
posted: new Date(Date.now()),
|
||||||
} else {
|
});
|
||||||
update.chapters!.push({
|
} else {
|
||||||
...modelFormChapter(c),
|
update.chapters!.push({
|
||||||
id: os.chapters[idx].id,
|
...modelFormChapter(c),
|
||||||
posted: os.chapters[idx].posted,
|
id: os.chapters[idx].id,
|
||||||
});
|
posted: os.chapters[idx].posted,
|
||||||
replaceOrUploadContent(os.chapters![idx].id, cont);
|
});
|
||||||
}
|
replaceOrUploadContent(os.chapters![idx].id, cont);
|
||||||
update.chapters![update.chapters!.length - 1].words = countWords(cont);
|
}
|
||||||
}
|
update.chapters![update.chapters!.length - 1].words = countWords(cont);
|
||||||
os = await Story.findOneAndUpdate(
|
}
|
||||||
{
|
os = await Story.findOneAndUpdate(
|
||||||
_id: os._id,
|
{
|
||||||
},
|
_id: os._id,
|
||||||
update,
|
},
|
||||||
{ new: true },
|
update,
|
||||||
);
|
{ new: true },
|
||||||
if (!os) {
|
);
|
||||||
throw createError({
|
if (!os) {
|
||||||
statusCode: 500,
|
throw createError({
|
||||||
message: "Something went wrong.",
|
statusCode: 500,
|
||||||
});
|
message: "Something went wrong.",
|
||||||
}
|
});
|
||||||
return {
|
}
|
||||||
success: true,
|
return {
|
||||||
data: os.toObject(),
|
success: true,
|
||||||
};
|
data: os.toObject(),
|
||||||
});
|
};
|
||||||
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
import san from "sanitize-html";
|
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 isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
|
||||||
import {
|
import {
|
||||||
getBucket,
|
getBucket,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user