public async Task <ApplicationResult <long> > Handle(SendChannelMessageCommand request, CancellationToken cancellationToken)
        {
            var currentUserResult = _currentUserService.GetResultUserId();

            if (!currentUserResult.Succeeded)
            {
                return(ApplicationResult <long> .Fail(currentUserResult.Error));
            }

            var channel = await _dbContext.Channels.FindAsync(new object[] { request.ChannelId }, cancellationToken);

            var sender = await _dbContext.Users.FindAsync(new object[] { currentUserResult.Value }, cancellationToken);

            var interlocutorUserId = await _dbContext.Interlocutors
                                     .Where(i => i.ChannelId == channel.Id)
                                     .Where(i => i.UserId != currentUserResult.Value)
                                     .Select(i => i.UserId)
                                     .SingleAsync(cancellationToken);

            var message = new Message
            {
                Channel = channel,
                Sender  = sender,
                Content = request.Content
            };

            await _dbContext.Messages.AddAsync(message, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            await _connectedUsersService.SendAsync(interlocutorUserId, "ReceiveMessage", new
            {
                message = new MessageModel
                {
                    Id       = message.Id,
                    AuthorId = sender.Id,
                    Content  = message.Content,
                    Own      = false,
                    Seen     = message.Seen,
                    SentDate = message.CreatedDate
                },
                channelId = channel.Id
            });

            return(ApplicationResult <long> .Ok(message.Id));
        }
Esempio n. 2
0
        public async Task <ApplicationResult> Handle(UpdateAccountProfileCommand request, CancellationToken cancellationToken)
        {
            var currentUserResult = _currentUserService.GetResultUserId();

            if (currentUserResult.Succeeded == false)
            {
                return(ApplicationResult.Fail(currentUserResult.Error));
            }

            var user = await _dbContext.Users.FindAsync(currentUserResult.Value);

            user.Name   = PersonName.Create(request.FirstName, request.LastName);
            user.Gender = !string.IsNullOrEmpty(request.Gender) ? (Gender?)Enum.Parse <Gender>(request.Gender) : null;
            user.About  = request.About;

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(ApplicationResult.Ok());
        }
        public async Task <ApplicationResult> Handle(AddFriendCommand request, CancellationToken cancellationToken)
        {
            var currentUserResult = _currentUserService.GetResultUserId();

            if (currentUserResult.Succeeded == false)
            {
                return(ApplicationResult.Fail(currentUserResult.Error));
            }

            var requestedBy = await _dbContext.Users.FindAsync(new object[] { currentUserResult.Value }, cancellationToken);

            var requestedTo = await _dbContext.Users.FindAsync(new object[] { request.UserId }, cancellationToken);

            if (await FriendshipExistsAsync(requestedBy.Id, requestedTo.Id, cancellationToken))
            {
                return(ApplicationResult.Ok());
            }

            var friendship = new Friendship
            {
                RequestedBy = requestedBy,
                RequestedTo = requestedTo,
                Approved    = true // TODO
            };

            await _dbContext.Friendships.AddAsync(friendship, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            await _mediator.Publish(new FriendshipCreatedEvent
            {
                FriendshipId  = friendship.Id,
                RequestedById = requestedBy.Id,
                RequestedToId = requestedTo.Id
            }, cancellationToken);

            return(ApplicationResult.Ok());
        }
        public async Task <ApplicationResult <long> > Handle(CreateUserChannelCommand request, CancellationToken cancellationToken)
        {
            var currentUserResult = _currentUserService.GetResultUserId();

            if (!currentUserResult.Succeeded)
            {
                return(ApplicationResult <long> .Fail(currentUserResult.Error));
            }

            var user = await _dbContext.Users.FindAsync(new object[] { request.UserId }, cancellationToken);

            var currentUser = await _dbContext.Users.FindAsync(new object[] { currentUserResult.Value }, cancellationToken);

            var channel          = new Channel();
            var userInterlocutor = new Interlocutor
            {
                Channel = channel,
                User    = user
            };
            var currentUserInterlocutor = new Interlocutor
            {
                Channel = channel,
                User    = currentUser
            };

            await _dbContext.Channels.AddAsync(channel, cancellationToken);

            await _dbContext.Interlocutors.AddAsync(userInterlocutor, cancellationToken);

            await _dbContext.Interlocutors.AddAsync(currentUserInterlocutor, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            await _mediator.Publish(new ChannelCreatedEvent { ChannelId = channel.Id }, cancellationToken);

            return(ApplicationResult <long> .Ok(channel.Id));
        }