next/server/api/story/[id]/[chapter]/reviews/index.post.ts
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 088232f750
refactor(api & server/utils): refactor imports of db helpers
create an index file which exports the functions we need in one accessible place
2023-12-11 21:29:46 -05:00

28 lines
723 B
TypeScript

import san from "sanitize-html";
import { storyQuerier } from "~/lib/server/dbHelpers";
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
import { Story } from "~/models/stories";
import { Review } from "~/models/stories/review";
export default eventHandler(async (ev) => {
isLoggedIn(ev);
let body = await readBody(ev);
let parent = await storyQuerier(ev);
let newReview = new Review({
text: san(body.review),
leftOn: parent._id,
author: ev.context.currentUser!._id,
replyingTo: null,
replies: [],
datePosted: new Date(),
});
await newReview.save();
parent.reviews += 1;
await parent.save();
// TODO: emails lol
return {
success: true,
data: newReview.toObject(),
};
});