storeGoods.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class StoreGoodsService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'storegoods');
  11. this.model = this.ctx.model.User.StoreGoods;
  12. }
  13. async create({ goods }) {
  14. assert(goods, '缺少商品信息');
  15. const customer = _.get(this.ctx, 'user._id');
  16. assert(customer, '缺少用户信息');
  17. let data = await this.model.findOne({ customer, goods });
  18. if (data) {
  19. await this.model.deleteOne({ customer, goods });
  20. return { msg: '取消收藏', result: false };
  21. }
  22. data = await this.model.create({ customer, goods, time: moment().format('YYYY-MM-DD HH:mm:ss') });
  23. return { msg: '收藏成功', result: true };
  24. }
  25. // 检查是否收藏商品
  26. async check({ goods, customer }) {
  27. const num = await this.model.count({ goods, customer });
  28. return num > 0;
  29. }
  30. }
  31. module.exports = StoreGoodsService;