Skip to content
Permalink
Browse files

88 comments

  • Loading branch information
fkkmemi committed Jan 31, 2019
1 parent 647d4c5 commit a6f4e7cbd9963dcbf3fafbc3a63f4496e9646cff
@@ -9,8 +9,8 @@ const articleSchema = new mongoose.Schema({
like: { type: Number, default: 0 }
},
ip: { type: String, default: '' },
comments: [],
_user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true },
// _comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment', default: null }],
_user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true, default: null },
_board: { type: mongoose.Schema.Types.ObjectId, ref: 'Board', index: true }
})

@@ -0,0 +1,15 @@
const mongoose = require('mongoose')

mongoose.set('useCreateIndex', true)
const commentSchema = new mongoose.Schema({
content: { type: String, default: '' },
cnt: {
view: { type: Number, default: 0 },
like: { type: Number, default: 0 }
},
ip: { type: String, default: '' },
_user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true, default: null },
_article: { type: mongoose.Schema.Types.ObjectId, ref: 'Article', index: true }
})

module.exports = mongoose.model('Comment', commentSchema)
@@ -3,6 +3,7 @@ const createError = require('http-errors')
const request = require('request')
const Board = require('../../../models/boards')
const Article = require('../../../models/articles')
const Comment = require('../../../models/comments')
const cfg = require('../../../../config')

router.get('/list/:_board', (req, res, next) => {
@@ -43,11 +44,19 @@ router.get('/list/:_board', (req, res, next) => {
router.get('/read/:_id', (req, res, next) => {
const _id = req.params._id

Article.findByIdAndUpdate(_id, { $inc: { 'cnt.view': 1 } }, { new: true })
.select('content cnt.view')
let atc = {}

Article.findByIdAndUpdate(_id, { $inc: { 'cnt.view': 1 } }, { new: true }).lean()
.select('content cnt')
.then(r => {
if (!r) throw new Error('잘못된 게시판입니다')
res.send({ success: true, d: r, token: req.token })
atc = r
atc._comments = []
return Comment.find({ _article: atc._id }).populate({ path: '_user', select: 'id name'}).sort({ _id: 1 }).limit(5)
})
.then(rs => {
if (rs) atc._comments = rs
res.send({ success: true, d: atc, token: req.token })
})
.catch(e => {
res.send({ success: false, msg: e.message })
@@ -11,7 +11,7 @@ router.get('/read/:name', (req, res, next) => {
const name = req.params.name
Board.findOne({ name })
.then(r => {
console.log(r)
// console.log(r)
// 권한으로 못보게 하려면..
// if (r.lv < req.lv) throw new Error(`${name} 게시판을 볼 수 있는 자격이 없습니다.`)
res.send({ success: true, d: r, token: req.token })
@@ -0,0 +1,101 @@
const router = require('express').Router()
const createError = require('http-errors')
const request = require('request')
const Board = require('../../../models/boards')
const Article = require('../../../models/articles')
const Comment = require('../../../models/comments')
const cfg = require('../../../../config')

router.post('/:_article', (req, res, next) => {
const _article = req.params._article
if (!_article) throw createError(400, '게시물이 선택되지 않았습니다')
const { content, response } = req.body

if (!content) throw createError(400, '내용이 없습니다')
if (!response) throw createError(400, '로봇 검증이 없습니다')

const ro = {
uri: 'https://www.google.com/recaptcha/api/siteverify',
json: true,
form: {
secret: cfg.recaptchaSecretKey,
response,
remoteip: req.ip
}
}
request.post(ro, (err, response, body) => {
if (err) throw createError(401, '로봇 검증 실패입니다')

Article.findById(_article)
.then(r => {
if (!r) throw createError(400, '잘못된 게시물입니다')
// if (r.lv < req.user.lv) throw createError(403, '권한이 없습니다')
const cmt = {
content,
_article,
ip: '1.1.1.1',//req.ip,
_user: null
}
if (req.user._id) cmt._user = req.user._id
return Comment.create(cmt)
})
.then(r => {
if (!r) throw new Error('게시물이 생성되지 않았습니다')
res.send({ success: true, d: r, token: req.token })
})
.catch(e => {
res.send({ success: false, msg: e.message })
})
})
})


router.put('/:_id', (req, res, next) => {
if (!req.user._id) throw createError(403, '댓글 수정 권한이 없습니다')
const _id = req.params._id

const { content } = req.body

if (!content) throw createError(400, '내용이 없습니다')

Comment.findById(_id)
.then(r => {
if (!r) throw new Error('댓글 존재하지 않습니다')
if (!r._user) throw new Error('손님 댓글 수정이 안됩니다')
if (r._user.toString() !== req.user._id) throw new Error('본인이 작성한 댓글이 아닙니다')
return Comment.findByIdAndUpdate(_id, { $set: { content } }, { new: true }).populate('_user')
})
.then(r => {
res.send({ success: true, d: r, token: req.token })
})
.catch(e => {
res.send({ success: false, msg: e.message })
})
})

router.delete('/:_id', (req, res, next) => {
if (!req.user._id) throw createError(403, '댓글 삭제 권한이 없습니다')
const _id = req.params._id

Comment.findById(_id).populate('_user', 'lv')
.then(r => {
if (!r) throw new Error('댓글이 존재하지 않습니다')
if (!r._user) {
if (req.user.lv > 0) throw new Error('손님 댓글은 삭제가 안됩니다')
}
else {
if (r._user._id.toString() !== req.user._id) {
if (r._user.lv < req.user.lv) throw new Error('본인이 작성한 댓글이 아닙니다')
}
}
return Comment.deleteOne({ _id })
})
.then(r => {
res.send({ success: true, d: r, token: req.token })
})
.catch(e => {
res.send({ success: false, msg: e.message })
})
})

module.exports = router;
@@ -65,6 +65,7 @@ router.all('*', function(req, res, next) {

router.use('/page', require('./page'))
router.use('/article', require('./article'))
router.use('/comment', require('./comment'))
router.use('/manage', require('./manage'))
router.use('/user', require('./user'))

0 comments on commit a6f4e7c

Please sign in to comment.
You can’t perform that action at this time.