436140
我的controller和service还有model是一一对应关系的。比如我有个device控制器里面有个叫做getDevice的action,那么我也就有个device的service,里面也有个getDevice的方法,然后在这service个方法里面操作模型,有时候只有一行代码,请问这种情况是否还有封装的必要,还是说直接在控制器的action里面操作模型 controller 代码
async getDevice(ctx) {
const { id } = ctx.params;
const { company_id } = ctx.state.user;
let result = await this.service.device.getDevice(id, company_id);
if (!result) {
this.ctx.throw(404, 'device not found');
}
ctx.body = result;
}
service代码
async getDevice(id, company_id) {
return await this.ctx.model.Device.scope({ method: ['company', company_id]}).findById(id);
}