mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
implement CommentsController
basics
This commit is contained in:
parent
decf416dd9
commit
6b10e4f778
@ -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 = {
|
||||
|
@ -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 })
|
||||
}
|
@ -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"
|
||||
export { default as BadgesController } from "./BadgesController"
|
||||
export { default as CommentsController } from "./CommentsController"
|
Loading…
x
Reference in New Issue
Block a user