11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Diagnostics . CodeAnalysis ;
45using System . Linq ;
@@ -15,8 +16,9 @@ namespace NetEvent.Client.Services
1516 [ ExcludeFromCodeCoverage ( Justification = "Ignore UI Services" ) ]
1617 public class SystemSettingsService : ISystemSettingsDataService
1718 {
18- private readonly IHttpClientFactory _HttpClientFactory ;
1919 private readonly ILogger < SystemSettingsService > _Logger ;
20+ private readonly IHttpClientFactory _HttpClientFactory ;
21+ private readonly ConcurrentDictionary < string , ConcurrentBag < Action < SystemSettingValueDto > > > _Callbacks = new ConcurrentDictionary < string , ConcurrentBag < Action < SystemSettingValueDto > > > ( ) ;
2022
2123 public SystemSettingsService ( IHttpClientFactory httpClientFactory , ILogger < SystemSettingsService > logger )
2224 {
@@ -27,7 +29,14 @@ public SystemSettingsService(IHttpClientFactory httpClientFactory, ILogger<Syste
2729 public async Task < SystemSettingValueDto ? > GetSystemSettingAsync ( SystemSettingGroup systemSettingGroup , string key , CancellationToken cancellationToken )
2830 {
2931 var systemSettings = await GetSystemSettingsAsync ( systemSettingGroup , cancellationToken ) ;
30- return systemSettings . FirstOrDefault ( x => x . Key . Equals ( SystemSettings . OrganizationName , StringComparison . Ordinal ) ) ;
32+ return systemSettings . FirstOrDefault ( x => x . Key . Equals ( key , StringComparison . Ordinal ) ) ;
33+ }
34+
35+ public Task < SystemSettingValueDto ? > GetSystemSettingAsync ( SystemSettingGroup systemSettingGroup , string key , Action < SystemSettingValueDto > valueChanged , CancellationToken cancellationToken )
36+ {
37+ var bag = _Callbacks . GetOrAdd ( GetCallbackKey ( systemSettingGroup , key ) , s => new ConcurrentBag < Action < SystemSettingValueDto > > ( ) ) ;
38+ bag . Add ( valueChanged ) ;
39+ return GetSystemSettingAsync ( systemSettingGroup , key , cancellationToken ) ;
3140 }
3241
3342 public async Task < List < SystemSettingValueDto > > GetSystemSettingsAsync ( SystemSettingGroup systemSettingGroup , CancellationToken cancellationToken )
@@ -63,6 +72,14 @@ public async Task<ServiceResult> UpdateSystemSetting(SystemSettingGroup systemSe
6372
6473 response . EnsureSuccessStatusCode ( ) ;
6574
75+ if ( _Callbacks . TryGetValue ( GetCallbackKey ( systemSettingGroup , systemSetting . Key ) , out var callbacks ) )
76+ {
77+ foreach ( var callback in callbacks )
78+ {
79+ callback ( systemSetting ) ;
80+ }
81+ }
82+
6683 return ServiceResult . Success ( "RoleService.UpdateRoleAsync.Success" ) ;
6784 }
6885 catch ( Exception ex )
@@ -72,5 +89,10 @@ public async Task<ServiceResult> UpdateSystemSetting(SystemSettingGroup systemSe
7289
7390 return ServiceResult . Error ( "RoleService.UpdateRoleAsync.Error" ) ;
7491 }
92+
93+ private static string GetCallbackKey ( SystemSettingGroup systemSettingGroup , string key )
94+ {
95+ return $ "{ systemSettingGroup } .{ key } ";
96+ }
7597 }
7698}
0 commit comments