示例#1
0
        public async Task <ICommandResult> HandleAsync(JoinChannelCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "User could not join this channel", Errors));
            }
            GetUserByIdQueryResult user = await _userRepository.GetById(command.UserId);

            if (user is null)
            {
                AddNotification(nameof(command.UserId), "User not found");
                return(new CommandResult(false, "User not found", Errors));
            }
            GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.ChannelId);

            if (channel is null)
            {
                AddNotification(nameof(command.ChannelId), "Channel not found");
                return(new CommandResult(false, "Channel not found", Errors));
            }
            bool userAlreadyIsMemeber = await _channelRepository.UserIsMemberOfChannel(user.Id, channel.Id);

            if (userAlreadyIsMemeber)
            {
                AddNotification(nameof(command.ChannelId), "User already is member of this channel");
                return(new CommandResult(false, "User could not join this channel", Errors));
            }
            await _channelRepository.AddUserToChannel(user.Id, channel.Id, false);

            return(new CommandResult(true, "User successfully joined the channel", data: null));
        }
        public async Task <ICommandResult> HandleAsync(UpdateChannelNameCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Could not update channel name", Errors));
            }

            GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.Id);

            if (channel is null)
            {
                AddNotification(nameof(command.Id), "Channel not found");
                return(new CommandResult(false, "Could not update channel name", Errors));
            }
            if (command.AdministratorId != channel.AdministratorId)
            {
                AddNotification(nameof(command.AdministratorId), "AdministratorId does't match channel administrator id");
                return(new CommandResult(false, "User does't have permission to update channel name", Errors));
            }
            if (channel.Name != command.Name)
            {
                await _channelRepository.UpdateChannelName(command.Id, command.Name);
            }
            return(new CommandResult(true, "Name successfully updated", new ChannelOutput
            {
                Id = command.Id,
                Name = command.Name
            }));
        }
示例#3
0
        public async Task SendMessage(SendMessageViewModel message)
        {
            GetChannelByIdQueryResult channel = await _channelRepository.GetById(message.ChannelId);

            if (channel is null)
            {
                return;
            }
            GetUserByIdQueryResult user = await _userRepository.GetById(message.SenderId);

            if (user is null)
            {
                return;
            }
            var finalMessage = new Message
            {
                Content   = message.Content,
                Date      = message.Date,
                ChannelId = message.ChannelId,
                Sender    = new User
                {
                    Id       = user.Id,
                    Username = user.Username
                }
            };
            await Clients.Group(message.ChannelId.ToString()).SendAsync("ReceiveMessage", finalMessage);
        }
        public async Task <ICommandResult> HandleAsync(DeleteChannelCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Could not delete this channel", Errors));
            }
            GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.ChannelId);

            if (channel is null)
            {
                AddNotification(nameof(command.ChannelId), "Channel not found");
                return(new CommandResult(false, "Could not delete this channel", Errors));
            }
            if (channel.AdministratorId != command.AdministratorId)
            {
                AddNotification(nameof(command.AdministratorId), "AdministratorId does't match channel administrator id");
                return(new CommandResult(false, "User does't have permission to delete this channel", Errors));
            }
            await _channelRepository.DeleteChannel(channel.Id);

            return(new CommandResult(true, "Channel successfully deleted", null, null));
        }