| | 1 | | using Dapper; |
| | 2 | | using Npgsql; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Adm.GetBatchesSummary; |
| | 5 | |
|
| 0 | 6 | | public class GetBatchesSummaryService(NpgsqlDataSource dataSource) : IAdmService |
| | 7 | | { |
| | 8 | | public async Task<GetBatchesSummaryOut> Get() |
| | 9 | | { |
| 0 | 10 | | await using var connection = await dataSource.OpenConnectionAsync(); |
| | 11 | |
|
| 0 | 12 | | var result = new GetBatchesSummaryOut(); |
| | 13 | |
|
| | 14 | | const string summarySql = @" |
| | 15 | | SELECT |
| | 16 | | count(1) AS total, |
| | 17 | | count(1) FILTER (WHERE status = 'Pending') AS pending, |
| | 18 | | count(1) FILTER (WHERE status = 'Processing') AS processing, |
| | 19 | | count(1) FILTER (WHERE status = 'Success') AS success, |
| | 20 | | count(1) FILTER (WHERE status = 'Error') AS error |
| | 21 | | FROM syki.command_batches |
| | 22 | | "; |
| | 23 | |
|
| | 24 | | const string typesSql = @" |
| | 25 | | SELECT type, count(1) AS total |
| | 26 | | FROM syki.command_batches |
| | 27 | | GROUP BY type |
| | 28 | | ORDER BY total DESC |
| | 29 | | "; |
| | 30 | |
|
| | 31 | | const string institutionsSql = @" |
| | 32 | | SELECT id, name |
| | 33 | | FROM syki.institutions |
| | 34 | | WHERE id <> '00000000-0000-0000-0000-000000000000' |
| | 35 | | ORDER BY name |
| | 36 | | "; |
| | 37 | |
|
| 0 | 38 | | result.Summary = await connection.QueryFirstAsync<BatchesSummaryOut>(summarySql); |
| 0 | 39 | | result.Types = (await connection.QueryAsync<BatchTypeCountOut>(typesSql)).ToList(); |
| 0 | 40 | | result.Institutions = (await connection.QueryAsync<TinyInstitutionOut>(institutionsSql)).ToList(); |
| | 41 | |
|
| 0 | 42 | | return result; |
| 0 | 43 | | } |
| | 44 | | } |