02——开发博客之数据存储04

API对接mysql(博客更新)

controller/blog,js

const updateBlog = (id, blogData = {}) => {
    const title = blogData.title
    const content = blogData.content

    const sql = `
        update blog set title='${title}',content='${content}' where id=${id}
    `
    return exec(sql).then((updateData) => {
        console.log('updateData is', updateData)
        if (updateData.affectedRows > 0) {
            return true
        }
        return false
    })
}

控制台打印:

updateData is OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '(Rows matched: 1  Changed: 1  Warnings: 0',
  protocol41: true,
  changedRows: 1 }

router/blog.js

// 更新一篇博客
    if (method === 'POST' && req.path ==='/api/blog/update') {
        const result = updateBlog(id, req.body)
        return result.then(val => {
            if (val) {
                return new SuccessModel()
            } else {
                return new ErrorModel('更新博客失败')
            }
        })
    }

在这里插入图片描述

API对接mysql(博客删除)

controller/blog.js

// 删除博客处理逻辑
const delBlog = (id,author) => {
    const sql = `delete from blog where id='${id}' and author='${author}'`
    return exec(sql).then((delData) => {
        
        if (delData.affectedRows > 0) {
            return true
        }
        return false
    })

router/blog.js

// 删除一篇博客
    if (method === 'POST' && req.path ==='/api/blog/delete') {
        // 由于还没有登陆,需模拟登陆后
        const author = 'zhangsan'

        const result = delBlog(id,author)
        return result.then(val => {
            if (result) {
                return new SuccessModel()
            } else {
                return new ErrorModel('删除博客失败')
            }
        })
    }

在这里插入图片描述

API对接mysql(用户登陆)

controller/user.js

const {exec} = require('../db/mysql')

const loginCheck = (username, password) => {
    const sql = `
        select username, realname from users where username='${username}' and password='${password}'
    `
    return exec(sql).then(rows => {
        return rows[0] || {}
    })
}

module.exports = {
    loginCheck
}

router/user.js

const { loginCheck} = require('../controller/user')
const { SuccessModel, ErrorModel} = require('../model/resModel')

const handleUserBlog = (req, res) => {
    const method = req.method  //GET/POST
    
    // 登陆
    if (method === 'POST' && req.path === '/api/user/login') {
        const {username, password} = req.body
        const result = loginCheck(username, password)
        return result.then(data => {
            if (data.username) {
                return new SuccessModel()
            }
            return new ErrorModel('登陆失败')
        }) 
    }
}

module.exports = handleUserBlog

app.js

// 处理 user 路由
        const userResult = handleUserRouter(req,res)
        if (userResult) {
           userResult.then(userData => {
              res.end(
                JSON.stringify(userData)
            )
           })
            return
        }

由于返回的是promise,需要对promise进行处理后再使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值