123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 点赞
- class ThumbsService extends CrudService {
- constructor(ctx) {
- super(ctx, 'thumbs');
- this.model = this.ctx.model.Thumbs;
- }
- async create(body) {
- // 有就取消点赞,没有就点赞
- const { openid, article_id } = body;
- const count = await this.model.count({ openid, article_id });
- let res = false;
- if (count > 0) await this.model.deleteOne({ openid, article_id });
- else { await this.model.create(body); res = true; }
- return res;
- }
- /**
- * 查询某人对某些数据是否点赞
- * @param {Object} { ids, openid }
- */
- async getSum({ ids, openid }) {
- const match = { article_id: { $in: ids.map(i => ObjectId(i)) } };
- if (openid) match.openid = openid;
- const res = await this.model.aggregate([
- { $match: match },
- { $group: {
- _id: '$article_id',
- sum: { $sum: 1 },
- } },
- ]);
- return res;
- }
- }
- module.exports = ThumbsService;
|