contract.js
1.7 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
'use strict';
const qs = require('qs');
const Controller = require('egg').Controller;
const uuid = require('node-uuid');
class ContractController extends Controller {
async info() {
const ctx = this.ctx;
const id = ctx.params.id;
try {
const result = await ctx.service.contract.find(id);
this.ctx.body = {
result,
success: true,
};
} catch (error) {
this.ctx.body = {
success: false,
error,
};
}
}
async list() {
const param = qs.parse(this.ctx.query);
const ctx = this.ctx;
try {
const result = await ctx.service.contract.list(param);
const total = await ctx.service.contract.count();
this.ctx.body = {
result,
total: total[0].total,
success: true,
};
} catch (error) {
this.ctx.body = {
success: false,
error,
};
}
}
async add() {
const ctx = this.ctx;
const id = uuid.v4();
const data = ctx.request.body;
data.id = id;
try {
await ctx.service.contract.add(data);
this.ctx.body = { success: true };
} catch (error) {
this.ctx.body = { success: false, error };
}
}
async update() {
const ctx = this.ctx;
const data = ctx.request.body;
try {
await ctx.service.contract.update(data);
this.ctx.body = { success: true };
} catch (error) {
this.ctx.body = { success: false, error };
}
}
async remove() {
const ctx = this.ctx;
try {
await ctx.service.contract.remove(ctx.request.body);
this.ctx.body = { success: true };
} catch (error) {
this.ctx.body = { success: false, error };
}
}
}
module.exports = ContractController;