next/lib/server/dbHelpers/storyQuerier.ts
2023-12-29 16:39:41 -05:00

27 lines
884 B
TypeScript

import type { H3Event, EventHandlerRequest } from "h3";
import { Challenge } from "@models/challenges/gen";
import { Story } from "@models/stories";
import { isIdNan } from "@server/middlewareButNotReally";
export default async function (ev: H3Event<EventHandlerRequest>) {
const id = isIdNan(ev);
const chapterIndex = ev.context.chapterIndex;
if (isNaN(id) || isNaN(chapterIndex))
throw createError({
statusCode: 404,
message: "Not found.",
});
const story = await Story.findById(id)
.populate("author", "username profile blocked")
.populate("coAuthor", "username profile")
.populate("chapters.bands")
.populate({
path: "ficmas",
populate: { path: "wisher", select: "_id username" },
})
.populate({ path: "challenge", model: Challenge })
.exec();
if (story == null)
throw createError({ statusCode: 404, message: "Not found." });
return story;
}