运维服务中台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

'use strict';
async function list (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const linkListRes = await models.QuickLink.findAll({
attributes: { exclude: ['userId'] },
where: {
userId,
}
})
ctx.status = 200;
ctx.body = linkListRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function edit (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const { linkId, name, link } = ctx.request.body
if (!name || !link) {
throw '请将参数填写完整'
}
let findOption = {
where: {
userId: userId,
$or: [{
name,
}, {
link,
}]
}
}
if (linkId) {
findOption.where.id = { $ne: linkId }
}
const existRes = await models.QuickLink.findOne({
where: {
userId: userId,
$or: [{
name,
}, {
link,
}]
}
})
if (existRes) {
throw '已有相同名称/地址的工具'
}
if (linkId) {
await models.QuickLink.update({
name,
link,
}, {
where: {
id: linkId
}
})
} else {
await models.QuickLink.create({
userId,
name,
link,
})
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function del (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const { linkId } = ctx.params
await models.QuickLink.destroy({
where: {
id: linkId,
userId,
}
})
ctx.status = 204;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
module.exports = {
list, edit, del,
};