diff --git a/server/api/story/[id]/[chapter]/reviews/index.get.ts b/server/api/story/[id]/[chapter]/reviews/index.get.ts new file mode 100644 index 0000000..87fb071 --- /dev/null +++ b/server/api/story/[id]/[chapter]/reviews/index.get.ts @@ -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; +}); diff --git a/server/api/story/[id]/[chapter]/reviews/index.post.ts b/server/api/story/[id]/[chapter]/reviews/index.post.ts new file mode 100644 index 0000000..03d6a1f --- /dev/null +++ b/server/api/story/[id]/[chapter]/reviews/index.post.ts @@ -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(), + }; +});