Skip to content

Commit

Permalink
Refactoring BotReader
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 16, 2024
1 parent c0bf0fb commit f5854e7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface IBotReader
Task<Bot?> Find(Guid id, DateTimeOffset now, CancellationToken token);

Task<string> GetToken(Guid botId, CancellationToken token);

Task<IReadOnlyCollection<string>> GetFeatures(Guid botId, CancellationToken token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public async Task<GetWidgetsResult> 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<string>());
dashboardSettings ?? DashboardSettings.CreateDefaultSettings(person.Id, query.BotId),
features);

return new GetWidgetsResult(widgets);
}
Expand Down
65 changes: 41 additions & 24 deletions src/Inc.TeamAssistant.Connector.DataAccess/BotReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public BotReader(IConnectionFactory connectionFactory)

public async Task<IReadOnlyCollection<Guid>> 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);

Expand All @@ -32,15 +34,17 @@ SELECT b.id AS id

public async Task<IReadOnlyCollection<BotDto>> GetBotsByUser(long userId, CancellationToken token)
{
var botsCommand = new CommandDefinition(@"
var botsCommand = new CommandDefinition(
"""
SELECT DISTINCT
b.id AS id,
b.name AS name,
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);
Expand All @@ -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)
Expand All @@ -94,7 +87,8 @@ FROM connector.features AS f

public async Task<Bot?> Find(Guid id, DateTimeOffset now, CancellationToken token)
{
var command = new CommandDefinition(@"
var command = new CommandDefinition(
"""
SELECT
b.id AS id,
b.name AS name,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -193,10 +188,12 @@ bcs.position AS position

public async Task<string> 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);
Expand All @@ -206,4 +203,24 @@ FROM connector.bots AS b
var botToken = await connection.QuerySingleAsync<string>(command);
return botToken;
}

public async Task<IReadOnlyCollection<string>> 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<string>(command);
return results.ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ public sealed record BotDto(
Guid Id,
string Name,
long OwnerId,
IReadOnlyCollection<string> Features,
IReadOnlyCollection<TeamDto> Teams);

0 comments on commit f5854e7

Please sign in to comment.