Example #1
0
        public async Task Handle(DeletedPostDomainEvent notification, CancellationToken cancellationToken)
        {
            _logger.LogInformation("----- Handling DeletedPostDomainEvent: at {AppName} - ({@DomainEvent})", Program.AppName, notification);

            var relations = await _userCommentRelationRepository.GetRelationsByCommentIdsAsync(notification.CommentIds);

            relations.ForEach(r => _userCommentRelationRepository.Remove(r));
        }
        public async Task Handle(CommentDeletedDomainEvent notification, CancellationToken cancellationToken)
        {
            _logger.LogInformation("----- Handling CommentDeletedDomainEvent: at {AppName} - ({@DomainEvent})", Program.AppName, notification);

            // 删除“用户-平论”关系
            var relations = await _userCommentRelationRepository.GetRelationsByCommentIdsAsync(notification.CommentIds);

            relations.ForEach(r => _userCommentRelationRepository.Remove(r));

            // 更新帖子的评论数
            var post = await _postRepository.GetByIdAsync(notification.PostId);

            if (post != null)
            {
                post.DecreaseCommentCount(notification.CommentIds.Count, _scoreRewardSettings.CommentPost);
            }
        }
Example #3
0
        public async Task <bool> Handle(ToggleLikeCommentCommand request, CancellationToken cancellationToken)
        {
            bool result = false;

            var userId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);

            var comment = await _commentRepository.GetByIdAsync(request.CommentId);

            if (comment == null)
            {
                throw new ClientException("操作失败", new List <string> {
                    $"Comment {request.CommentId} does not exists."
                });
            }

            var userCommentRelation = await _userCommentRelationRepository.GetAsync(userId, request.CommentId);

            if (userCommentRelation == null)
            {
                userCommentRelation = new UserCommentRelation(userId, request.CommentId);
                userCommentRelation.Like(comment.PostId);
                _userCommentRelationRepository.Add(userCommentRelation);

                result = await _userCommentRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

                if (result)
                {
                    await SendCommentLikedEventAsync(userId, comment);
                }
            }
            else
            {
                userCommentRelation.UnLike(comment.PostId);
                _userCommentRelationRepository.Remove(userCommentRelation);

                result = await _userCommentRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

                if (result)
                {
                    await SendPostUnLikedEventAsync(comment.PostId);
                }
            }

            return(result);
        }