|
|
|
@ -12,251 +12,293 @@ var catServ = authorization.getService("CategoryService");
|
|
|
|
|
var attrServ = authorization.getService("AttributeService");
|
|
|
|
|
|
|
|
|
|
// 获取分类列表
|
|
|
|
|
// 引入Express框架的路由器对象(假设前面已经正确引入了Express)
|
|
|
|
|
const router = require('express').Router();
|
|
|
|
|
|
|
|
|
|
// 处理获取分类列表的GET请求,路径为根路径 "/"
|
|
|
|
|
router.get("/",
|
|
|
|
|
function(req,res,next){
|
|
|
|
|
// 参数验证
|
|
|
|
|
// if(!req.query.pagenum || req.query.pagenum <= 0) return res.sendResult(null,400,"pagenum 参数错误");
|
|
|
|
|
// if(!req.query.pagesize || req.query.pagesize <= 0) return res.sendResult(null,400,"pagesize 参数错误");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
function(req,res,next){
|
|
|
|
|
var conditions = null;
|
|
|
|
|
if(req.query.pagenum && req.query.pagesize) {
|
|
|
|
|
conditions = {
|
|
|
|
|
"pagenum" : req.query.pagenum,
|
|
|
|
|
"pagesize" : req.query.pagesize
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catServ.getAllCategories(req.query.type,conditions,function(err,result){
|
|
|
|
|
if(err) return res.sendResult(null,400,"获取分类列表失败");
|
|
|
|
|
res.sendResult(result,200,"获取成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
});
|
|
|
|
|
// 第一个中间件函数,用于参数验证
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 验证pagenum参数是否存在且大于0,如果不符合要求则返回错误响应,状态码400表示请求参数错误
|
|
|
|
|
// 此处代码被注释掉了,若取消注释则会进行该参数验证逻辑
|
|
|
|
|
// if (!req.query.pagenum || req.query.pagenum <= 0) return res.sendResult(null, 400, "pagenum 参数错误");
|
|
|
|
|
// 验证pagesize参数是否存在且大于0,如果不符合要求则返回错误响应
|
|
|
|
|
// 此处代码被注释掉了,若取消注释则会进行该参数验证逻辑
|
|
|
|
|
// if (!req.query.pagesize || req.query.pagesize <= 0) return res.sendResult(null, 400, "pagesize 参数错误");
|
|
|
|
|
// 如果参数验证通过,调用next()将控制权传递给下一个中间件或路由处理函数
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 第二个中间件函数,用于获取分类列表的业务逻辑处理
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
var conditions = null;
|
|
|
|
|
// 如果pagenum和pagesize参数都存在,则构建包含这两个参数的查询条件对象
|
|
|
|
|
if (req.query.pagenum && req.query.pagesize) {
|
|
|
|
|
conditions = {
|
|
|
|
|
"pagenum": req.query.pagenum,
|
|
|
|
|
"pagesize": req.query.pagesize
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用catServ服务(假设是自定义的分类相关服务模块)的getAllCategories方法,传入分类类型和查询条件等参数
|
|
|
|
|
// 处理获取分类列表的异步操作,成功则返回结果,失败则返回错误信息
|
|
|
|
|
catServ.getAllCategories(req.query.type, conditions, function (err, result) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, "获取分类列表失败");
|
|
|
|
|
res.sendResult(result, 200, "获取成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 创建分类
|
|
|
|
|
// 处理创建分类的POST请求,路径为 "/"
|
|
|
|
|
router.post("/",
|
|
|
|
|
// 参数验证
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.body.cat_name) {
|
|
|
|
|
return res.sendResult(null,400,"必须提供分类名称");
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
catServ.addCategory({
|
|
|
|
|
"cat_pid":req.body.cat_pid,
|
|
|
|
|
"cat_name":req.body.cat_name,
|
|
|
|
|
"cat_level":req.body.cat_level
|
|
|
|
|
},function(err,result) {
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(result,201,"创建成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查请求体中是否包含cat_name字段,如果没有则返回错误响应
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.body.cat_name) {
|
|
|
|
|
return res.sendResult(null, 400, "必须提供分类名称");
|
|
|
|
|
}
|
|
|
|
|
// 参数验证通过,将控制权传递给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于创建分类的具体操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用catServ服务的addCategory方法,传入包含分类相关信息(父分类ID、分类名称、分类级别等)的对象
|
|
|
|
|
// 处理创建分类的异步操作,成功则返回创建后的结果,失败则返回错误信息
|
|
|
|
|
catServ.addCategory({
|
|
|
|
|
"cat_pid": req.body.cat_pid,
|
|
|
|
|
"cat_name": req.body.cat_name,
|
|
|
|
|
"cat_level": req.body.cat_level
|
|
|
|
|
}, function (err, result) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(result, 201, "创建成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 处理根据分类ID获取分类详情的GET请求,路径中包含分类ID参数,如 "/:id"
|
|
|
|
|
router.get("/:id",
|
|
|
|
|
// 参数验证
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 正常业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
catServ.getCategoryById(req.params.id,function(err,result){
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(result,200,"获取成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID参数是否存在以及是否为数字类型
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 正常业务逻辑中间件,用于根据分类ID获取分类详情的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用catServ服务的getCategoryById方法,传入分类ID参数
|
|
|
|
|
// 处理获取分类详情的异步操作,成功则返回结果,失败则返回错误信息
|
|
|
|
|
catServ.getCategoryById(req.params.id, function (err, result) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(result, 200, "获取成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 删除分类
|
|
|
|
|
// 处理删除分类的DELETE请求,路径中包含分类ID参数,如 "/:id"
|
|
|
|
|
router.delete("/:id",
|
|
|
|
|
// 参数验证
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
catServ.deleteCategory(req.params.id,function(msg) {
|
|
|
|
|
res.sendResult(null,200,msg);
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID参数是否存在以及是否为数字类型
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于执行删除分类的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用catServ服务的deleteCategory方法,传入分类ID参数
|
|
|
|
|
// 处理删除分类的异步操作,成功则返回提示信息,失败则返回错误信息
|
|
|
|
|
catServ.deleteCategory(req.params.id, function (msg) {
|
|
|
|
|
res.sendResult(null, 200, msg);
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 更新分类
|
|
|
|
|
// 处理更新分类的PUT请求,路径中包含分类ID参数,如 "/:id"
|
|
|
|
|
router.put("/:id",
|
|
|
|
|
// 参数验证
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
if(!req.body.cat_name || req.body.cat_name == "") return res.sendResult(null,400,"分类名称不能为空");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
catServ.updateCategory(req.params.id,req.body.cat_name,function(err,result) {
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(result,200,"更新成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID参数是否存在、是否为数字类型,以及分类名称是否为空
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
if (!req.body.cat_name || req.body.cat_name == "") return res.sendResult(null, 400, "分类名称不能为空");
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于执行更新分类的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用catServ服务的updateCategory方法,传入分类ID和新的分类名称等参数
|
|
|
|
|
// 处理更新分类的异步操作,成功则返回更新后的结果,失败则返回错误信息
|
|
|
|
|
catServ.updateCategory(req.params.id, req.body.cat_name, function (err, result) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(result, 200, "更新成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 通过参数方式查询静态参数还是动态参数
|
|
|
|
|
// 处理通过分类ID获取分类参数(attributes)的GET请求,路径为 "/:id/attributes"
|
|
|
|
|
router.get("/:id/attributes",
|
|
|
|
|
// 验证参数
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
if(!req.query.sel || (req.query.sel != "only" && req.query.sel != "many")) {
|
|
|
|
|
return res.sendResult(null,400,"属性类型必须设置");
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
// attrServ
|
|
|
|
|
attrServ.getAttributes(req.params.id,req.query.sel,function(err,attributes){
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(attributes,200,"获取成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID参数是否存在、是否为数字类型,以及属性类型(sel)是否设置正确
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
if (!req.query.sel || (req.query.sel!= "only" && req.query.sel!= "many")) {
|
|
|
|
|
return res.sendResult(null, 400, "属性类型必须设置");
|
|
|
|
|
}
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于获取分类参数的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用attrServ服务(假设是自定义的属性相关服务模块)的getAttributes方法,传入分类ID和属性类型等参数
|
|
|
|
|
// 处理获取分类参数的异步操作,成功则返回获取到的属性列表,失败则返回错误信息
|
|
|
|
|
attrServ.getAttributes(req.params.id, req.query.sel, function (err, attributes) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(attributes, 200, "获取成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 获取参数详情
|
|
|
|
|
// 处理根据分类ID和参数ID获取参数详情的GET请求,路径为 "/:id/attributes/:attrId"
|
|
|
|
|
router.get("/:id/attributes/:attrId",
|
|
|
|
|
// 验证参数
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
if(!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null,400,"参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.attrId))) return res.sendResult(null,400,"参数ID必须是数字");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
attrServ.attributeById(req.params.attrId,function(err,attr){
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(attr,200,"获取成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID和参数ID是否存在以及是否为数字类型
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
if (!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null, 400, "参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.attrId))) return res.sendResult(null, 400, "参数ID必须是数字");
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用attrServ服务的attributeById方法,传入参数ID参数
|
|
|
|
|
// 处理获取参数详情的异步操作,成功则返回参数详情,失败则返回错误信息
|
|
|
|
|
attrServ.attributeById(req.params.attrId, function (err, attr) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(attr, 200, "获取成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 创建参数
|
|
|
|
|
// 处理创建分类参数的POST请求,路径为 "/:id/attributes"
|
|
|
|
|
router.post("/:id/attributes",
|
|
|
|
|
// 验证参数
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
|
|
|
|
|
if(!req.body.attr_name) return res.sendResult(null,400,"参数名称不能为空");
|
|
|
|
|
// 参数验证中间件,检查分类ID是否存在、是否为数字类型,参数名称是否为空,以及参数的attr_sel类型是否正确等
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
|
|
|
|
|
if(!req.body.attr_sel || (req.body.attr_sel != "only" && req.body.attr_sel != "many")) {
|
|
|
|
|
return res.sendResult(null,400,"参数 attr_sel 类型必须为 only 或 many");
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
if(!req.body.attr_write || (req.body.attr_write != "manual" && req.body.attr_write != "list")) {
|
|
|
|
|
return res.sendResult(null,400,"参数的 attr_write 必须为 manual 或 list");
|
|
|
|
|
}*/
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
attrServ.createAttribute(
|
|
|
|
|
{
|
|
|
|
|
"attr_name" : req.body.attr_name,
|
|
|
|
|
"cat_id" : req.params.id,
|
|
|
|
|
"attr_sel" : req.body.attr_sel,
|
|
|
|
|
"attr_write" : req.body.attr_sel == "many" ? "list" : "manual",//req.body.attr_write,
|
|
|
|
|
"attr_vals" : req.body.attr_vals ? req.body.attr_vals : ""
|
|
|
|
|
},
|
|
|
|
|
function(err,attr) {
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(attr,201,"创建成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (!req.body.attr_name) return res.sendResult(null, 400, "参数名称不能为空");
|
|
|
|
|
|
|
|
|
|
if (!req.body.attr_sel || (req.body.attr_sel!= "only" && req.body.attr_sel!= "many")) {
|
|
|
|
|
return res.sendResult(null, 400, "参数 attr_sel 类型必须为 only 或 many");
|
|
|
|
|
}
|
|
|
|
|
// 以下代码被注释掉了,原本可能是用于验证参数的attr_write字段是否符合要求
|
|
|
|
|
/*
|
|
|
|
|
if (!req.body.attr_write || (req.body.attr_write!= "manual" && req.body.attr_write!= "list")) {
|
|
|
|
|
return res.sendResult(null, 400, "参数的 attr_write 必须为 manual 或 list");
|
|
|
|
|
}*/
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于创建分类参数的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 根据参数的attr_sel类型来确定attr_write的值,如果attr_sel为"many"则attr_write设为"list",否则设为"manual"
|
|
|
|
|
const attr_write_value = req.body.attr_sel == "many"? "list" : "manual";
|
|
|
|
|
// 调用attrServ服务的createAttribute方法,传入包含参数相关信息(名称、所属分类ID、选择类型、写入类型、参数值等)的对象
|
|
|
|
|
// 处理创建分类参数的异步操作,成功则返回创建后的参数信息,失败则返回错误信息
|
|
|
|
|
attrServ.createAttribute(
|
|
|
|
|
{
|
|
|
|
|
"attr_name": req.body.attr_name,
|
|
|
|
|
"cat_id": req.params.id,
|
|
|
|
|
"attr_sel": req.body.attr_sel,
|
|
|
|
|
"attr_write": attr_write_value,
|
|
|
|
|
"attr_vals": req.body.attr_vals? req.body.attr_vals : ""
|
|
|
|
|
},
|
|
|
|
|
function (err, attr) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(attr, 201, "创建成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 更新参数
|
|
|
|
|
// 处理更新分类参数的PUT请求,路径为 "/:id/attributes/:attrId"
|
|
|
|
|
router.put("/:id/attributes/:attrId",
|
|
|
|
|
// 验证参数
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
if(!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null,400,"参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.attrId))) return res.sendResult(null,400,"参数ID必须是数字");
|
|
|
|
|
if(!req.body.attr_sel || (req.body.attr_sel != "only" && req.body.attr_sel != "many")) {
|
|
|
|
|
return res.sendResult(null,400,"参数 attr_sel 类型必须为 only 或 many");
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID、参数ID是否存在、是否为数字类型,参数的attr_sel类型是否正确,以及参数名称是否为空等
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
if (!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null, 400, "参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.attrId))) return res.sendResult(null, 400, "参数ID必须是数字");
|
|
|
|
|
if (!req.body.attr_sel || (req.body.attr_sel!= "only" && req.body.attr_sel!= "many")) {
|
|
|
|
|
return res.sendResult(null, 400, "参数 attr_sel 类型必须为 only 或 many");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!req.body.attr_name || req.body.attr_name == "") return res.sendResult(null,400,"参数名称不能为空");
|
|
|
|
|
if (!req.body.attr_name || req.body.attr_name == "") return res.sendResult(null, 400, "参数名称不能为空");
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
attrServ.updateAttribute(
|
|
|
|
|
req.params.attrId,
|
|
|
|
|
{
|
|
|
|
|
"attr_name" : req.body.attr_name,
|
|
|
|
|
"cat_id" : req.params.id,
|
|
|
|
|
"attr_sel" : req.body.attr_sel,
|
|
|
|
|
"attr_write" : req.body.attr_sel == "many" ? "list" : "manual",//req.body.attr_write,
|
|
|
|
|
"attr_vals" : req.body.attr_vals ? req.body.attr_vals : ""
|
|
|
|
|
},
|
|
|
|
|
function(err,newAttr) {
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(newAttr,200,"更新成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于更新分类参数的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 根据参数的attr_sel类型来确定attr_write的值,如果attr_sel为"many"则attr_write设为"list",否则设为"manual"
|
|
|
|
|
const attr_write_value = req.body.attr_sel == "many"? "list" : "manual";
|
|
|
|
|
// 调用attrServ服务的updateAttribute方法,传入参数ID以及包含更新后的参数相关信息(名称、所属分类ID、选择类型、写入类型、参数值等)的对象
|
|
|
|
|
// 处理更新分类参数的异步操作,成功则返回更新后的参数信息,失败则返回错误信息
|
|
|
|
|
attrServ.updateAttribute(
|
|
|
|
|
req.params.attrId,
|
|
|
|
|
{
|
|
|
|
|
"attr_name": req.body.attr_name,
|
|
|
|
|
"cat_id": req.params.id,
|
|
|
|
|
"attr_sel": req.body.attr_sel,
|
|
|
|
|
"attr_write": attr_write_value,
|
|
|
|
|
"attr_vals": req.body.attr_vals? req.body.attr_vals : ""
|
|
|
|
|
},
|
|
|
|
|
function (err, newAttr) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(newAttr, 200, "更新成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 删除参数
|
|
|
|
|
// 处理删除分类参数的DELETE请求,路径为 "/:id/attributes/:attrId"
|
|
|
|
|
router.delete("/:id/attributes/:attrId",
|
|
|
|
|
// 验证参数
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
if(!req.params.id) {
|
|
|
|
|
return res.sendResult(null,400,"分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.id))) return res.sendResult(null,400,"分类ID必须是数字");
|
|
|
|
|
if(!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null,400,"参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if(isNaN(parseInt(req.params.attrId))) return res.sendResult(null,400,"参数ID必须是数字");
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
function(req,res,next) {
|
|
|
|
|
attrServ.deleteAttribute(req.params.attrId,function(err,newAttr) {
|
|
|
|
|
if(err) return res.sendResult(null,400,err);
|
|
|
|
|
res.sendResult(null,200,"删除成功");
|
|
|
|
|
})(req,res,next);
|
|
|
|
|
}
|
|
|
|
|
// 参数验证中间件,检查分类ID和参数ID是否存在以及是否为数字类型
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
if (!req.params.id) {
|
|
|
|
|
return res.sendResult(null, 400, "分类ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "分类ID必须是数字");
|
|
|
|
|
if (!req.params.attrId) {
|
|
|
|
|
return res.sendResult(null, 400, "参数ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(parseInt(req.params.attrId))) return res.sendResult(null, 400, "参数ID必须是数字");
|
|
|
|
|
// 参数验证通过,将控制权交给下一个中间件
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// 业务逻辑中间件,用于执行删除分类参数的操作
|
|
|
|
|
function (req, res, next) {
|
|
|
|
|
// 调用attrServ服务的deleteAttribute方法,传入参数ID参数
|
|
|
|
|
// 处理删除分类参数的异步操作,成功则返回提示信息,失败则返回错误信息
|
|
|
|
|
attrServ.deleteAttribute(req.params.attrId, function (err, newAttr) {
|
|
|
|
|
if (err) return res.sendResult(null, 400, err);
|
|
|
|
|
res.sendResult(null, 200, "删除成功");
|
|
|
|
|
})(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 将配置好的路由器对象导出,以便在其他模块中使用
|
|
|
|
|
module.exports = router;
|