發佈商品,咱們須要把它存在一張商品表裏面,仍是老規矩,新建一張t_product表。 node
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;
複製代碼
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;
複製代碼
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) {
//一、查看數據庫中是否有相同用戶名
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;
複製代碼