From f5854e77880e0742fa0bd8da46d59bafdc83173c Mon Sep 17 00:00:00 2001 From: dyatlov-a Date: Mon, 16 Sep 2024 09:50:24 +0400 Subject: [PATCH] Refactoring BotReader --- .../Contracts/IBotReader.cs | 2 + .../GetWidgets/GetWidgetsQueryHandler.cs | 6 +- .../BotReader.cs | 65 ++++++++++++------- .../Queries/GetBotsByCurrentUser/BotDto.cs | 1 - 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/Inc.TeamAssistant.Connector.Application/Contracts/IBotReader.cs b/src/Inc.TeamAssistant.Connector.Application/Contracts/IBotReader.cs index 6e8f660a..f92a9c28 100644 --- a/src/Inc.TeamAssistant.Connector.Application/Contracts/IBotReader.cs +++ b/src/Inc.TeamAssistant.Connector.Application/Contracts/IBotReader.cs @@ -12,4 +12,6 @@ public interface IBotReader Task Find(Guid id, DateTimeOffset now, CancellationToken token); Task GetToken(Guid botId, CancellationToken token); + + Task> GetFeatures(Guid botId, CancellationToken token); } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.Connector.Application/QueryHandlers/GetWidgets/GetWidgetsQueryHandler.cs b/src/Inc.TeamAssistant.Connector.Application/QueryHandlers/GetWidgets/GetWidgetsQueryHandler.cs index 9679e5f9..e55e6abd 100644 --- a/src/Inc.TeamAssistant.Connector.Application/QueryHandlers/GetWidgets/GetWidgetsQueryHandler.cs +++ b/src/Inc.TeamAssistant.Connector.Application/QueryHandlers/GetWidgets/GetWidgetsQueryHandler.cs @@ -28,12 +28,12 @@ public async Task Handle(GetWidgetsQuery query, CancellationTo ArgumentNullException.ThrowIfNull(query); var person = _personResolver.GetCurrentPerson(); - var bots = await _botReader.GetBotsByUser(person.Id, token); + var features = await _botReader.GetFeatures(query.BotId, token); var dashboardSettings = await _repository.Find(person.Id, query.BotId, token); var widgets = DashboardSettingsConverter.Convert( - dashboardSettings?? DashboardSettings.CreateDefaultSettings(person.Id, query.BotId), - bots.SingleOrDefault(b => b.Id == query.BotId)?.Features ?? Array.Empty()); + dashboardSettings ?? DashboardSettings.CreateDefaultSettings(person.Id, query.BotId), + features); return new GetWidgetsResult(widgets); } diff --git a/src/Inc.TeamAssistant.Connector.DataAccess/BotReader.cs b/src/Inc.TeamAssistant.Connector.DataAccess/BotReader.cs index 20bd4996..2cd164ed 100644 --- a/src/Inc.TeamAssistant.Connector.DataAccess/BotReader.cs +++ b/src/Inc.TeamAssistant.Connector.DataAccess/BotReader.cs @@ -18,9 +18,11 @@ public BotReader(IConnectionFactory connectionFactory) public async Task> GetBotIds(CancellationToken token) { - var command = new CommandDefinition(@" + var command = new CommandDefinition( + """ SELECT b.id AS id - FROM connector.bots AS b;", + FROM connector.bots AS b; + """, flags: CommandFlags.None, cancellationToken: token); @@ -32,7 +34,8 @@ SELECT b.id AS id public async Task> GetBotsByUser(long userId, CancellationToken token) { - var botsCommand = new CommandDefinition(@" + var botsCommand = new CommandDefinition( + """ SELECT DISTINCT b.id AS id, b.name AS name, @@ -40,7 +43,8 @@ b.owner_id AS ownerid FROM connector.bots AS b LEFT JOIN connector.teams AS t ON t.bot_id = b.id LEFT JOIN connector.teammates AS tm ON t.id = tm.team_id - WHERE t.owner_id = @user_id OR tm.person_id = @user_id OR b.owner_id = @user_id;", + WHERE t.owner_id = @user_id OR tm.person_id = @user_id OR b.owner_id = @user_id; + """, new { user_id = userId }, flags: CommandFlags.None, cancellationToken: token); @@ -50,38 +54,27 @@ FROM connector.bots AS b var bots = (await connection.QueryAsync<(Guid BotId, string Name, long OwnerId)>(botsCommand)).ToArray(); var botIds = bots.Select(b => b.BotId).ToArray(); - var dataCommand = new CommandDefinition(@" + var dataCommand = new CommandDefinition( + """ SELECT t.id AS id, t.bot_id AS botid, t.name AS name FROM connector.teams AS t WHERE t.bot_id = ANY(@bot_ids); - - SELECT - af.bot_id AS botid, - f.name AS featurename - FROM connector.features AS f - JOIN connector.activated_features AS af ON f.id = af.feature_id - WHERE af.bot_id = ANY(@bot_ids);", + """, new { bot_ids = botIds }, flags: CommandFlags.None, cancellationToken: token); - await using var query = await connection.QueryMultipleAsync(dataCommand); - - var teams = (await query.ReadAsync<(Guid Id, Guid BotId, string Name)>()) + var teams = (await connection.QueryAsync<(Guid Id, Guid BotId, string Name)>(dataCommand)) .ToLookup(t => t.BotId); - var features = (await query.ReadAsync<(Guid BotId, string FeatureName)>()) - .ToLookup(f => f.BotId, f => f.FeatureName); + var results = bots .Select(b => new BotDto( b.BotId, b.Name, b.OwnerId, - features[b.BotId] - .OrderBy(f => f) - .ToArray(), teams[b.BotId] .Select(t => new TeamDto(t.Id, t.Name)) .OrderBy(t => t.Name) @@ -94,7 +87,8 @@ FROM connector.features AS f public async Task Find(Guid id, DateTimeOffset now, CancellationToken token) { - var command = new CommandDefinition(@" + var command = new CommandDefinition( + """ SELECT b.id AS id, b.name AS name, @@ -145,7 +139,8 @@ bc.scopes AS scopes bcs.value AS value, bcs.dialog_message_id AS dialogmessageid, bcs.position AS position - FROM connector.bot_command_stages AS bcs;", + FROM connector.bot_command_stages AS bcs; + """, new { id, @@ -193,10 +188,12 @@ bcs.position AS position public async Task GetToken(Guid botId, CancellationToken token) { - var command = new CommandDefinition(@" + var command = new CommandDefinition( + """ SELECT b.token AS token FROM connector.bots AS b - WHERE b.id = @bot_id;", + WHERE b.id = @bot_id; + """, new { bot_id = botId }, flags: CommandFlags.None, cancellationToken: token); @@ -206,4 +203,24 @@ FROM connector.bots AS b var botToken = await connection.QuerySingleAsync(command); return botToken; } + + public async Task> GetFeatures(Guid botId, CancellationToken token) + { + var command = new CommandDefinition( + """ + SELECT + f.name AS featurename + FROM connector.features AS f + JOIN connector.activated_features AS af ON f.id = af.feature_id + WHERE af.bot_id = @bot_id; + """, + new { bot_id = botId }, + flags: CommandFlags.None, + cancellationToken: token); + + await using var connection = _connectionFactory.Create(); + + var results = await connection.QueryAsync(command); + return results.ToArray(); + } } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.Connector.Model/Queries/GetBotsByCurrentUser/BotDto.cs b/src/Inc.TeamAssistant.Connector.Model/Queries/GetBotsByCurrentUser/BotDto.cs index f6b60a8c..78e3084a 100644 --- a/src/Inc.TeamAssistant.Connector.Model/Queries/GetBotsByCurrentUser/BotDto.cs +++ b/src/Inc.TeamAssistant.Connector.Model/Queries/GetBotsByCurrentUser/BotDto.cs @@ -4,5 +4,4 @@ public sealed record BotDto( Guid Id, string Name, long OwnerId, - IReadOnlyCollection Features, IReadOnlyCollection Teams); \ No newline at end of file