This commit is contained in:
@@ -3,10 +3,17 @@ namespace TaxBaik.Infrastructure.Data;
|
||||
using System.Data;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using Dapper;
|
||||
using TaxBaik.Domain.Interfaces;
|
||||
|
||||
public sealed class DbConnectionFactory : IDbConnectionFactory
|
||||
{
|
||||
static DbConnectionFactory()
|
||||
{
|
||||
// Keep PostgreSQL snake_case columns aligned with C# PascalCase properties.
|
||||
DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
}
|
||||
|
||||
private readonly string _connectionString;
|
||||
|
||||
public DbConnectionFactory(IConfiguration configuration)
|
||||
|
||||
@@ -98,6 +98,33 @@ public class MigrationRunner
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var resourceNames = assembly.GetManifestResourceNames()
|
||||
.Where(x => x.Contains(".Migrations.V") && x.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(x => x);
|
||||
|
||||
foreach (var resourceName in resourceNames)
|
||||
{
|
||||
using var stream = assembly.GetManifestResourceStream(resourceName);
|
||||
if (stream == null)
|
||||
continue;
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
var sql = reader.ReadToEnd();
|
||||
var fileName = Path.GetFileNameWithoutExtension(resourceName);
|
||||
var versionStart = fileName.IndexOf('V');
|
||||
var versionEnd = fileName.IndexOf('_', versionStart + 1);
|
||||
if (versionStart < 0 || versionEnd < 0)
|
||||
continue;
|
||||
|
||||
var version = fileName.Substring(versionStart + 1, versionEnd - versionStart - 1);
|
||||
var description = fileName.Substring(versionEnd + 1);
|
||||
|
||||
migrations.Add(new Migration { Version = version, Description = description, Sql = sql });
|
||||
}
|
||||
}
|
||||
|
||||
return migrations;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,16 @@ public class AdminUserRepository : BaseRepository, IAdminUserRepository
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
return await conn.QueryFirstOrDefaultAsync<AdminUser>(
|
||||
"SELECT * FROM admin_users WHERE username = @username",
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
username,
|
||||
password_hash AS PasswordHash,
|
||||
last_login_at AS LastLoginAt,
|
||||
created_at AS CreatedAt
|
||||
FROM admin_users
|
||||
WHERE username = @username
|
||||
""",
|
||||
new { username });
|
||||
}
|
||||
|
||||
@@ -20,7 +29,16 @@ public class AdminUserRepository : BaseRepository, IAdminUserRepository
|
||||
{
|
||||
using var conn = _connectionFactory.CreateConnection();
|
||||
return await conn.QueryFirstOrDefaultAsync<AdminUser>(
|
||||
"SELECT * FROM admin_users WHERE id = @id",
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
username,
|
||||
password_hash AS PasswordHash,
|
||||
last_login_at AS LastLoginAt,
|
||||
created_at AS CreatedAt
|
||||
FROM admin_users
|
||||
WHERE id = @id
|
||||
""",
|
||||
new { id });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user