mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
Format ./web/backend/Infrastructure/Infrastructure.Sql
This commit is contained in:
@@ -9,12 +9,9 @@ namespace Infrastructure.Sql;
|
|||||||
/// resolving the connection string from environment variables or application configuration.
|
/// resolving the connection string from environment variables or application configuration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configuration">The application configuration, used as a fallback source for the connection string.</param>
|
/// <param name="configuration">The application configuration, used as a fallback source for the connection string.</param>
|
||||||
public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
public class DefaultSqlConnectionFactory(IConfiguration configuration) : ISqlConnectionFactory
|
||||||
: ISqlConnectionFactory
|
|
||||||
{
|
{
|
||||||
private readonly string _connectionString = GetConnectionString(
|
private readonly string _connectionString = GetConnectionString(configuration);
|
||||||
configuration
|
|
||||||
);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new, unopened <see cref="SqlConnection" /> using the resolved connection string.
|
/// Creates a new, unopened <see cref="SqlConnection" /> using the resolved connection string.
|
||||||
@@ -39,10 +36,9 @@ public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
|||||||
private static string GetConnectionString(IConfiguration configuration)
|
private static string GetConnectionString(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
// Check for full connection string first
|
// Check for full connection string first
|
||||||
string? fullConnectionString = Environment.GetEnvironmentVariable(
|
string? fullConnectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
|
||||||
"DB_CONNECTION_STRING"
|
if (!string.IsNullOrEmpty(fullConnectionString))
|
||||||
);
|
return fullConnectionString;
|
||||||
if (!string.IsNullOrEmpty(fullConnectionString)) return fullConnectionString;
|
|
||||||
|
|
||||||
// Try to build from individual environment variables (preferred method for Docker)
|
// Try to build from individual environment variables (preferred method for Docker)
|
||||||
try
|
try
|
||||||
@@ -53,7 +49,8 @@ public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
|||||||
{
|
{
|
||||||
// Fall back to configuration-based connection string if env vars are not set
|
// Fall back to configuration-based connection string if env vars are not set
|
||||||
string? connString = configuration.GetConnectionString("Default");
|
string? connString = configuration.GetConnectionString("Default");
|
||||||
if (!string.IsNullOrEmpty(connString)) return connString;
|
if (!string.IsNullOrEmpty(connString))
|
||||||
|
return connString;
|
||||||
|
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
"Database connection string not configured. Set DB_CONNECTION_STRING or DB_SERVER, DB_NAME, DB_USER, DB_PASSWORD env vars or ConnectionStrings:Default."
|
"Database connection string not configured. Set DB_CONNECTION_STRING or DB_SERVER, DB_NAME, DB_USER, DB_PASSWORD env vars or ConnectionStrings:Default."
|
||||||
|
|||||||
@@ -6,15 +6,12 @@
|
|||||||
<RootNamespace>Infrastructure.Sql</RootNamespace>
|
<RootNamespace>Infrastructure.Sql</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.4"/>
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.4" />
|
||||||
|
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
|
||||||
|
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||||
<PackageReference
|
<PackageReference
|
||||||
Include="Microsoft.SqlServer.Types"
|
Include="Microsoft.Extensions.Configuration.Abstractions"
|
||||||
Version="160.1000.6"
|
Version="9.0.0"
|
||||||
/>
|
|
||||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0"/>
|
|
||||||
<PackageReference
|
|
||||||
Include="Microsoft.Extensions.Configuration.Abstractions"
|
|
||||||
Version="9.0.0"
|
|
||||||
/>
|
/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -17,28 +17,20 @@ public static class SqlConnectionStringHelper
|
|||||||
{
|
{
|
||||||
string server =
|
string server =
|
||||||
Environment.GetEnvironmentVariable("DB_SERVER")
|
Environment.GetEnvironmentVariable("DB_SERVER")
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException("DB_SERVER environment variable is not set");
|
||||||
"DB_SERVER environment variable is not set"
|
|
||||||
);
|
|
||||||
|
|
||||||
string dbName =
|
string dbName =
|
||||||
databaseName
|
databaseName
|
||||||
?? Environment.GetEnvironmentVariable("DB_NAME")
|
?? Environment.GetEnvironmentVariable("DB_NAME")
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException("DB_NAME environment variable is not set");
|
||||||
"DB_NAME environment variable is not set"
|
|
||||||
);
|
|
||||||
|
|
||||||
string user =
|
string user =
|
||||||
Environment.GetEnvironmentVariable("DB_USER")
|
Environment.GetEnvironmentVariable("DB_USER")
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException("DB_USER environment variable is not set");
|
||||||
"DB_USER environment variable is not set"
|
|
||||||
);
|
|
||||||
|
|
||||||
string password =
|
string password =
|
||||||
Environment.GetEnvironmentVariable("DB_PASSWORD")
|
Environment.GetEnvironmentVariable("DB_PASSWORD")
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException("DB_PASSWORD environment variable is not set");
|
||||||
"DB_PASSWORD environment variable is not set"
|
|
||||||
);
|
|
||||||
|
|
||||||
SqlConnectionStringBuilder builder = new()
|
SqlConnectionStringBuilder builder = new()
|
||||||
{
|
{
|
||||||
@@ -47,7 +39,7 @@ public static class SqlConnectionStringHelper
|
|||||||
UserID = user,
|
UserID = user,
|
||||||
Password = password,
|
Password = password,
|
||||||
TrustServerCertificate = true,
|
TrustServerCertificate = true,
|
||||||
Encrypt = true
|
Encrypt = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
return builder.ConnectionString;
|
return builder.ConnectionString;
|
||||||
|
|||||||
Reference in New Issue
Block a user