内容修改
/views/admin/content_edit.html
{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
<li><a href="/">管理首页</a></li>
<li><span>内容修改</span></li>
</ol>
<h3>内容修改</h3>
<form role="form" method="post">
<div class="form-group">
<label for="title">分类:</label>
<select name="category" id="category" class="form-control">
{%for category in categories%}
{%if content.category._id.toString()==category._id.toString()%}
<option value="{{category.id}}" selected>{{category.name}}</option>
{%else%}
<option value="{{category.id}}">{{category.name}}</option>
{%endif%}
{%endfor%}
</select>
</div>
<div class="form-group">
<label for="title">标题:</label>
<input type="text" value="{{content.title}}" class="form-control" id="title" placeholder="请输入内容标题" name="title">
</div>
<div class="form-group">
<label for="description">简介:</label>
<textarea name="description" id="description" class="form-control" rows="5" placeholder="请输入内容简介">{{content.description}}</textarea>
</div>
<div class="form-group">
<label for="content">内容:</label>
<textarea name="content" id="content" class="form-control" rows="10" placeholder="请输入内容">{{content.content}}</textarea>
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}
/routers/admin.js
/**
* 修改内容
*/
router.get('/content/edit',function (req,res) {
var id = req.query.id || '';
var categories = [];
Category.find().sort({_id:-1}).then(function (rs) {
categories = rs;
return Content.findOne({
_id:id
}).populate('category');
}).then(function (content) {
if(!content){
res.render('admin/error',{
userInfo:req.userInfo,
message:'文章不存在'
});
return Promise.reject();
}else {
res.render('admin/content_edit',{
userInfo:req.userInfo,
categories:categories,
content:content
})
}
});
});
修改内容保存
/routers/admin.js
/**
* 保存修改内容
*/
router.post('/content/edit',function (req,res) {
var id = req.query.id||'';
if(req.body.category==''){
res.render('admin/error',{
userInfo:req.userInfo,
message:'分类不能为空'
});
return;
}
if(req.body.title==''){
res.render('admin/error',{
userInfo:req.userInfo,
message:'标题不能为空'
});
return;
}
Content.update({
_id:id
},{
category: req.body.category,
title:req.body.title,
description:req.body.description,
content:req.body.content
}).then(function () {
res.render('admin/success',{
userInfo:req.userInfo,
message:'文章修改成功',
url:'/admin/content/edit?id='+id
})
});
});
内容删除
/routers/admin.js
/**
* 内容删除
*/
router.get('/content/delete',function (req,res) {
var id = req.query.id||'';
Content.remove({
_id:id
}).then(function () {
res.render('admin/success',{
userInfo:req.userInfo,
message:'删除成功',
url:'/admin/content'
});
})
});