feat(api): create review endpoints
create endpoints for adding+getting a single chapter's reviews
This commit is contained in:
parent
2b0d478ca7
commit
356cb5ea6b
11
server/api/story/[id]/[chapter]/reviews/index.get.ts
Normal file
11
server/api/story/[id]/[chapter]/reviews/index.get.ts
Normal file
@ -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;
|
||||||
|
});
|
27
server/api/story/[id]/[chapter]/reviews/index.post.ts
Normal file
27
server/api/story/[id]/[chapter]/reviews/index.post.ts
Normal file
@ -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(),
|
||||||
|
};
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user