Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- przestrzeń nazw SoniqCalendar.Services
- {
- ClientManager klasy publicznej
- {
- klasa publiczna OptionsClientApplication
- {
- ciąg publiczny Nazwa {get; zestaw; }
- ciąg publiczny ApiKey {get; zestaw; }
- ciąg publiczny DatabaseProvider {get; zestaw; }
- ciąg publiczny DatabaseConnectionString {get; zestaw; }
- }
- Opcje klasy publicznej
- {
- ciąg publiczny SettingsDir {get; zestaw; }
- public OptionsClientApplication [] ClientApplications {get; zestaw; }
- }
- prywatne tylko do odczytu Opcje _opcje;
- prywatny tylko do odczytu List <ClientApplication> _clients = new List <ClientApplication> ();
- prywatny słownik tylko do odczytu <ciąg, ClientApplication> _clientApiKeyLookup =
- nowy słownik <ciąg, ClientApplication> ();
- prywatny tylko do odczytu JsonSerializerOptions _jsonSerializerOptions;
- public IEnumerable <ClientApplication> Clients => _clients;
- public ClientManager (opcje IOptions <Opcje>)
- {
- _options = opcje. Wartość;
- _jsonSerializerOptions = new JsonSerializerOptions ()
- {
- WriteIndented = true,
- Konwertery =
- {
- nowy TimeSpanJsonConverter (),
- }
- };
- }
- /// <podsumowanie>
- /// Rejestruje aplikacje klienckie i inicjuje ich bazy danych.
- /// Musi zostać wywołany przed użyciem.
- /// </summary>
- public async Task Initialize ()
- {
- if (! Directory.Exists (_options.SettingsDir))
- Directory.CreateDirectory (_options.SettingsDir);
- foreach (klient var w _options.ClientApplications)
- czekają na AddClientFromConfig (klient);
- }
- private async Zadanie AddClientFromConfig (OptionsClientApplication optionClient)
- {
- var app = new ClientApplication ()
- {
- Nazwa = optionClient.Name,
- ApiKey = optionClient.ApiKey,
- DbOptions = BuildDbOptions (optionClient.DatabaseProvider, optionClient.DatabaseConnectionString),
- Settings = LoadSettings (optionClient.Name),
- Logger = CreateLogger (),
- };
- czekaj na użycie (var context = new AppContext (app.DbOptions.Options))
- czekają na kontekst.Database.EnsureCreatedAsync ();
- _clients.Add (aplikacja);
- _clientApiKeyLookup.Add (app.ApiKey, aplikacja);
- }
- prywatny ILogger CreateLogger ()
- {
- var logger = new LoggerConfiguration ()
- .Enrich.FromLogContext ()
- .MinimumLevel.Debug ()
- .WriteTo.ColoredConsole (
- LogEventLevel.Verbose)
- .CreateLogger ();
- }
- public async Task SeedDatabases ()
- {
- foreach (klient var w _clients)
- {
- czekaj na użycie var context = new AppContext (client.DbOptions.Options);
- var seeder = new TestDbSeeder (kontekst);
- czekaj na seeder.Seed ();
- }
- }
- /// <podsumowanie>
- /// Zwraca <patrz cref = "ClientApplication" /> przez dany klucz interfejsu API.
- /// </summary>
- /// <wyjątek cref = "InvalidApiKeyException"> Zgłasza, gdy nie ma klienta z takim kluczem API. </exception>
- public ClientApplication GetByApiKey (ciąg apiKey)
- {
- if (! _clientApiKeyLookup.ContainsKey (apiKey))
- wrzuć nowy InvalidApiKeyException ();
- return _clientApiKeyLookup [apiKey];
- }
- prywatny statyczny DbCon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement