コード例 #1
0
            public async Task <IActionResult> Handle(GetUserRequest request, CancellationToken cancellationToken)
            {
                var userId = _currentUserAccessor.GetUserId();
                var user   = await _authDbContext.Users.FindAsync(userId);

                var response = _mapper.Map <UserResponse>(user);

                return(new OkObjectResult(response));
            }
コード例 #2
0
            public async Task <IActionResult> Handle(GetChannelListRequest request, CancellationToken cancellationToken)
            {
                var userId   = _currentUserAccessor.GetUserId();
                var channels = await _chatChannelRepository.FindByPredicateAsync(x => x.ParticipantUserIds.Contains(userId));

                var response = _mapper.Map <IEnumerable <ChatChannelResponse> >(channels);

                return(new OkObjectResult(response));
            }
コード例 #3
0
            public async Task <IActionResult> Handle(GetMessageListRequest request, CancellationToken cancellationToken)
            {
                var query = new ListChatMessagesQuery
                {
                    ChannelId = request.ChannelId,
                    UserId    = _userAccessor.GetUserId()
                };

                var result = await _mediator.Send(query, cancellationToken);

                return(new OkObjectResult(result));
            }
            public async Task <IActionResult> Handle(DeleteCurrentParticipantFromChannelRequest request, CancellationToken cancellationToken)
            {
                var userId = _currentUserAccessor.GetUserId();

                var channel = await _chatChannelEntityRepository.FindAsync(request.ChannelId);

                if (!channel.ParticipantUserIds.Contains(userId))
                {
                    return(new UnauthorizedResult());
                }

                channel.ParticipantUserIds.Remove(userId);
                await _chatChannelEntityRepository.UpdateAsync(channel);

                return(new OkResult());
            }
コード例 #5
0
            public async Task <IActionResult> Handle(PostChannelRequest request, CancellationToken cancellationToken)
            {
                var userId = _currentUserAccessor.GetUserId();

                request.Participants.Add(userId);

                var channel = new ChatChannel
                {
                    Title = request.Title,
                    ParticipantUserIds = request.Participants,
                    CreatorUserId      = userId
                };

                // TODO: Check whether all participants even exist in DB

                await _channelEntityRepository.AddAsync(channel);

                return(new OkResult());
            }
コード例 #6
0
        public async Task SendAsync(string channelId, string message)
        {
            var userId = _currentUserAccessor.GetUserId();

            Ensure.That(userId).IsNotNullOrEmpty();

            var channel = await GetChannelAsync(channelId);

            Ensure.That(channel).IsNotNull();

            var chatMessage = new ChatMessage
            {
                ChannelId = channelId,
                Message   = message,
                UserId    = userId
            };

            await _chatMessageEntityRepository.AddAsync(chatMessage);

            await _hubContext.Clients.Users(channel.ParticipantUserIds.ToList())
            .ReceiveMessage(chatMessage);
        }