-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartisan.sh
More file actions
263 lines (220 loc) · 6.07 KB
/
artisan.sh
File metadata and controls
263 lines (220 loc) · 6.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
set -e
if [ ! ${1} ]; then
errmsg="${0##*/}: No argument"
logger -s ${errmsg}
echo "usage: ${0##*/} [apiName]"
exit 1
fi
apiName=${1}
folder=''
# 两个参数,则默认第一个为文件夹
if [ ${2} ]; then
folder=${1}'/'
apiName=${2}
fi
apiClass=`echo ${apiName} | awk '{print toupper(substr($0,0,1))substr($0,2,length($0))}'`
# controller
controllerFile=app/controller/${folder}${apiName}.js
touch ${controllerFile}
cat > ${controllerFile} << EOL
const Response = require('../../util/response')
const Service${apiClass} = require('../../services/${folder}${apiName}')
module.exports = {
/**
* @api {get} /api/${folder}v1/${apiName}s ${apiName}列表
* @apiGroup ${apiName}
* @apiPermission todo
* @apiVersion 1.0.0
* @apiParam {String} param 参数
* @apiSuccess {String} data 返回数据
* @apiSuccessExample {json} Visual Preview:
* {
"success": true,
"code": 200,
"message": "请求成功",
"data": [
{
"id": 1,
},
....
]
}
*/
index: async function (ctx) {
let data = await Service${apiClass}.list()
return Response.output(ctx, data)
},
/**
* @api {get} /api/${folder}v1/${apiName}s/:${apiName}Id ${apiName}详情
* @apiGroup ${apiName}
* @apiPermission todo
* @apiVersion 1.0.0
* @apiParam {String} param 参数
* @apiSuccess {String} data 返回数据
* @apiSuccessExample {json} Visual Preview:
* {
"success": true,
"code": 200,
"message": "请求成功",
"data": [
{
"id": 1,
},
....
]
}
*/
detail: async function (ctx) {
let data = await Service${apiClass}.detail(ctx.params.${apiName}Id)
return Response.output(ctx, data)
},
/**
* @api {post} /api/${folder}v1/${apiName}s 新增${apiName}
* @apiGroup ${apiName}
* @apiPermission todo
* @apiVersion 1.0.0
* @apiParam {String} param 参数
* @apiSuccess {String} data 返回数据
* @apiSuccessExample {json} Visual Preview:
* {
"success": true,
"code": 200,
"message": "请求成功",
"data": {
"id": "1",
}
}
*/
add: async function (ctx) {
let result = await Service${apiClass}.add(ctx.input)
return Response.output(ctx, result)
},
/**
* @api {put} /api/${folder}v1/${apiName}s/:${apiName}Id 编辑${apiName}
* @apiGroup ${apiName}
* @apiPermission todo
* @apiVersion 1.0.0
*
* @apiParam {String} param 参数
* @apiSuccess {String} data 返回数据
*
* @apiSuccessExample {json} Visual Preview:
* {
"success": true,
"code": 200,
"message": "请求成功",
"data": {
"name": "name",
}
}
*/
edit: async function (ctx) {
let result = await Service${apiClass}.edit(ctx.params.${apiName}Id, ctx.input)
return Response.output(ctx, result)
},
/**
* @api {delete} /api/${folder}v1/${apiName}s/:${apiName}Id 删除${apiName}
* @apiGroup ${apiName}
* @apiPermission todo
* @apiVersion 1.0.0
* @apiSuccessExample {json} Visual Preview:
* {
"success": true,
"code": 200,
"message": "请求成功",
"data": {}
}
*/
delete: async function (ctx) {
let data = {
status: -1
}
let result = await Service${apiClass}.edit(ctx.params.${apiName}Id, data)
return Response.output(ctx)
},
}
EOL
# services
servicesFile=app/services/${folder}${apiName}.js
touch ${servicesFile}
cat > ${servicesFile} << EOL
const Model${apiClass} = require('../../model/${folder}${apiName}')
const ApiError = require('../../util/api_error')
const Validate = require('request-validate')
const _ = require('underscore')
module.exports = {
list: async function (kid) {
let result = await Model${apiClass}.list(kid)
return result
},
detail: async function (id) {
let result = await Model${apiClass}.first(id)
if (_.isEmpty(result)) {
throw new ApiError('common.notExist', '${apiName}')
}
if (result.status == -1) {
throw new ApiError('common.notExist', '${apiName}')
}
return result
},
add: async function (data) {
Validate(data, {
name: 'required',
})
let insertData = {
name: data.name
}
let insertResult = await Model${apiClass}.add(insertData)
insertData.id = insertResult.insertId
return insertData
},
edit: async function (id, data) {
let detail = await this.detail(id)
let where = {
id: id
}
let updateData = {
name: _.has(data, 'name') ? data.name : detail.name,
status: _.has(data, 'status') ? data.status : detail.status
}
let editResult = await Model${apiClass}.edit(updateData, where)
return updateData
},
}
EOL
# model
modelFile=app/model/${folder}${apiName}.js
touch ${modelFile}
cat > ${modelFile} << EOL
const DB = require('../../libraries/db')
const ModelBase = require('../../model/base')
const table = 'mist_${apiName}'
module.exports = {
add: async function (data) {
let res = ModelBase.execInsert(table, data)
return await res;
},
list: async function (kid) {
let result = DB.readMysql.select(
'*'
)
.from(table)
.where('kid', kid)
.where('status', '!=', -1)
return await result
},
first: async function (id) {
let result = DB.readMysql.first(
'*'
)
.from(table)
.where('id', id)
return await result
},
edit: async function (data, where, notWhere = {}) {
let result = ModelBase.execUpdate(table, data, where, notWhere)
return await result
}
}
EOL