'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const moment = require('moment'); // class StoreGoodsService extends CrudService { constructor(ctx) { super(ctx, 'storegoods'); this.model = this.ctx.model.User.StoreGoods; } async create({ goods }) { assert(goods, '缺少商品信息'); const customer = _.get(this.ctx, 'user._id'); assert(customer, '缺少用户信息'); let data = await this.model.findOne({ customer, goods }); if (data) { await this.model.deleteOne({ customer, goods }); return { msg: '取消收藏', result: false }; } data = await this.model.create({ customer, goods, time: moment().format('YYYY-MM-DD HH:mm:ss') }); return { msg: '收藏成功', result: true }; } // 检查是否收藏商品 async check({ goods, customer }) { const num = await this.model.count({ goods, customer }); return num > 0; } } module.exports = StoreGoodsService;