finalizare 1.0
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Application.Services.HashingAlgorithms; // Ensure this namespace matches your actual service namespace
|
||||
using Infrastructure.Services.HashingAlgorithms; // Ensure this namespace matches your actual service namespace
|
||||
using Domain.Entities;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Infrastructure.Migrations;
|
||||
|
||||
/// <inheritdoc />
|
||||
public partial class SeedAdminTable : Migration{
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public SeedAdminTable()
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../API"))
|
||||
.AddJsonFile("appsettings.json");
|
||||
|
||||
_configuration = builder.Build();
|
||||
|
||||
// Build the service provider
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddSingleton<IHashingAlgorithms, HashingAlgorithms>()
|
||||
.BuildServiceProvider();
|
||||
|
||||
// Resolve the hashing service
|
||||
_hashingAlgorithms = serviceProvider.GetService<IHashingAlgorithms>();
|
||||
}
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
var adminName = _configuration["AdminSettings:Name"];
|
||||
var adminEmail = _configuration["AdminSettings:Email"];
|
||||
var adminPassword = _configuration["AdminSettings:Password"];
|
||||
|
||||
var adminUser = new Admin();
|
||||
adminUser.SetName(adminName);
|
||||
adminUser.SetEmail(adminEmail);
|
||||
adminUser.SetPassword(_hashingAlgorithms.Sha256Algorithm(adminPassword));
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "Admins",
|
||||
columns: new[] { "Id", "Name", "Email", "Password" },
|
||||
values: new object[] { adminUser.Id, adminUser.Name, adminUser.Email, adminUser.Password }
|
||||
);
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
var adminEmail = _configuration["AdminSettings:Email"];
|
||||
migrationBuilder.DeleteData(
|
||||
table: "Admins",
|
||||
keyColumn: "Email",
|
||||
keyValue: adminEmail
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user