items.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const assert = require('assert');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. class ItemsService extends CrudService {
  6. constructor(ctx) {
  7. super(ctx, 'naf_code_items');
  8. this.mCategory = this.ctx.model.Category;
  9. this.mItems = this.ctx.model.Items;
  10. this.model = this.mItems;
  11. }
  12. async create({ category }, data) {
  13. assert(category);
  14. assert(data);
  15. // TODO:检查类别是否存在
  16. const c = await this.mCategory.findOne({ code: category }).exec();
  17. if (!c) {
  18. this.logger.error(`code category: ${category} not exist!`);
  19. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '类别信息不存在');
  20. }
  21. // TODO:保存数据
  22. const res = await this.mItems.create({ ...data, category });
  23. return res;
  24. }
  25. async delete({ category, code }) {
  26. assert(category, 'category不能为空');
  27. assert(code, 'code不能为空');
  28. await this.mItems.deleteOne({ category, code }).exec();
  29. return 'deleted';
  30. }
  31. }
  32. module.exports = ItemsService;