From 356cb5ea6ba8ebc554a0c474e194c9ad103ae93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Mon, 2 Oct 2023 21:43:24 -0400 Subject: [PATCH] feat(api): create review endpoints create endpoints for adding+getting a single chapter's reviews --- .../story/[id]/[chapter]/reviews/index.get.ts | 11 ++++++++ .../[id]/[chapter]/reviews/index.post.ts | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 server/api/story/[id]/[chapter]/reviews/index.get.ts create mode 100644 server/api/story/[id]/[chapter]/reviews/index.post.ts 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(), + }; +});