From 6b10e4f778a250f100059fc961a02589502ae02e Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 16 Sep 2022 15:02:11 +0200 Subject: [PATCH] implement `CommentsController` basics --- .../controllers/CommentsController/index.js | 59 ++++++++++++++++++- .../CommentsController/methods/getComments.js | 11 ++++ packages/server/src/controllers/index.js | 3 +- 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 packages/server/src/controllers/CommentsController/methods/getComments.js diff --git a/packages/server/src/controllers/CommentsController/index.js b/packages/server/src/controllers/CommentsController/index.js index b4d1c805..b900ce49 100644 --- a/packages/server/src/controllers/CommentsController/index.js +++ b/packages/server/src/controllers/CommentsController/index.js @@ -2,15 +2,70 @@ import { Controller } from "linebridge/dist/server" import { User, Post, Comment } from "../../models" import { Schematized } from "../../lib" +import getComments from "./methods/getComments" + export default class CommentsController extends Controller { static refName = "CommentsController" get = { - + "/comments": { + fn: Schematized({ + required: ["targetId"], + select: ["targetId"], + }, async (req, res) => { + + }) + }, + "/post/:post_id/comments": { + fn: async (req, res) => { + const { post_id } = req.params + + let comments = await Comment.find({ parent_id: post_id }).catch(err => { + res.status(500).json({ message: err.message }) + + return false + }) + + if (comments) { + // fullfill comments with user data + comments = await Promise.all(comments.map(async comment => { + const user = await User.findById(comment.user_id) + + return { + ...comment.toObject(), + user: user.toObject(), + } + })) + + return res.json(comments) + } + } + } } post = { - + "/post/:post_id/comment": { + middlewares: ["withAuthentication"], + fn: Schematized({ + required: ["message"], + select: ["message"], + }, async (req, res) => { + const { post_id } = req.params + const { message } = req.selection + + const comment = new Comment({ + user_id: req.user._id.toString(), + parent_id: post_id, + message: message, + }) + + await comment.save() + + if (comment) { + return res.json(comment) + } + }) + } } put = { diff --git a/packages/server/src/controllers/CommentsController/methods/getComments.js b/packages/server/src/controllers/CommentsController/methods/getComments.js new file mode 100644 index 00000000..74bcdf27 --- /dev/null +++ b/packages/server/src/controllers/CommentsController/methods/getComments.js @@ -0,0 +1,11 @@ +import { User, Post, Comment } from "../../../models" + +export default (payload) => { + const { parent_id, _id } = payload + + if (typeof _id !== "undefined") { + return Comment.findById(_id) + } + + return Comment.find({ parent_id }) +} \ No newline at end of file diff --git a/packages/server/src/controllers/index.js b/packages/server/src/controllers/index.js index 01c07fb0..fd2d14c2 100644 --- a/packages/server/src/controllers/index.js +++ b/packages/server/src/controllers/index.js @@ -6,4 +6,5 @@ export { default as FilesController } from "./FilesController" export { default as PublicController } from "./PublicController" export { default as PostsController } from "./PostsController" export { default as StreamingController } from "./StreamingController" -export { default as BadgesController } from "./BadgesController" \ No newline at end of file +export { default as BadgesController } from "./BadgesController" +export { default as CommentsController } from "./CommentsController" \ No newline at end of file