| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.JSInterop; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace Pomodoro.Web.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Service for IndexedDB operations via JavaScript interop |
| | | 9 | | /// Provides persistent storage with larger capacity than localStorage |
| | | 10 | | /// </summary> |
| | | 11 | | public class IndexedDbService : IIndexedDbService, IAsyncDisposable |
| | | 12 | | { |
| | | 13 | | private readonly IJSRuntime _jsRuntime; |
| | | 14 | | private readonly JsonSerializerOptions _jsonOptions; |
| | | 15 | | private readonly ILogger<IndexedDbService> _logger; |
| | | 16 | | private bool _isInitialized; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Event raised when a storage error occurs |
| | | 20 | | /// </summary> |
| | | 21 | | public event Action<string>? OnStorageError; |
| | | 22 | | |
| | 383 | 23 | | public IndexedDbService(IJSRuntime jsRuntime, ILogger<IndexedDbService> logger) |
| | 383 | 24 | | { |
| | 383 | 25 | | _jsRuntime = jsRuntime; |
| | 383 | 26 | | _logger = logger; |
| | 383 | 27 | | _jsonOptions = new JsonSerializerOptions |
| | 383 | 28 | | { |
| | 383 | 29 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 383 | 30 | | PropertyNameCaseInsensitive = true |
| | 383 | 31 | | }; |
| | 383 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task InitializeAsync() |
| | 331 | 35 | | { |
| | 343 | 36 | | if (_isInitialized) return; |
| | | 37 | | |
| | | 38 | | try |
| | 319 | 39 | | { |
| | | 40 | | // The indexedDbInterop.js is loaded via script tag in index.html, so we use _jsRuntime directly |
| | | 41 | | // rather than importing as ES module (the JS uses window.indexedDbInterop pattern) |
| | 319 | 42 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.InitDatabase); |
| | 307 | 43 | | _isInitialized = true; |
| | 307 | 44 | | _logger.LogDebug(Constants.Messages.LogInitializedSuccessfully); |
| | 307 | 45 | | } |
| | 12 | 46 | | catch (Exception ex) |
| | 12 | 47 | | { |
| | 12 | 48 | | _logger.LogError(ex, Constants.Messages.LogFailedToInitialize); |
| | 12 | 49 | | throw; |
| | | 50 | | } |
| | 319 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Initializes JavaScript constants with user settings for consistency across JS interop |
| | | 55 | | /// </summary> |
| | | 56 | | public async Task InitializeJsConstantsAsync(int pomodoroMinutes, int shortBreakMinutes, int longBreakMinutes) |
| | 24 | 57 | | { |
| | | 58 | | try |
| | 24 | 59 | | { |
| | 24 | 60 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.PomodoroConstantsInitialize, new |
| | 24 | 61 | | { |
| | 24 | 62 | | pomodoroMinutes, |
| | 24 | 63 | | shortBreakMinutes, |
| | 24 | 64 | | longBreakMinutes |
| | 24 | 65 | | }); |
| | 12 | 66 | | _logger.LogDebug(Constants.Messages.LogJsConstantsInitializedFormat, |
| | 12 | 67 | | pomodoroMinutes, shortBreakMinutes, longBreakMinutes); |
| | 12 | 68 | | } |
| | 12 | 69 | | catch (Exception ex) |
| | 12 | 70 | | { |
| | 12 | 71 | | _logger.LogWarning(ex, Constants.Messages.LogFailedToInitializeJsConstants); |
| | 12 | 72 | | } |
| | 24 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<T?> GetAsync<T>(string storeName, string key) |
| | 61 | 76 | | { |
| | 61 | 77 | | EnsureInitialized(); |
| | | 78 | | |
| | | 79 | | try |
| | 49 | 80 | | { |
| | 49 | 81 | | var result = await _jsRuntime.InvokeAsync<JsonElement>(Constants.IndexedDbJsFunctions.GetItem, storeName, ke |
| | 37 | 82 | | if (result.ValueKind == JsonValueKind.Null || result.ValueKind == JsonValueKind.Undefined) |
| | 12 | 83 | | return default; |
| | | 84 | | |
| | 25 | 85 | | return DeserializeItem<T>(result, storeName, key); |
| | | 86 | | } |
| | 12 | 87 | | catch (Exception ex) |
| | 12 | 88 | | { |
| | | 89 | | // Log error for debugging - storage failures should be investigated |
| | 12 | 90 | | _logger.LogError(ex, Constants.Messages.LogErrorGettingItem, storeName, key); |
| | | 91 | | // Notify subscribers of the error |
| | 12 | 92 | | NotifyStorageError(string.Format(Constants.Messages.LogFailedToGetItem, storeName, ex.Message)); |
| | 12 | 93 | | return default; |
| | | 94 | | } |
| | 49 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<List<T>> GetAllAsync<T>(string storeName) |
| | 32 | 98 | | { |
| | 32 | 99 | | EnsureInitialized(); |
| | | 100 | | |
| | | 101 | | try |
| | 31 | 102 | | { |
| | 31 | 103 | | var result = await _jsRuntime.InvokeAsync<JsonElement>(Constants.IndexedDbJsFunctions.GetAllItems, storeName |
| | 18 | 104 | | if (result.ValueKind == JsonValueKind.Null || result.ValueKind == JsonValueKind.Undefined) |
| | 13 | 105 | | return new List<T>(); |
| | | 106 | | |
| | 5 | 107 | | return DeserializeList<T>(result, storeName, "all"); |
| | | 108 | | } |
| | 13 | 109 | | catch (Exception ex) |
| | 13 | 110 | | { |
| | | 111 | | // Log error for debugging - storage failures should be investigated |
| | 13 | 112 | | _logger.LogError(ex, Constants.Messages.LogErrorGettingAllItems, storeName); |
| | | 113 | | // Notify subscribers of the error |
| | 13 | 114 | | NotifyStorageError(string.Format(Constants.Messages.LogFailedToGetAllItems, storeName, ex.Message)); |
| | 13 | 115 | | return new List<T>(); |
| | | 116 | | } |
| | 31 | 117 | | } |
| | | 118 | | |
| | | 119 | | public async Task<List<T>> QueryByIndexAsync<T>(string storeName, string indexName, object value) |
| | 38 | 120 | | { |
| | 38 | 121 | | EnsureInitialized(); |
| | | 122 | | |
| | | 123 | | try |
| | 38 | 124 | | { |
| | 38 | 125 | | var result = await _jsRuntime.InvokeAsync<JsonElement>(Constants.IndexedDbJsFunctions.GetItemsByIndex, store |
| | 26 | 126 | | if (result.ValueKind == JsonValueKind.Null || result.ValueKind == JsonValueKind.Undefined) |
| | 13 | 127 | | return new List<T>(); |
| | | 128 | | |
| | 13 | 129 | | return DeserializeList<T>(result, storeName, indexName); |
| | | 130 | | } |
| | 12 | 131 | | catch (Exception ex) |
| | 12 | 132 | | { |
| | 12 | 133 | | _logger.LogDebug(ex, Constants.Messages.LogErrorQueryingIndex, indexName); |
| | 12 | 134 | | return new List<T>(); |
| | | 135 | | } |
| | 38 | 136 | | } |
| | | 137 | | |
| | | 138 | | public async Task<List<T>> QueryByDateRangeAsync<T>(string storeName, string indexName, string startDate, string end |
| | 39 | 139 | | { |
| | 39 | 140 | | EnsureInitialized(); |
| | | 141 | | |
| | | 142 | | try |
| | 39 | 143 | | { |
| | 39 | 144 | | var result = await _jsRuntime.InvokeAsync<JsonElement>(Constants.IndexedDbJsFunctions.GetItemsByDateRange, s |
| | 26 | 145 | | if (result.ValueKind == JsonValueKind.Null || result.ValueKind == JsonValueKind.Undefined) |
| | 13 | 146 | | return new List<T>(); |
| | | 147 | | |
| | 13 | 148 | | return DeserializeList<T>(result, storeName, indexName); |
| | | 149 | | } |
| | 13 | 150 | | catch (Exception ex) |
| | 13 | 151 | | { |
| | 13 | 152 | | _logger.LogDebug(ex, Constants.Messages.LogErrorQueryingDateRange); |
| | 13 | 153 | | return new List<T>(); |
| | | 154 | | } |
| | 39 | 155 | | } |
| | | 156 | | |
| | | 157 | | public async Task<bool> PutAsync<T>(string storeName, T item) |
| | 24 | 158 | | { |
| | 24 | 159 | | EnsureInitialized(); |
| | | 160 | | |
| | | 161 | | try |
| | 24 | 162 | | { |
| | 24 | 163 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.PutItem, storeName, item); |
| | 12 | 164 | | return true; |
| | | 165 | | } |
| | 12 | 166 | | catch (Exception ex) |
| | 12 | 167 | | { |
| | 12 | 168 | | _logger.LogError(ex, Constants.Messages.LogErrorPuttingItem, storeName); |
| | 12 | 169 | | return false; |
| | | 170 | | } |
| | 24 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Puts all items in a single transaction for better performance. |
| | | 175 | | /// Note: Uses a single transaction for atomicity - if one item fails, all changes are rolled back. |
| | | 176 | | /// </summary> |
| | | 177 | | public async Task<bool> PutAllAsync<T>(string storeName, List<T> items) |
| | 51 | 178 | | { |
| | 51 | 179 | | EnsureInitialized(); |
| | | 180 | | |
| | 51 | 181 | | if (items == null || items.Count == 0) |
| | 27 | 182 | | return true; |
| | | 183 | | |
| | | 184 | | try |
| | 24 | 185 | | { |
| | 24 | 186 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.PutAllItems, storeName, items); |
| | 12 | 187 | | return true; |
| | | 188 | | } |
| | 12 | 189 | | catch (Exception ex) |
| | 12 | 190 | | { |
| | 12 | 191 | | _logger.LogError(ex, Constants.Messages.LogErrorPuttingAllItems, storeName); |
| | 12 | 192 | | return false; |
| | | 193 | | } |
| | 51 | 194 | | } |
| | | 195 | | |
| | | 196 | | public async Task<bool> DeleteAsync(string storeName, string key) |
| | 24 | 197 | | { |
| | 24 | 198 | | EnsureInitialized(); |
| | | 199 | | |
| | | 200 | | try |
| | 24 | 201 | | { |
| | 24 | 202 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.DeleteItem, storeName, key); |
| | 12 | 203 | | return true; |
| | | 204 | | } |
| | 12 | 205 | | catch (Exception ex) |
| | 12 | 206 | | { |
| | 12 | 207 | | _logger.LogError(ex, Constants.Messages.LogErrorDeletingItem, storeName); |
| | 12 | 208 | | return false; |
| | | 209 | | } |
| | 24 | 210 | | } |
| | | 211 | | |
| | | 212 | | public async Task<bool> ClearAsync(string storeName) |
| | 25 | 213 | | { |
| | 25 | 214 | | EnsureInitialized(); |
| | | 215 | | |
| | | 216 | | try |
| | 25 | 217 | | { |
| | 25 | 218 | | await _jsRuntime.InvokeVoidAsync(Constants.IndexedDbJsFunctions.ClearStore, storeName); |
| | 13 | 219 | | return true; |
| | | 220 | | } |
| | 12 | 221 | | catch (Exception ex) |
| | 12 | 222 | | { |
| | 12 | 223 | | _logger.LogError(ex, Constants.Messages.LogErrorClearingStore, storeName); |
| | 12 | 224 | | return false; |
| | | 225 | | } |
| | 25 | 226 | | } |
| | | 227 | | |
| | | 228 | | public async Task<int> GetCountAsync(string storeName) |
| | 3 | 229 | | { |
| | 3 | 230 | | EnsureInitialized(); |
| | | 231 | | |
| | | 232 | | try |
| | 2 | 233 | | { |
| | 2 | 234 | | return await _jsRuntime.InvokeAsync<int>(Constants.IndexedDbJsFunctions.GetCount, storeName); |
| | | 235 | | } |
| | 1 | 236 | | catch (Exception ex) |
| | 1 | 237 | | { |
| | 1 | 238 | | _logger.LogDebug(ex, Constants.Messages.LogErrorGettingCount, storeName); |
| | 1 | 239 | | return 0; |
| | | 240 | | } |
| | 2 | 241 | | } |
| | | 242 | | |
| | | 243 | | public ValueTask DisposeAsync() |
| | 12 | 244 | | { |
| | | 245 | | // No module to dispose since we're using _jsRuntime directly |
| | 12 | 246 | | return ValueTask.CompletedTask; |
| | 12 | 247 | | } |
| | | 248 | | |
| | | 249 | | private void EnsureInitialized() |
| | 297 | 250 | | { |
| | 297 | 251 | | if (!_isInitialized) |
| | 14 | 252 | | { |
| | 14 | 253 | | throw new InvalidOperationException(Constants.Messages.IndexedDbNotInitialized); |
| | | 254 | | } |
| | 283 | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Notifies subscribers of storage errors |
| | | 259 | | /// </summary> |
| | | 260 | | private void NotifyStorageError(string errorMessage) |
| | 44 | 261 | | { |
| | 44 | 262 | | OnStorageError?.Invoke(errorMessage); |
| | 44 | 263 | | } |
| | | 264 | | |
| | | 265 | | private T? DeserializeItem<T>(JsonElement result, string storeName, string key) |
| | 25 | 266 | | { |
| | | 267 | | try |
| | 25 | 268 | | { |
| | 25 | 269 | | return JsonSerializer.Deserialize<T>(result.GetRawText(), _jsonOptions); |
| | | 270 | | } |
| | 13 | 271 | | catch (JsonException jsonEx) |
| | 13 | 272 | | { |
| | 13 | 273 | | HandleDeserializationError(jsonEx, storeName, key); |
| | 13 | 274 | | return default; |
| | | 275 | | } |
| | 25 | 276 | | } |
| | | 277 | | |
| | | 278 | | private List<T> DeserializeList<T>(JsonElement result, string storeName, string key) |
| | 43 | 279 | | { |
| | | 280 | | try |
| | 43 | 281 | | { |
| | 43 | 282 | | var list = JsonSerializer.Deserialize<List<T>>(result.GetRawText(), _jsonOptions); |
| | 37 | 283 | | return list ?? new List<T>(); |
| | | 284 | | } |
| | 6 | 285 | | catch (JsonException jsonEx) |
| | 6 | 286 | | { |
| | 6 | 287 | | HandleDeserializationError(jsonEx, storeName, key); |
| | 6 | 288 | | return new List<T>(); |
| | | 289 | | } |
| | 43 | 290 | | } |
| | | 291 | | |
| | | 292 | | private void HandleDeserializationError(JsonException jsonEx, string storeName, string key) |
| | 19 | 293 | | { |
| | 19 | 294 | | _logger.LogWarning(jsonEx, Constants.Messages.LogJsonDeserializationFailed, storeName, key); |
| | 19 | 295 | | NotifyStorageError($"Data corruption detected in {storeName}"); |
| | 19 | 296 | | } |
| | | 297 | | } |