| | | 1 | | using Dapper; |
| | | 2 | | using Npgsql; |
| | | 3 | | using Syki.Back.Hubs; |
| | | 4 | | |
| | | 5 | | namespace Syki.Back.Features.Adm.GetUsers; |
| | | 6 | | |
| | 2 | 7 | | public class GetUsersService(DatabaseSettings dbSettings, HybridCache cache) : IAdmService |
| | | 8 | | { |
| | | 9 | | public async Task<List<UserOut>> Get() |
| | | 10 | | { |
| | 2 | 11 | | using var connection = new NpgsqlConnection(dbSettings.ConnectionString); |
| | | 12 | | |
| | | 13 | | const string sql = @" |
| | | 14 | | SELECT |
| | | 15 | | u.id, |
| | | 16 | | u.name AS name, |
| | | 17 | | u.email, |
| | | 18 | | u.institution_id, |
| | | 19 | | i.name AS institution, |
| | | 20 | | STRING_AGG(r.name, ',') AS role |
| | | 21 | | FROM |
| | | 22 | | syki.users u |
| | | 23 | | INNER JOIN |
| | | 24 | | syki.institutions i ON i.id = u.institution_id |
| | | 25 | | INNER JOIN |
| | | 26 | | syki.user_roles ur ON ur.user_id = u.id |
| | | 27 | | INNER JOIN |
| | | 28 | | syki.roles r ON r.id = ur.role_id |
| | | 29 | | GROUP BY |
| | | 30 | | u.id, i.name |
| | | 31 | | "; |
| | | 32 | | |
| | 2 | 33 | | var users = await cache.GetOrCreateAsync( |
| | 2 | 34 | | key: "users", |
| | 2 | 35 | | state: (connection, sql), |
| | 2 | 36 | | factory: async (state, _) => |
| | 2 | 37 | | { |
| | 2 | 38 | | var data = await state.connection.QueryAsync<UserOut>(state.sql); |
| | 2 | 39 | | return data.ToList(); |
| | 2 | 40 | | } |
| | 2 | 41 | | ); |
| | | 42 | | |
| | 1680 | 43 | | foreach (var user in users) |
| | | 44 | | { |
| | 838 | 45 | | if (SykiHubUsersStore.Users.TryGetValue(user.Id, out var connections)) |
| | | 46 | | { |
| | 0 | 47 | | user.Online = true; |
| | 0 | 48 | | user.Connections = connections.Count; |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | return users; |
| | 2 | 53 | | } |
| | | 54 | | } |