NodeJS实战 利用Express&MongoDB搭建博客(8)内容修改

35 篇文章 0 订阅
17 篇文章 0 订阅

内容修改

    /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'
        });
    })
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值