'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // 阅读数加一 class ReadService extends CrudService { constructor(ctx) { super(ctx, 'read'); } async plus(model, id) { assert(model && id, '缺少数据'); // _.capitalize=>首字母大写 const data = await this.ctx.model[_.capitalize(model)].findById(id); if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要阅读的数据'); // 默认将read给0,别引起错误,数据不对可以讨论,找原因,但是程序中断了就不好了 data.read = (data.read || 0) + 1; await data.save(); } } module.exports = ReadService;