45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using ZA.CoreService.ESBCertificateManager.Models;
|
||
|
||
namespace ZA.CoreService.ESBCertificateManager.Data;
|
||
|
||
public static class TargetRepositoryFactory
|
||
{
|
||
public static async Task<(ITargetRepository Repository, string LoadMessage)> CreateAsync(
|
||
AppSettings settings,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
string samplePath = Path.GetFullPath(
|
||
Path.Combine(AppContext.BaseDirectory, settings.SampleTargetsPath));
|
||
|
||
if (settings.UseOfflineSampleData)
|
||
{
|
||
ITargetRepository jsonRepo = new JsonTargetRepository(samplePath);
|
||
_ = await jsonRepo.GetActiveTargetsAsync(cancellationToken);
|
||
return (jsonRepo, $"Ziele geladen aus {jsonRepo.SourceDescription}.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(settings.ConnectionString))
|
||
{
|
||
return OfflineSample(samplePath, "Keine SQL-Verbindung konfiguriert – Offline-Sample wird verwendet.");
|
||
}
|
||
|
||
try
|
||
{
|
||
SqlTargetRepository sqlRepo = new(settings.ConnectionString);
|
||
_ = await sqlRepo.GetActiveTargetsAsync(cancellationToken);
|
||
return (sqlRepo, $"Ziele geladen aus {sqlRepo.SourceDescription}.");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return OfflineSample(
|
||
samplePath,
|
||
$"SQL nicht erreichbar ({ex.Message}). Offline-Sample wird verwendet.");
|
||
}
|
||
}
|
||
|
||
private static (ITargetRepository Repository, string LoadMessage) OfflineSample(
|
||
string samplePath,
|
||
string message)
|
||
=> (new JsonTargetRepository(samplePath), message);
|
||
}
|