feat(api): create review endpoints

create endpoints for adding+getting a single chapter's reviews
This commit is contained in:
parent 2b0d478ca7
commit 356cb5ea6b
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 38 additions and 0 deletions

@ -0,0 +1,11 @@
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
import { Review } from "~/models/stories/review";
export default eventHandler(async (ev) => {
let story = await storyQuerier(ev);
let comments = await Review.find({
whichChapter: story.chapters[ev.context.chapterIndex || 0].id,
replyingTo: null,
});
return comments;
});

@ -0,0 +1,27 @@
import san from "sanitize-html";
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
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(),
};
});