47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using ZA.CoreService.ESBCertificateManager.Models;
|
|
|
|
namespace ZA.CoreService.ESBCertificateManager.Data;
|
|
|
|
public sealed class JsonTargetRepository : ITargetRepository
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
Converters = { new JsonStringEnumConverter() }
|
|
};
|
|
|
|
private readonly string _filePath;
|
|
|
|
public JsonTargetRepository(string filePath)
|
|
{
|
|
_filePath = filePath;
|
|
}
|
|
|
|
public string SourceDescription => $"Offline-Sample ({Path.GetFileName(_filePath)})";
|
|
|
|
public async Task<IReadOnlyList<DeploymentTarget>> GetActiveTargetsAsync(
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (!File.Exists(_filePath))
|
|
{
|
|
throw new FileNotFoundException(
|
|
$"Sample-Zieldatei wurde nicht gefunden: {_filePath}",
|
|
_filePath);
|
|
}
|
|
|
|
await using FileStream stream = File.OpenRead(_filePath);
|
|
List<DeploymentTarget>? targets = await JsonSerializer.DeserializeAsync<List<DeploymentTarget>>(
|
|
stream,
|
|
JsonOptions,
|
|
cancellationToken);
|
|
|
|
return (targets ?? [])
|
|
.Where(t => t.IsActive)
|
|
.OrderBy(t => t.SortOrder)
|
|
.ThenBy(t => t.Name)
|
|
.ToList();
|
|
}
|
|
}
|