public async Task <bool> Handle(ToppingCircleCommand request, CancellationToken cancellationToken)
        {
            var myId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            // 需要置顶或取消置顶的圈子
            var relation = await _userCircleRelationRepository.GetRelationAsync(request.CircleId, myId);

            if (relation == null)
            {
                throw new ClientException("操作失败", new List <string> {
                    $"User {myId} is not in circle {request.CircleId}"
                });
            }

            if (request.Topping)
            {
                relation.ToppingCircle();
            }
            else
            {
                relation.UnToppingCircle();
            }

            return(await _userCircleRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken));
        }
Beispiel #2
0
        public async Task <bool> Handle(QuitCircleCommand request, CancellationToken cancellationToken)
        {
            var myId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userCircle = await _userCircleRelationRepository.GetRelationAsync(request.CircleId, myId);

            if (userCircle == null)
            {
                return(true);
            }

            var circle = await _circleRepository.GetByIdAsync(request.CircleId);

            if (circle.OwnerId == myId)
            {
                throw new ClientException("操作失败", new List <string> {
                    $"User {myId} is the owner of the circle {request.CircleId}, can't quit circle."
                });
            }

            userCircle.Quit();
            _userCircleRelationRepository.Remove(userCircle);

            return(await _userCircleRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken));
        }
        public async Task <bool> Handle(JoinCircleCommand request, CancellationToken cancellationToken)
        {
            var myId       = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var userCircle = await _userCircleRelationRepository.GetRelationAsync(request.CircleId, myId);

            if (userCircle == null)
            {
                var circle = await _circleRepository.GetByIdAsync(request.CircleId);

                if (circle == null)
                {
                    throw new ClientException("操作失败", new List <string> {
                        $"Circle {request.CircleId} dos not exist."
                    });
                }

                _messageSession = (IMessageSession)_serviceProvider.GetService(typeof(IMessageSession));

                if (circle.VerifyJoin)
                {
                    // 发送用户申请入圈事件
                    await SendAppliedJoinCircleEventAsync(myId, circle, request.Description);

                    return(true);
                }

                // 无需审核,直接入圈,创建user circle关系
                ////await _addCircleMemberCommandHandler.AddCircleMemberAsync(circle, myId, cancellationToken);
                //userCircle = new UserCircleRelation(myId, request.CircleId);
                //_userCircleRelationRepository.Add(userCircle);
                //if (await _userCircleRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken))
                //{
                //    await SendJoinedCircleEventAsync(myId, circle);
                //    return true;
                //}
                //添加并发送用户已入圈事件
                if (await AddCircleMemberCommandHandler.AddCircleMemberAsync(circle.Id, myId, _userCircleRelationRepository, cancellationToken))
                {
                    await AddCircleMemberCommandHandler.SendJoinedCircleEventAsync(circle, myId, _messageSession, _logger);

                    return(true);
                }

                // 保存失败
                throw new ApplicationException("操作失败");
            }

            throw new ClientException("操作失败", new List <string> {
                $"User {myId} is already in circle {request.CircleId}"
            });
        }
Beispiel #4
0
        /// <summary>
        /// 将用户加入圈子,使用静态方法是因为JoinCircleCommandHandler也需要加用户入圈
        /// </summary>
        /// <param name="circleId">圈子id</param>
        /// <param name="joinedUserId">要加入圈子的用户id</param>
        /// <param name="userCircleRelationRepository"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task <bool> AddCircleMemberAsync(Guid circleId, Guid joinedUserId,
                                                             IUserCircleRelationRepository userCircleRelationRepository, CancellationToken cancellationToken)
        {
            var userCircle = await userCircleRelationRepository.GetRelationAsync(circleId, joinedUserId);

            if (userCircle == null)
            {
                userCircle = new UserCircleRelation(joinedUserId, circleId);

                userCircle.Join();
                userCircleRelationRepository.Add(userCircle);

                return(await userCircleRelationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken));
            }

            throw new ClientException("用户已在圈中", new List <string> {
                $"User {joinedUserId} is already in circle {circleId}"
            });
        }