Skip to content

Commit

Permalink
88 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fkkmemi committed Jan 31, 2019
1 parent 647d4c5 commit a6f4e7c
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 30 deletions.
4 changes: 2 additions & 2 deletions be/models/articles.js
Expand Up @@ -9,8 +9,8 @@ const articleSchema = new mongoose.Schema({
like: { type: Number, default: 0 } like: { type: Number, default: 0 }
}, },
ip: { type: String, default: '' }, ip: { type: String, default: '' },
comments: [], // _comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment', default: null }],
_user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true }, _user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true, default: null },
_board: { type: mongoose.Schema.Types.ObjectId, ref: 'Board', index: true } _board: { type: mongoose.Schema.Types.ObjectId, ref: 'Board', index: true }
}) })


Expand Down
15 changes: 15 additions & 0 deletions be/models/comments.js
@@ -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)
15 changes: 12 additions & 3 deletions be/routes/api/article/index.js
Expand Up @@ -3,6 +3,7 @@ const createError = require('http-errors')
const request = require('request') const request = require('request')
const Board = require('../../../models/boards') const Board = require('../../../models/boards')
const Article = require('../../../models/articles') const Article = require('../../../models/articles')
const Comment = require('../../../models/comments')
const cfg = require('../../../../config') const cfg = require('../../../../config')


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


Article.findByIdAndUpdate(_id, { $inc: { 'cnt.view': 1 } }, { new: true }) let atc = {}
.select('content cnt.view')
Article.findByIdAndUpdate(_id, { $inc: { 'cnt.view': 1 } }, { new: true }).lean()
.select('content cnt')
.then(r => { .then(r => {
if (!r) throw new Error('잘못된 게시판입니다') 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 => { .catch(e => {
res.send({ success: false, msg: e.message }) res.send({ success: false, msg: e.message })
Expand Down
2 changes: 1 addition & 1 deletion be/routes/api/board/index.js
Expand Up @@ -11,7 +11,7 @@ router.get('/read/:name', (req, res, next) => {
const name = req.params.name const name = req.params.name
Board.findOne({ name }) Board.findOne({ name })
.then(r => { .then(r => {
console.log(r) // console.log(r)
// 권한으로 못보게 하려면.. // 권한으로 못보게 하려면..
// if (r.lv < req.lv) throw new Error(`${name} 게시판을 볼 수 있는 자격이 없습니다.`) // if (r.lv < req.lv) throw new Error(`${name} 게시판을 볼 수 있는 자격이 없습니다.`)
res.send({ success: true, d: r, token: req.token }) res.send({ success: true, d: r, token: req.token })
Expand Down
101 changes: 101 additions & 0 deletions be/routes/api/comment/index.js
@@ -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;
1 change: 1 addition & 0 deletions be/routes/api/index.js
Expand Up @@ -65,6 +65,7 @@ router.all('*', function(req, res, next) {


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


Expand Down

0 comments on commit a6f4e7c

Please sign in to comment.