diff --git a/Single_Page_Anwendung/Models/DatenEinlesen.cs b/Single_Page_Anwendung/Models/DatenEinlesen.cs index 5d39866..8fb6eff 100644 --- a/Single_Page_Anwendung/Models/DatenEinlesen.cs +++ b/Single_Page_Anwendung/Models/DatenEinlesen.cs @@ -1,6 +1,42 @@ -namespace Single_Page_Anwendung.Models +using Aps_Single_Page_Anwendung.Models; +using Aps_Single_Page_Anwendung.Repositories; +using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Linq; + +namespace Single_Page_Anwendung.Models { public class DatenEinlesen { + public static void Initialize(IServiceProvider serviceProvider) + { + // Holy shit Oo + using (var futterContext = new FutterContext(serviceProvider.GetRequiredService>())) + { + if (futterContext.Speisen.Any()) + { + return; + } + var kategorieRepository = + new FileKategorieRepository(serviceProvider.GetRequiredService()); + + var kategorien = kategorieRepository.GetKategorien().ToList(); + + kategorien.ForEach(k => + { + k.Id = 0; + foreach (var speise in k.Speisen) + { + speise.Id = 0; + speise.KategorieId = 0; + } + }); + + futterContext.Kategorien.AddRange(kategorien); + futterContext.SaveChanges(); + } + } } } diff --git a/Single_Page_Anwendung/Program.cs b/Single_Page_Anwendung/Program.cs index fb02a94..464d60e 100644 --- a/Single_Page_Anwendung/Program.cs +++ b/Single_Page_Anwendung/Program.cs @@ -1,7 +1,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Single_Page_Anwendung.Models; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +15,23 @@ namespace Aps_Single_Page_Anwendung { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + using(var scope = host.Services.CreateScope()) + { + var service = scope.ServiceProvider; + try + { + DatenEinlesen.Initialize(service); + } + catch (Exception ex) + { + var logger = service.GetRequiredService>(); + logger.LogError(ex, "Fehler mit der Datenbank!"); + throw; + } + } + + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/Single_Page_Anwendung/data/kategorie.json b/Single_Page_Anwendung/data/kategorie.json index 9ef623b..2371b6f 100644 --- a/Single_Page_Anwendung/data/kategorie.json +++ b/Single_Page_Anwendung/data/kategorie.json @@ -1,31 +1,31 @@ [{ "id": 1, "name": "Salate", - "description": "Fröhlich bunte Salate" + "beschreibung": "Fröhlich bunte Salate" }, { "id": 2, "name": "Suppen", - "description": "Cremige Suppen" + "beschreibung": "Cremige Suppen" }, { "id": 3, "name": "Vorspeißen", - "description": "Kleine Portionen" + "beschreibung": "Kleine Portionen" }, { "id": 4, "name": "Hauptgerichte", - "description": "Eine vielfalt aus verschiedenen Gerichten" + "beschreibung": "Eine vielfalt aus verschiedenen Gerichten" }, { "id": 5, "name": "Nachspeißen", - "description": "Leckere süße Desserts" + "beschreibung": "Leckere süße Desserts" }, { "id": 6, "name": "Getränke", - "description": "Eine große Auswahl an Getränkeetränken" + "beschreibung": "Eine große Auswahl an Getränkeetränken" } ] \ No newline at end of file