CheckPoint: Avances Varios

This commit is contained in:
2025-12-18 13:32:50 -03:00
parent 8f535f3a6e
commit 32663e6324
92 changed files with 12629 additions and 195 deletions

View File

@@ -1,4 +1,6 @@
using Dapper;
// Asegúrate de que BCrypt.Net esté instalado en este proyecto o referenciado
// Si no te reconoce BCrypt, avísame, pero debería funcionar porque ya se usa en AuthService.
namespace SIGCM.Infrastructure.Data;
@@ -14,60 +16,7 @@ public class DbInitializer
public async Task InitializeAsync()
{
using var connection = _connectionFactory.CreateConnection();
var sql = @"
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Users')
BEGIN
CREATE TABLE Users (
Id INT IDENTITY(1,1) PRIMARY KEY,
Username NVARCHAR(50) NOT NULL UNIQUE,
PasswordHash NVARCHAR(255) NOT NULL,
Role NVARCHAR(20) NOT NULL,
Email NVARCHAR(100) NULL,
CreatedAt DATETIME DEFAULT GETUTCDATE()
);
-- Seed generic admin (password: admin123)
-- Hash created with BCrypt
INSERT INTO Users (Username, PasswordHash, Role)
VALUES ('admin', '$2a$11$u.w..ExampleHashPlaceholder...', 'Admin');
END
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Categories')
BEGIN
CREATE TABLE Categories (
Id INT IDENTITY(1,1) PRIMARY KEY,
ParentId INT NULL,
Name NVARCHAR(100) NOT NULL,
Slug NVARCHAR(100) NOT NULL,
Active BIT DEFAULT 1,
FOREIGN KEY (ParentId) REFERENCES Categories(Id)
);
END
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Operations')
BEGIN
CREATE TABLE Operations (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(50) NOT NULL UNIQUE
);
END
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'CategoryOperations')
BEGIN
CREATE TABLE CategoryOperations (
CategoryId INT NOT NULL,
OperationId INT NOT NULL,
PRIMARY KEY (CategoryId, OperationId),
FOREIGN KEY (CategoryId) REFERENCES Categories(Id) ON DELETE CASCADE,
FOREIGN KEY (OperationId) REFERENCES Operations(Id) ON DELETE CASCADE
);
END
";
// Fixing the placeholder hash to a valid one might be necessary if I want to login immediately.
// I will update the hash command later or create a small utility to generate one.
// For now, I'll remove the INSERT or comment it out until I can generate a real hash in C#.
var schemaSql = @"
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Users')
BEGIN
@@ -167,6 +116,22 @@ BEGIN
);
END
";
// Ejecutar creación de tablas
await connection.ExecuteAsync(schemaSql);
// --- SEED DE DATOS (Usuario Admin) ---
var adminCount = await connection.ExecuteScalarAsync<int>("SELECT COUNT(1) FROM Users WHERE Username = 'admin'");
if (adminCount == 0)
{
// Creamos el hash válido para la clave del usuario usando la librería del proyecto
var passwordHash = BCrypt.Net.BCrypt.HashPassword("Diagonal423");
var insertAdminSql = @"
INSERT INTO Users (Username, PasswordHash, Role, Email)
VALUES ('admin', @PasswordHash, 'Admin', 'admin@sigcm.com')";
await connection.ExecuteAsync(insertAdminSql, new { PasswordHash = passwordHash });
}
}
}
}