forked from nswbmw/N-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
|
||
const checkLogin = require('../middlewares/check').checkLogin | ||
const CommentModel = require('../models/comments') | ||
|
||
// POST /comments 创建一条留言 | ||
router.post('/', checkLogin, function (req, res, next) { | ||
const author = req.session.user._id | ||
const postId = req.fields.postId | ||
const content = req.fields.content | ||
|
||
// 校验参数 | ||
try { | ||
if (!content.length) { | ||
throw new Error('请填写留言内容') | ||
} | ||
} catch (e) { | ||
req.flash('error', e.message) | ||
return res.redirect('back') | ||
} | ||
|
||
const comment = { | ||
author: author, | ||
postId: postId, | ||
content: content | ||
} | ||
|
||
CommentModel.create(comment) | ||
.then(function () { | ||
req.flash('success', '留言成功') | ||
// 留言成功后跳转到上一页 | ||
res.redirect('back') | ||
}) | ||
.catch(next) | ||
}) | ||
|
||
// GET /comments/:commentId/remove 删除一条留言 | ||
router.get('/:commentId/remove', checkLogin, function (req, res, next) { | ||
const commentId = req.params.commentId | ||
const author = req.session.user._id | ||
|
||
CommentModel.getCommentById(commentId) | ||
.then(function (comment) { | ||
if (!comment) { | ||
throw new Error('留言不存在') | ||
} | ||
if (comment.author.toString() !== author.toString()) { | ||
throw new Error('没有权限删除留言') | ||
} | ||
CommentModel.delCommentById(commentId) | ||
.then(function () { | ||
req.flash('success', '删除留言成功') | ||
// 删除成功后跳转到上一页 | ||
res.redirect('back') | ||
}) | ||
.catch(next) | ||
}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters