| | | 1 | | using LOCKnet.Core.DataAbstractions; |
| | | 2 | | using Microsoft.Data.Sqlite; |
| | | 3 | | |
| | | 4 | | namespace LOCKnet.Data.Repositories; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// SQLite-Implementierung von <see cref="ISettingsRepository"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public class SettingsRepository : RepositoryBase, ISettingsRepository |
| | | 10 | | { |
| | | 11 | | /// <summary>Initialisiert eine neue Instanz von <see cref="SettingsRepository"/>.</summary> |
| | | 12 | | /// <param name="connectionString">Der vollständige SQLite-Connection-String.</param> |
| | 36 | 13 | | public SettingsRepository(string connectionString) : base(connectionString) { } |
| | | 14 | | |
| | | 15 | | /// <summary>Initialisiert eine neue Instanz von <see cref="SettingsRepository"/>.</summary> |
| | | 16 | | /// <param name="connectionFactory">Factory fuer Storage-spezifische SQLite-Verbindungen.</param> |
| | 51 | 17 | | public SettingsRepository(ISqliteConnectionFactory connectionFactory) : base(connectionFactory) { } |
| | | 18 | | |
| | | 19 | | #region ISettingsRepository |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | | 22 | | public void Set(string key, string value) |
| | 32 | 23 | | { |
| | 32 | 24 | | using var conn = GetConnection(); |
| | 32 | 25 | | using var cmd = conn.CreateCommand(); |
| | 32 | 26 | | cmd.CommandText = @" |
| | 32 | 27 | | INSERT INTO Settings (Key, Value) VALUES ($key, $value) |
| | 32 | 28 | | ON CONFLICT(Key) DO UPDATE SET Value = $value, UpdatedAt = CURRENT_TIMESTAMP;"; |
| | 32 | 29 | | cmd.Parameters.AddWithValue("$key", key); |
| | 32 | 30 | | cmd.Parameters.AddWithValue("$value", value); |
| | 32 | 31 | | cmd.ExecuteNonQuery(); |
| | 64 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public string? Get(string key) |
| | 6 | 36 | | { |
| | 6 | 37 | | using var conn = GetConnection(); |
| | 6 | 38 | | using var cmd = conn.CreateCommand(); |
| | 6 | 39 | | cmd.CommandText = "SELECT Value FROM Settings WHERE Key = $key"; |
| | 6 | 40 | | cmd.Parameters.AddWithValue("$key", key); |
| | 6 | 41 | | return cmd.ExecuteScalar() as string; |
| | 6 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | | 45 | | public IReadOnlyDictionary<string, string> GetAll() |
| | 5 | 46 | | { |
| | 5 | 47 | | var dict = new Dictionary<string, string>(); |
| | 5 | 48 | | using var conn = GetConnection(); |
| | 5 | 49 | | using var cmd = conn.CreateCommand(); |
| | 5 | 50 | | cmd.CommandText = "SELECT Key, Value FROM Settings"; |
| | | 51 | | |
| | 5 | 52 | | using var reader = cmd.ExecuteReader(); |
| | 12 | 53 | | while (reader.Read()) |
| | 7 | 54 | | dict[reader.GetString(0)] = reader.GetString(1); |
| | | 55 | | |
| | 5 | 56 | | return dict; |
| | 5 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc/> |
| | | 60 | | public void Remove(string key) |
| | 3 | 61 | | { |
| | 3 | 62 | | using var conn = GetConnection(); |
| | 3 | 63 | | using var cmd = conn.CreateCommand(); |
| | 3 | 64 | | cmd.CommandText = "DELETE FROM Settings WHERE Key = $key;"; |
| | 3 | 65 | | cmd.Parameters.AddWithValue("$key", key); |
| | 3 | 66 | | cmd.ExecuteNonQuery(); |
| | 6 | 67 | | } |
| | | 68 | | |
| | | 69 | | #endregion |
| | | 70 | | } |