target.js
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-02-16 10:54:22
* @LastEditors: 前端 qianduan01@didihu.com.cn
* @LastEditTime: 2023-05-04 10:38:14
* @FilePath: \achievements-system\app\controller\Target.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
"use strict";
const Controller = require("egg").Controller;
const StatusCode = require("../utils/status-code");
const { Op } = require("sequelize");
class TargetController extends Controller {
// 查询项目列表
async index() {
const { ctx } = this;
let where = {};
try {
let { pageNum, pageSize, type } = ctx.query;
let start = pageSize * (pageNum - 1);
if (type) {
where = { type};
}
let res = await ctx.model.Target.findAndCountAll({
limit: parseInt(pageSize),
offset: parseInt(start),
where: where,
// order: [['createdAt', 'DESC']],
});
if (!res) return (ctx.body = StatusCode.ERROR_400("查询失败!"));
ctx.body = StatusCode.SUCCESS_200("查询成功!", res);
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 创建项目
async create() {
const { ctx } = this;
try {
let data = ctx.request.body;
let res = await ctx.model.Target.create(data);
if (!res) return (ctx.body = StatusCode.ERROR_400("添加失败!"));
ctx.body = StatusCode.SUCCESS_200("添加成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 编辑项目
async update() {
const { ctx } = this;
const params = ctx.request.body;
// 定义编辑员工接口的请求参数规则
const ider = await ctx.model.Target.findOne({ where: { id: params.id } });
if (!ider) {
StatusCode.ERROR_400("不存在该记录!");
return false;
}
try {
let res = await ctx.model.Target.update(params, {
where: { id: params.id },
});
if (!res) return (ctx.body = StatusCode.ERROR_400("更新失败!"));
ctx.body = StatusCode.SUCCESS_200("更新成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 删除项目
async delete() {
const { ctx } = this;
try {
let { id } = ctx.request.body;
let res = await ctx.model.Target.destroy({ where: { id: id } });
if (!res) return (ctx.body = StatusCode.ERROR_400("删除失败!"));
ctx.body = StatusCode.SUCCESS_200("删除成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 项目详情
async detail() {
const { ctx } = this;
try {
let { id } = ctx.request.body;
let res = await ctx.model.Target.findOne({ where: { id } });
if (!res) return (ctx.body = StatusCode.ERROR_400("不存在该记录!"));
ctx.body = StatusCode.SUCCESS_200("操作成功!", res);
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
}
module.exports = TargetController;