Node實戰系列:零基礎入門之增刪改查(04天)

1、昨天已經牛刀小試了一下,成功使用了註冊和登陸,今天就帶你使用常規的增刪改查

發佈商品,咱們須要把它存在一張商品表裏面,仍是老規矩,新建一張t_product表。 node

雷震子
雷震子

2、發佈商品接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function addProduct(req, res) {
    //打印請求報文
    logger.info(req.body);
    var param = req.body;
    var productName = param.productName;
    var productPrice = param.productPrice;
    var productType = param.productType;
    var productImg = param.productImg;
    var productDes = param.productDes;
    var response = new Response(false, '', -1);
    if (productName && productPrice && productType && productImg && productDes) {
        //一、查看數據庫中是否有相同用戶名
        connection.query("insert into t_product (p_name, p_price, p_type,p_img,p_des) VALUES(?,?,?,?,?)", [productName, productPrice, 
            productType, productImg, productDes], function (error, results, fields) {
            if (error) throw error;
            //三、若是沒有相同用戶名,而且有一條記錄,則註冊成功
            if (results.affectedRows == 1) {
                response = new Response(false, '添加成功', 1, null);
                logger.info(response);
                res.send(response);
            } else {
                response = new Response(false, '添加成功', -1, null);
                logger.info(response);
                res.send(response);
            }
        });
    } else {
        response = new Response(false, '有參數爲空', -1);
        logger.info(response);
        res.send(response);
    }
}
module.exports = addProduct;
複製代碼

3、獲取列表信息接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function getProductList(req, res) {
    //打印請求報文
    //一、查詢數據庫中是否有用戶名
    connection.query("select * from t_product", [], function (error, results, fields) {
        if (error) throw error;
        var response = new Response(true, '查詢成功', 1, results);
        logger.info(response);
        res.send(response);
    });
}
module.exports = getProductList;
複製代碼

4、關鍵字模糊查詢接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function getListByCondition(req, res) {
    //打印請求報文
    logger.info(req.body);
    var param = req.body;
    var productName = "'" + param.productName + "'";
    var productType = param.productType;
    var response = new Response(false, '', -1, []);
    if (productName) {
        connection.query("select * from t_product where p_name = " + productName, function (error, results, fields) {
            if (error) throw error;
            var response = new Response(true, 'productName查詢成功', 1, results);
            logger.info(response);
            res.send(response);
        });
    } else if (productType || productType === 0) {
        connection.query("select * from t_product where p_type =" + productType, function (error, results, fields) {
            if (error) throw error;
            var response = new Response(true, 'productType||productType===0 查詢成功', 1, results);
            logger.info(response);
            res.send(response);
        });
    } else if (productName && productType) {
        connection.query("select * from t_product where p_name =" + productName + " and p_type = " + productType, function (error, results, fields) {
            if (error) throw error;
            var response = new Response(true, 'productName && productType查詢成功', 1, results);
            logger.info(response);
            res.send(response);
        });
    } else {
        response = new Response(true, '', 1, []);
        logger.info(response);
        res.send(response);
    }
}
module.exports = getListByCondition;
複製代碼

5、更新商品接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function updateProduct(req, res) {
    //打印請求報文
    logger.info('xxxxxx更新===' + req);
    var param = req.body;
    var productId = param.productId;
    var productName = "'" + param.product_name + "'";
    var productType = param.product_type;
    var productPrice = param.product_price;
    var productImg = "'" + param.product_img + "'";
    var productDes = "'" + param.product_des + "'";
    var response = new Response(false, '', -1, []);
    if (productId != '') {
        connection.query("update t_product set p_name =" + productName +
            ",p_type = " + productType +
            ",p_price = " + productPrice +
            ",p_img = " + productImg +
            ",p_des = " + productDes +
            " where pid = " + productId, function (error, results, fields) {
                if (error) throw error;
                response = new Response(true, '更新成功', 1, results);
                logger.info(response);
                res.send(response);
            });
    } else {
        res.send('沒有商品id');
    }
}
module.exports = updateProduct;
複製代碼

6、刪除商品接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function deleteProduct(req, res) {
    //打印請求報文
    logger.info(req.body);
    var param = req.body;
    var pid = param.pid;
    var response = new Response(false, '', -1);
    if (pid) {
        //一、查看數據庫中是否有相同用戶名
        connection.query("delete from t_product where pid = " + pid, function (error, results, fields) {
            if (error) throw error;
            //三、若是沒有相同用戶名,而且有一條記錄,則註冊成功
            if (results.affectedRows == 1) {
                response = new Response(false, '刪除成功', 1, null);
                logger.info(response);
                res.send(response);
            } else {
                response = new Response(false, '刪除失敗', -1, null);
                logger.info(response);
                res.send(response);
            }
        });
    } else {
        response = new Response(false, '刪除失敗,商品id爲空', -1);
        logger.info(response);
        res.send(response);
    }
}
module.exports = deleteProduct;
複製代碼

7、 咱們看看數據庫是否有了

雷震子
雷震子
咱們的增刪改查就完成了哈。是否是以爲很簡單呢??透漏一下下期內容,就是最後的臨門一腳,上線。

(ps:有些代碼是截圖的,可能會複製不了,最後我會上傳到github,你能夠把代碼下載下來哈哈哈。。。)

相關文章
相關標籤/搜索