| | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | 2 | | using MRA.Infrastructure.Cache; |
| | 3 | | using MRA.Infrastructure.RemoteConfig; |
| | 4 | | using MRA.Infrastructure.Settings; |
| | 5 | | using MRA.Services.Cache; |
| | 6 | |
|
| | 7 | | namespace MRA.Services.RemoteConfig; |
| | 8 | |
|
| | 9 | | public class RemoteConfigService : CacheServiceBase, IRemoteConfigService |
| | 10 | | { |
| | 11 | | private List<object> _keys; |
| | 12 | |
|
| | 13 | | private readonly IRemoteConfigDatabase _remoteConfig; |
| | 14 | |
|
| | 15 | | public RemoteConfigService( |
| | 16 | | ICacheProvider cache, |
| | 17 | | AppSettings appSettings, |
| 1 | 18 | | IRemoteConfigDatabase remoteConfigDatabase) : base(appSettings, cache) |
| | 19 | | { |
| 1 | 20 | | _remoteConfig = remoteConfigDatabase; |
| 1 | 21 | | RegisterRemoteConfig(); |
| 1 | 22 | | } |
| | 23 | |
|
| | 24 | | private void RegisterRemoteConfig() |
| | 25 | | { |
| 1 | 26 | | _keys = new List<object>() |
| 1 | 27 | | { |
| 1 | 28 | | new RemoteConfigSetting<double>(RemoteConfigKey.Popularity_Date, _appSettings.RemoteConfig.DefaultValues.Pop |
| 1 | 29 | | new RemoteConfigSetting<int>(RemoteConfigKey.Popularity_Months, _appSettings.RemoteConfig.DefaultValues.Popu |
| 1 | 30 | | new RemoteConfigSetting<double>(RemoteConfigKey.Popularity_Critic, _appSettings.RemoteConfig.DefaultValues.P |
| 1 | 31 | | new RemoteConfigSetting<double>(RemoteConfigKey.Popularity_Popular, _appSettings.RemoteConfig.DefaultValues. |
| 1 | 32 | | new RemoteConfigSetting<double>(RemoteConfigKey.Popularity_Favorite, _appSettings.RemoteConfig.DefaultValues |
| 1 | 33 | | }; |
| 1 | 34 | | } |
| | 35 | |
|
| | 36 | | public double GetPopularityDate() => |
| 0 | 37 | | GetOrSetFromCache(RemoteConfigKey.Popularity_Date.ToString(), () => _remoteConfig.GetValue(GetKey<double>(Remote |
| | 38 | | public int GetPopularityMonths() => |
| 0 | 39 | | GetOrSetFromCache(RemoteConfigKey.Popularity_Date.ToString(), () => _remoteConfig.GetValue(GetKey<int>(RemoteCon |
| | 40 | | public double GetPopularityCritic() => |
| 0 | 41 | | GetOrSetFromCache(RemoteConfigKey.Popularity_Date.ToString(), () => _remoteConfig.GetValue(GetKey<double>(Remote |
| | 42 | | public double GetPopularityPopular() => |
| 0 | 43 | | GetOrSetFromCache(RemoteConfigKey.Popularity_Date.ToString(), () => _remoteConfig.GetValue(GetKey<double>(Remote |
| | 44 | | public double GetPopularityFavorite() => |
| 0 | 45 | | GetOrSetFromCache(RemoteConfigKey.Popularity_Date.ToString(), () => _remoteConfig.GetValue(GetKey<double>(Remote |
| | 46 | |
|
| | 47 | |
|
| | 48 | | private RemoteConfigSetting<T> GetKey<T>(RemoteConfigKey key) |
| | 49 | | { |
| 0 | 50 | | var config = _keys.OfType<RemoteConfigSetting<T>>() |
| 0 | 51 | | .FirstOrDefault(k => k.Key == key); |
| | 52 | |
|
| 0 | 53 | | if (config == null) |
| 0 | 54 | | throw new InvalidOperationException($"La clave '{key}' no está registrada."); |
| | 55 | |
|
| 0 | 56 | | return config; |
| | 57 | | } |
| | 58 | | } |