mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 11:00:06 +01:00
Seedowanie
This commit is contained in:
40
Controllers/Seed.cs
Normal file
40
Controllers/Seed.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using QuotifyBE.Data;
|
||||
using QuotifyBE.DTOs;
|
||||
using QuotifyBE.Entities;
|
||||
using QuotifyBE.Mapping;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace QuotifyBE.Controllers
|
||||
{
|
||||
public class Seed : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly GeneralUseHelpers guhf;
|
||||
|
||||
public Seed(ApplicationDbContext db, GeneralUseHelpers GUHF)
|
||||
{
|
||||
_db = db;
|
||||
guhf = GUHF;
|
||||
}
|
||||
public async Task SeedAsync()
|
||||
{
|
||||
var AccountNum = await _db.Users.CountAsync();
|
||||
if (AccountNum == 0)
|
||||
{
|
||||
var Admin = new User
|
||||
{
|
||||
Name="admin",
|
||||
Email = "admin@mail.com",
|
||||
PasswordHash = guhf.HashWithSHA512("admin")
|
||||
|
||||
};
|
||||
_db.Users.Add(Admin);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Program.cs
10
Program.cs
@@ -72,7 +72,15 @@ builder.Services.AddSwaggerGen(options =>
|
||||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
var app = builder.Build();
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var guhf = scope.ServiceProvider.GetRequiredService<GeneralUseHelpers>();
|
||||
|
||||
var seeder = new Seed(db, guhf);
|
||||
await seeder.SeedAsync();
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
||||
Reference in New Issue
Block a user