以下是示例link的示例,说明了我如何针对该ID更新或REST记录从数据库获得RESTful API POST和PUT返回完整响应。当数据保存在db中时,返回。同样,如果更新记录也获得了与该ID相关的记录,则针对该ID进行更新且未更新的记录-如上面的链接所示。
我的代码:
var mc = require('../config/databas').mc;
module.exports = {
createPlace: function (req, res) {
var requestData = req.body;
if (!requestData.PlaceName) {
return res.status(400).json({ code: "place creation failed", message: "Place name not given" });
}
var placedata = {
PlaceName: requestData.PlaceName,
PlaceCategoryCode:requestData.PlaceCategoryCode,
Description: requestData.Description,
CompanyId_FK: requestData.CompanyId_FK,
OwnerCompanyId_FK: requestData.OwnerCompanyId_FK,
IsArchived: requestData.IsArchived,
ModifiedDate: new Date(),
CreatedBy_FK: requestData.CreatedBy_FK,
ModifiedBy_FK:requestData.ModifiedBy_FK,
CreatedDate: new Date()
};
mc.query('INSERT INTO mytable SET ?', placedata, function (err, result, fields) {
if (err) {
console.log(err);
return res.status(400).json({ code: "place creation failed", message: "Place name not given" });
}
return res.status(200).json({ code: "place cretaed", message: "success" });
});
},
updatePlaceById: function (req, res) {
var placeId = req.params.placeId;
var requestData = req.body;
if (!requestData.PlaceName) {
return res.status(400).json({ code: "Place UpdateFailed", message: "Name fields required " });
}
mc.query('SELECT * FROM mytable where placeId=?', [placeId], function (err, result, fields) {
if (err) {
console.log(err)
return res.status(400).json({ code: "Place UpdateFailed", message: "Error occure while updating " });
}
if (result.length === 0) {
return res.status(400).json({ code: "Place not found for this id ", message: "This id has no place" });
}
mc.query("UPDATE mytable SET PlaceName=?,Description=? where placeId=?", [requestData.PlaceName, requestData.Description, placeId], function (err, result, fields) {
if (err) {
console.log(err);
return res.status(500).json({ code: "Place UpdateFailed", message: "Error occure while updating " });
}
return res.status(200).json({ code: "place updated ",data:requestData })
return res.send(result);
});
});
},