dflzm
Version:
x
159 lines (148 loc) • 2.66 kB
JavaScript
/**
* @brief select group
*
* @param group_id
*
* @return {} or []
*/
const selectGroup = async (ctx) => {
try {
const { group_id } = ctx.query
const attributes = ['group_id', 'path_id', 'comment', 'created_at', 'updated_at']
let rst
if (group_id) {
rst = await ctx.sql.auth_group
.findOne({
attributes,
where: {
group_id,
}
})
} else {
rst = await ctx.sql.auth_group
.findAll({
attributes
})
}
ctx.body = {
code: rst ? 0 : 1,
msg: (group_id && !rst) || (!group_id && !rst.length) ? '没有匹配的查询结果' : '',
data: rst ? rst : group_id ? {} : [],
}
} catch (err) {
ctx.logger.error(err)
ctx.body = {
code: 1,
msg: 'error',
data: {},
}
}
}
/**
* @brief create group
*
* @param path_id, comment
*
* @return {}
*/
const createGroup = async (ctx) => {
try {
const {
path_id,
comment,
} = ctx.request.body
console.log(ctx.sql)
const rst = await ctx.sql.auth_group
.create({
path_id,
comment,
})
ctx.body = {
code: 0,
msg: '',
data: rst,
}
} catch (err) {
ctx.logger.error(err)
ctx.body = {
code: 1,
msg: 'error',
data: {},
}
}
}
/**
* @brief update group
*
* @param group_id, group, comment
*
* @return {}
*/
const updateGroup = async (ctx) => {
try {
const {
group_id,
path_id,
comment,
} = ctx.request.body
const rst = await ctx.sql.auth_group
.update({
path_id,
comment,
}, {
where: {
group_id
}
})
ctx.body = {
code: rst[0] ? 0 : 1,
msg: rst[0] ? '' : '没有匹配的数据项',
data: rst,
}
} catch (err) {
ctx.logger.error(err)
ctx.body = {
code: 1,
msg: 'error',
data: {},
}
}
}
/**
* @brief delete group
*
* @param group_id
*
* @return {}
*/
const deleteGroup = async (ctx) => {
try {
const {
group_id,
} = ctx.request.body
const rst = await ctx.sql.auth_group
.destroy({
where: {
group_id
}
})
ctx.body = {
code: rst ? 0 : 1,
msg: rst ? '' : '没有查询到匹配的 项',
data: rst,
}
} catch (err) {
ctx.logger.error(err)
ctx.body = {
code: 1,
msg: 'error',
data: {},
}
}
}
module.exports = {
'GET /auth_group': selectGroup,
'PUT /auth_group': updateGroup,
'POST /auth_group': createGroup,
'DELETE /auth_group': deleteGroup,
}