| | 1 | | using Dapper; |
| | 2 | | using Npgsql; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Adm.GetCommand; |
| | 5 | |
|
| 0 | 6 | | public class GetCommandService(NpgsqlDataSource dataSource) : IAdmService |
| | 7 | | { |
| | 8 | | public async Task<CommandOut> Get(Guid id) |
| | 9 | | { |
| 0 | 10 | | await using var connection = await dataSource.OpenConnectionAsync(); |
| | 11 | |
|
| | 12 | | const string sql = @" |
| | 13 | | SELECT * |
| | 14 | | FROM syki.commands |
| | 15 | | WHERE id = @Id |
| | 16 | | "; |
| 0 | 17 | | var command = await connection.QueryFirstOrDefaultAsync<CommandOut>(sql, new { id }) ?? new(); |
| 0 | 18 | | command.Description = command.Type.ToCommandDescription(); |
| | 19 | |
|
| | 20 | | const string sourceBatchSql = @" |
| | 21 | | SELECT id |
| | 22 | | FROM syki.command_batches |
| | 23 | | WHERE next_command_id = @Id |
| | 24 | | "; |
| 0 | 25 | | command.SourceBatchId = await connection.QueryFirstOrDefaultAsync<Guid?>(sourceBatchSql, new { id }); |
| | 26 | |
|
| | 27 | | const string retriesSql = @" |
| | 28 | | SELECT * |
| | 29 | | FROM syki.commands |
| | 30 | | WHERE original_id = @Id |
| | 31 | | "; |
| 0 | 32 | | command.Retries = (await connection.QueryAsync<CommandOut>(retriesSql, new { id })).ToList(); |
| 0 | 33 | | command.Retries.ForEach(x => x.Description = x.Type.ToCommandDescription()); |
| | 34 | |
|
| | 35 | | const string subcommandsSql = @" |
| | 36 | | SELECT * |
| | 37 | | FROM syki.commands |
| | 38 | | WHERE parent_id = @Id |
| | 39 | | "; |
| 0 | 40 | | command.Subcommands = (await connection.QueryAsync<CommandOut>(subcommandsSql, new { id })).ToList(); |
| 0 | 41 | | command.Subcommands.ForEach(x => x.Description = x.Type.ToCommandDescription()); |
| | 42 | |
|
| | 43 | | const string bacthesSql = @" |
| | 44 | | SELECT * |
| | 45 | | FROM syki.command_batches |
| | 46 | | WHERE source_command_id = @Id |
| | 47 | | "; |
| 0 | 48 | | command.Batches = (await connection.QueryAsync<BatchOut>(bacthesSql, new { id })).ToList(); |
| | 49 | |
|
| 0 | 50 | | return command; |
| 0 | 51 | | } |
| | 52 | | } |