| | | 1 | | using Npgsql; |
| | | 2 | | |
| | | 3 | | namespace Anichron.Infrastructure.Configuration; |
| | | 4 | | |
| | | 5 | | public sealed class DatabaseConnectionSettings |
| | | 6 | | { |
| | | 7 | | public string? Host { get; set; } |
| | | 8 | | public string? Database { get; set; } |
| | | 9 | | public int Port { get; set; } |
| | | 10 | | public string? Username { get; set; } |
| | | 11 | | public string? Password { get; set; } |
| | | 12 | | |
| | | 13 | | public IReadOnlyList<string> MissingSettings() |
| | 23 | 14 | | { |
| | 23 | 15 | | List<string> missing = []; |
| | 23 | 16 | | if (Host is null) |
| | 5 | 17 | | missing.Add("Postgres Host missing"); |
| | 23 | 18 | | if (Database is null) |
| | 4 | 19 | | missing.Add("Postgres Database name missing"); |
| | 23 | 20 | | if (Username is null) |
| | 4 | 21 | | missing.Add(SecretMissing("USER")); |
| | 23 | 22 | | if (Password is null) |
| | 4 | 23 | | missing.Add(SecretMissing("PASSWORD")); |
| | 23 | 24 | | return missing; |
| | 23 | 25 | | } |
| | | 26 | | |
| | | 27 | | public string ToConnectionString() |
| | 8 | 28 | | => new NpgsqlConnectionStringBuilder |
| | 8 | 29 | | { |
| | 8 | 30 | | Host = Host, |
| | 8 | 31 | | Database = Database, |
| | 8 | 32 | | Port = Port, |
| | 8 | 33 | | Username = Username, |
| | 8 | 34 | | Password = Password, |
| | 8 | 35 | | Pooling = true, |
| | 8 | 36 | | }.ToString(); |
| | | 37 | | |
| | | 38 | | private static string SecretMissing(string configKey) |
| | 8 | 39 | | => $"Configuration value for {configKey} or secret file {configKey}_FILE not found."; |
| | | 40 | | } |