第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

【金秋打卡】第9天 Node.js+Express+Koa2 開發(fā)Web Server博客 6-8

標(biāo)簽:
征文 活動

课程名称: 2022全新 Node.js+Express+Koa2 开发Web Server博客

课程章节: 6-8 API对接mysql(博客更新和删除)

课程讲师: 双越

课程内容:
修改项目 blog-1 文件夹。
对 博客更新 和 删除进行修改
./src/controller/blog.js

/* 
  数据层
*/
const { exec } = require("../db/mysql");


// 更新一篇博客
const updateBlog = (id, blogData = {}) => {
  // id 就是要更新博客的id
  // blogData 是一个博客对象,包含 title content 属性
  const title = blogData.title;
  const content = blogData.content;

  const sql = `
      update blogs 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;
  });
};

// 删除一篇博客
const delBlog = (id, author) => {
  // id 就是要删除博客的id
  const sql = `delete from blogs where id=${id} and author='${author}'`;

  return exec(sql).then((delData) => {
    // console.log("delData is", delData);
    if (delData.affectedRows > 0) {
      return true;
    }

    return false;
  });
};

module.exports = {

  updateBlog,
  delBlog,
};


./src/router/blog.js

const {
  updateBlog,
  delBlog,
} = require("../controller/blog.js");

const { SuccessModel, ErrorModel } = require("../model/resModel.js");

// 博客相关接口
const handleBlogRouter = (req, res) => {
  const method = req.method; // GET POST
  const id = req.query.id;


  // 更新一篇博客
  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("更新博客失败");
      }
    });
  }

  // 删除一篇博客
  if (method == "POST" && req.path === "/api/blog/del") {
    // 假数据,在开发登录时再改成真数据
    const author = "zhangsan";
    let result = delBlog(id, author);

    return result.then((val) => {
      // 判断是否成功
      if (val) {
        return new SuccessModel("这是删除博客的接口");
      } else {
        return new ErrorModel("删除博客失败");
      }
    });
  }
};

module.exports = handleBlogRouter;

课程收获:
更新mysql博客数据和删除博客数据,有一定的了解
图片描述

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消