From 9e00954c294231783697043627eebbfc3202ee2b Mon Sep 17 00:00:00 2001 From: kuba Date: Wed, 16 Jul 2025 11:34:03 +0200 Subject: [PATCH] Seedowanie --- Controllers/Seed.cs | 40 ++++++++++++++++++++++++++++++++++++++++ Program.cs | 10 +++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Controllers/Seed.cs diff --git a/Controllers/Seed.cs b/Controllers/Seed.cs new file mode 100644 index 0000000..685e66f --- /dev/null +++ b/Controllers/Seed.cs @@ -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(); + } + + } + } +} diff --git a/Program.cs b/Program.cs index ce24c1f..e5b803f 100644 --- a/Program.cs +++ b/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(); + var guhf = scope.ServiceProvider.GetRequiredService(); + + var seeder = new Seed(db, guhf); + await seeder.SeedAsync(); +} // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment())