category.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const assert = require('assert');
  3. const { isNullOrUndefined } = require('naf-core').Util;
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. class CategoryService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'naf_code_category');
  9. this.mItems = this.ctx.model.Items;
  10. this.mCategory = this.ctx.model.Category;
  11. this.model = this.mCategory;
  12. }
  13. async delete({ code }) {
  14. assert(code, 'code不能为空');
  15. // TODO:检查数据是否存在
  16. const entity = await this.mCategory.findOne({ code }).exec();
  17. if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  18. // TODO: 检查是否包含字典项数据
  19. const count = await this.mItems.count({ category: entity.code }).exec();
  20. if (count > 0) {
  21. throw new BusinessError(ErrorCode.SERVICE_FAULT, '存在字典项数据,不能删除');
  22. }
  23. await this.mCategory.deleteOne({ _id: entity._id }).exec();
  24. return 'deleted';
  25. }
  26. async clear({ category }) {
  27. assert(category, 'category不能为空');
  28. // TODO:检查数据是否存在
  29. const entity = await this.mCategory.findOne({ code: category }).exec();
  30. if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  31. // TODO: 删除字典项数据
  32. await this.mItems.deleteMany({ category: entity.code }).exec();
  33. return 'cleared';
  34. }
  35. // async query({ skip, limit, order } = {}, data = {}) {
  36. // // const rs = await this._find(trimData(data), null, trimData({ skip, limit, sort: order && { [order]: 1 } }));
  37. // const rs = await this.model.find({}, null, {}).exec();
  38. // return rs;
  39. // }
  40. }
  41. module.exports = CategoryService;