Node实战系列:零基础入门之增删改查(04天)

552 阅读3分钟

一、昨天已经牛刀小试了一下,成功使用了注册和登录,今天就带你使用常规的增删改查

发布商品,我们需要把它存在一张商品表里面,还是老规矩,新建一张t_product表。

雷震子
雷震子

二、发布商品接口

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) {
        //1、查看数据库中是否有相同用户名
        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;
            //3、如果没有相同用户名,并且有一条记录,则注册成功
            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;

三、获取列表信息接口

var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function getProductList(req, res) {
    //打印请求报文
    //1、查询数据库中是否有用户名
    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;

四、关键字模糊查询接口

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;

五、更新商品接口

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;

六、删除商品接口

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) {
        //1、查看数据库中是否有相同用户名
        connection.query("delete from t_product where pid = " + pid, function (error, results, fields) {
            if (error) throw error;
            //3、如果没有相同用户名,并且有一条记录,则注册成功
            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;

七、 我们看看数据库是否有了

雷震子
雷震子
我们的增删改查就完成了哈。是不是觉得很简单呢??透漏一下下期内容,就是最后的临门一脚,上线。

(ps:有些代码是截图的,可能会复制不了,最后我会上传到github,你可以把代码下载下来哈哈哈。。。)