|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Controller = require('egg').Controller; |
| 4 | +const _ = require('lodash'); |
| 5 | + |
| 6 | +const MongoObjectIdSchema = { |
| 7 | + type: 'string', |
| 8 | + max: 24, |
| 9 | + min: 24, |
| 10 | +}; |
| 11 | + |
| 12 | +class CollectController extends Controller { |
| 13 | + async index() { |
| 14 | + const { ctx, service } = this; |
| 15 | + const name = ctx.params.name; |
| 16 | + |
| 17 | + const user = await service.user.getUserByLoginName(name); |
| 18 | + |
| 19 | + if (!user) { |
| 20 | + ctx.status = 404; |
| 21 | + ctx.body = { |
| 22 | + success: false, |
| 23 | + error_msg: '用户不存在', |
| 24 | + }; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const opt = { skip: 0, limit: 100 }; |
| 29 | + |
| 30 | + const collects = await service.topicCollect.getTopicCollectsByUserId(user._id, opt); |
| 31 | + const ids = collects.map(doc => { |
| 32 | + return doc.topic_id.toString(); |
| 33 | + }); |
| 34 | + |
| 35 | + const query = { _id: { $in: ids } }; |
| 36 | + let topics = await service.topic.getTopicsByQuery(query, {}); |
| 37 | + |
| 38 | + topics = _.sortBy(topics, topic => { |
| 39 | + return ids.indexOf(topic._id.toString()); |
| 40 | + }); |
| 41 | + |
| 42 | + topics = topics.map(topic => { |
| 43 | + topic.author = _.pick(topic.author, [ 'loginname', 'avatar_url' ]); |
| 44 | + return _.pick(topic, [ 'id', 'author_id', 'tab', 'content', 'title', 'last_reply_at', |
| 45 | + 'good', 'top', 'reply_count', 'visit_count', 'create_at', 'author' ]); |
| 46 | + }); |
| 47 | + |
| 48 | + ctx.body = { |
| 49 | + success: true, |
| 50 | + data: topics, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + async collect() { |
| 55 | + const { ctx, service } = this; |
| 56 | + const topic_id = ctx.request.body.topic_id; |
| 57 | + const user_id = ctx.request.user.id; |
| 58 | + |
| 59 | + ctx.validate({ |
| 60 | + topic_id: MongoObjectIdSchema, |
| 61 | + }, ctx.request.body); |
| 62 | + |
| 63 | + const topic = await service.topic.getTopic(topic_id); |
| 64 | + |
| 65 | + if (!topic) { |
| 66 | + ctx.status = 404; |
| 67 | + ctx.body = { |
| 68 | + success: false, |
| 69 | + error_msg: '话题不存在', |
| 70 | + }; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const doc = await service.topicCollect.getTopicCollect( |
| 75 | + user_id, |
| 76 | + topic._id |
| 77 | + ); |
| 78 | + |
| 79 | + if (doc) { |
| 80 | + ctx.body = { |
| 81 | + success: false, |
| 82 | + error_msg: '已经收藏过该主题', |
| 83 | + }; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + await service.topicCollect.newAndSave(user_id, topic._id); |
| 88 | + await Promise.all([ |
| 89 | + service.user.incrementCollectTopicCount(user_id), |
| 90 | + service.topic.incrementCollectCount(topic_id), |
| 91 | + ]); |
| 92 | + |
| 93 | + ctx.body = { success: true }; |
| 94 | + } |
| 95 | + |
| 96 | + async de_collect() { |
| 97 | + const { ctx, service } = this; |
| 98 | + const topic_id = ctx.request.body.topic_id; |
| 99 | + const user_id = ctx.request.user.id; |
| 100 | + |
| 101 | + ctx.validate({ |
| 102 | + topic_id: MongoObjectIdSchema, |
| 103 | + }, ctx.request.body); |
| 104 | + |
| 105 | + const topic = await service.topic.getTopic(topic_id); |
| 106 | + |
| 107 | + if (!topic) { |
| 108 | + ctx.status = 404; |
| 109 | + ctx.body = { |
| 110 | + success: false, |
| 111 | + error_msg: '话题不存在', |
| 112 | + }; |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const removeResult = await service.topicCollect.remove( |
| 117 | + user_id, |
| 118 | + topic._id |
| 119 | + ); |
| 120 | + |
| 121 | + if (removeResult.result.n === 0) { |
| 122 | + ctx.status = 500; |
| 123 | + ctx.body = { |
| 124 | + success: false, |
| 125 | + error_msg: '取消收藏失败', |
| 126 | + }; |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const user = await service.user.getUserById(user_id); |
| 131 | + |
| 132 | + user.collect_topic_count -= 1; |
| 133 | + await user.save(); |
| 134 | + |
| 135 | + topic.collect_count -= 1; |
| 136 | + await topic.save(); |
| 137 | + |
| 138 | + ctx.body = { success: true }; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = CollectController; |
0 commit comments