Simplify DB to six tables and load credentials via Always Encrypted SonicCredential.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using ZA.CoreService.ESBCertificateManager.Models;
|
||||
|
||||
namespace ZA.CoreService.ESBCertificateManager.Services;
|
||||
@@ -20,65 +21,57 @@ public sealed class DeploymentTargetRepository
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DeploymentTarget>> GetActiveAsync(
|
||||
string environmentCode,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(environmentCode))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"Der EnvironmentCode fehlt.",
|
||||
nameof(environmentCode));
|
||||
}
|
||||
|
||||
const string sql = """
|
||||
SELECT
|
||||
target.CertificateTargetId,
|
||||
target.TargetDirectory,
|
||||
target.TargetFileName,
|
||||
target.BackupEnabled,
|
||||
target.BackupDirectoryName,
|
||||
target.BackupRetentionDays,
|
||||
target.IsActive AS TargetIsActive,
|
||||
target.[CertificateTargetId],
|
||||
target.[TargetDirectory],
|
||||
target.[TargetFileName],
|
||||
target.[BackupEnabled],
|
||||
target.[BackupDirectoryName],
|
||||
target.[IsActive] AS [TargetIsActive],
|
||||
|
||||
container.SonicContainerId,
|
||||
container.ContainerName,
|
||||
container.ContainerDisplayName,
|
||||
container.RestartTimeoutSeconds,
|
||||
container.[SonicContainerId],
|
||||
container.[ContainerName],
|
||||
container.[ContainerDisplayName],
|
||||
container.[RestartTimeoutSeconds],
|
||||
|
||||
connection.ConnectionName,
|
||||
connection.[ConnectionName],
|
||||
|
||||
service.ServiceCode,
|
||||
service.ServiceName,
|
||||
environment.[EnvironmentCode],
|
||||
environment.[SortOrder] AS [EnvironmentSortOrder],
|
||||
|
||||
environment.EnvironmentCode,
|
||||
environment.SortOrder AS EnvironmentSortOrder,
|
||||
|
||||
company.CompanyCode,
|
||||
country.CountryCode
|
||||
FROM dbo.CertificateTarget AS target
|
||||
INNER JOIN dbo.SonicContainer AS container
|
||||
ON container.SonicContainerId = target.SonicContainerId
|
||||
INNER JOIN dbo.SonicConnection AS connection
|
||||
ON connection.SonicConnectionId = container.SonicConnectionId
|
||||
INNER JOIN dbo.EsbService AS service
|
||||
ON service.EsbServiceId = container.EsbServiceId
|
||||
INNER JOIN dbo.Company AS company
|
||||
ON company.CompanyId = service.CompanyId
|
||||
LEFT JOIN dbo.Country AS country
|
||||
ON country.CountryId = service.CountryId
|
||||
INNER JOIN dbo.Environment AS environment
|
||||
ON environment.EnvironmentId = service.EnvironmentId
|
||||
WHERE target.IsActive = 1
|
||||
AND container.IsActive = 1
|
||||
AND connection.IsActive = 1
|
||||
AND service.IsActive = 1
|
||||
AND company.IsActive = 1
|
||||
AND environment.IsActive = 1
|
||||
AND
|
||||
(
|
||||
country.CountryId IS NULL
|
||||
OR country.IsActive = 1
|
||||
)
|
||||
company.[CompanyCode]
|
||||
FROM [dbo].[CertificateTarget] AS target
|
||||
INNER JOIN [dbo].[SonicContainer] AS container
|
||||
ON container.[SonicContainerId] = target.[SonicContainerId]
|
||||
INNER JOIN [dbo].[SonicConnection] AS connection
|
||||
ON connection.[SonicConnectionId] = container.[SonicConnectionId]
|
||||
INNER JOIN [dbo].[Environment] AS environment
|
||||
ON environment.[EnvironmentId] = connection.[EnvironmentId]
|
||||
INNER JOIN [dbo].[Company] AS company
|
||||
ON company.[CompanyId] = container.[CompanyId]
|
||||
WHERE target.[IsActive] = 1
|
||||
AND container.[IsActive] = 1
|
||||
AND connection.[IsActive] = 1
|
||||
AND company.[IsActive] = 1
|
||||
AND environment.[IsActive] = 1
|
||||
AND environment.[EnvironmentCode] = @EnvironmentCode
|
||||
ORDER BY
|
||||
company.CompanyCode,
|
||||
country.CountryCode,
|
||||
environment.SortOrder,
|
||||
service.ServiceCode,
|
||||
container.ContainerName,
|
||||
target.TargetDirectory,
|
||||
target.TargetFileName;
|
||||
company.[CompanyCode],
|
||||
environment.[SortOrder],
|
||||
container.[ContainerName],
|
||||
target.[TargetDirectory],
|
||||
target.[TargetFileName];
|
||||
""";
|
||||
|
||||
List<DeploymentTarget> targets = [];
|
||||
@@ -88,6 +81,17 @@ public sealed class DeploymentTargetRepository
|
||||
|
||||
await using SqlCommand command = new(sql, connection);
|
||||
|
||||
command.Parameters.Add(
|
||||
new SqlParameter(
|
||||
"@EnvironmentCode",
|
||||
SqlDbType.NVarChar,
|
||||
30)
|
||||
{
|
||||
Value = environmentCode
|
||||
.Trim()
|
||||
.ToUpperInvariant()
|
||||
});
|
||||
|
||||
await using SqlDataReader reader =
|
||||
await command.ExecuteReaderAsync(cancellationToken);
|
||||
|
||||
@@ -106,9 +110,6 @@ public sealed class DeploymentTargetRepository
|
||||
int backupDirectoryNameOrdinal =
|
||||
reader.GetOrdinal("BackupDirectoryName");
|
||||
|
||||
int backupRetentionDaysOrdinal =
|
||||
reader.GetOrdinal("BackupRetentionDays");
|
||||
|
||||
int targetIsActiveOrdinal =
|
||||
reader.GetOrdinal("TargetIsActive");
|
||||
|
||||
@@ -124,12 +125,6 @@ public sealed class DeploymentTargetRepository
|
||||
int connectionNameOrdinal =
|
||||
reader.GetOrdinal("ConnectionName");
|
||||
|
||||
int serviceCodeOrdinal =
|
||||
reader.GetOrdinal("ServiceCode");
|
||||
|
||||
int serviceNameOrdinal =
|
||||
reader.GetOrdinal("ServiceName");
|
||||
|
||||
int environmentCodeOrdinal =
|
||||
reader.GetOrdinal("EnvironmentCode");
|
||||
|
||||
@@ -139,9 +134,6 @@ public sealed class DeploymentTargetRepository
|
||||
int companyCodeOrdinal =
|
||||
reader.GetOrdinal("CompanyCode");
|
||||
|
||||
int countryCodeOrdinal =
|
||||
reader.GetOrdinal("CountryCode");
|
||||
|
||||
while (await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
string containerName =
|
||||
@@ -152,34 +144,19 @@ public sealed class DeploymentTargetRepository
|
||||
reader,
|
||||
containerDisplayNameOrdinal);
|
||||
|
||||
string serviceCode =
|
||||
reader.GetString(serviceCodeOrdinal);
|
||||
|
||||
string serviceName =
|
||||
reader.GetString(serviceNameOrdinal);
|
||||
|
||||
string companyCode =
|
||||
reader.GetString(companyCodeOrdinal);
|
||||
|
||||
string? countryCode =
|
||||
ReadNullableString(reader, countryCodeOrdinal);
|
||||
|
||||
string displayName =
|
||||
string.IsNullOrWhiteSpace(containerDisplayName)
|
||||
? containerName
|
||||
: containerDisplayName;
|
||||
|
||||
string locationCode =
|
||||
string.IsNullOrWhiteSpace(countryCode)
|
||||
? companyCode
|
||||
: $"{companyCode}/{countryCode}";
|
||||
|
||||
targets.Add(new DeploymentTarget
|
||||
{
|
||||
Id = reader.GetInt32(targetIdOrdinal),
|
||||
|
||||
Name =
|
||||
$"{locationCode} / {serviceName} / {displayName}",
|
||||
Name = $"{companyCode} / {displayName}",
|
||||
|
||||
Environment =
|
||||
reader.GetString(environmentCodeOrdinal),
|
||||
@@ -199,10 +176,7 @@ public sealed class DeploymentTargetRepository
|
||||
BackupDirectoryName =
|
||||
reader.GetString(backupDirectoryNameOrdinal),
|
||||
|
||||
BackupRetentionDays =
|
||||
ReadNullableInt32(
|
||||
reader,
|
||||
backupRetentionDaysOrdinal),
|
||||
BackupRetentionDays = null,
|
||||
|
||||
ContainerName = containerName,
|
||||
|
||||
@@ -238,13 +212,4 @@ public sealed class DeploymentTargetRepository
|
||||
? null
|
||||
: reader.GetString(ordinal);
|
||||
}
|
||||
|
||||
private static int? ReadNullableInt32(
|
||||
SqlDataReader reader,
|
||||
int ordinal)
|
||||
{
|
||||
return reader.IsDBNull(ordinal)
|
||||
? null
|
||||
: reader.GetInt32(ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user