From 480a15d4a0c503ca8985a65bd846cc345fc7a658 Mon Sep 17 00:00:00 2001 From: artiga033 Date: Wed, 25 Aug 2021 11:03:58 +0800 Subject: [PATCH] bReader.Server: imporved db behavior. better ui function, minor fix. --- bReader.Server/Program.cs | 30 ++++++++++++++----- bReader.Server/Services/SettingService.cs | 2 +- .../Components/AppSettingPanel.razor | 5 ---- bReader.Shared/Components/NavMenu.razor | 6 ++-- bReader.Shared/Pages/Settings.razor | 4 +-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bReader.Server/Program.cs b/bReader.Server/Program.cs index fc7704d..a173800 100644 --- a/bReader.Server/Program.cs +++ b/bReader.Server/Program.cs @@ -1,11 +1,11 @@ +using bReader.Server.Data; +using bReader.Shared; +using bReader.Shared.Services; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace bReader.Server { @@ -13,9 +13,15 @@ public class Program { public static void Main(string[] args) { - var host = CreateHostBuilder(args); - - host.Build().Run(); + var host = CreateHostBuilder(args).Build(); + using (var scope = host.Services.CreateScope()) + { + using var db = scope.ServiceProvider.GetRequiredService>().CreateDbContext(); + db.Database.Migrate(); + var set = scope.ServiceProvider.GetRequiredService(); + InitSettings(set); + } + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => @@ -24,5 +30,13 @@ public static IHostBuilder CreateHostBuilder(string[] args) => { webBuilder.UseStartup(); }); + + protected static void InitSettings(ISettingService settingService) + { + Dictionary dic = settingService.GetSettingsAsync().Result; + dic.AddIfNone("SourceUpdatePeriod", "60"); + + settingService.SaveSettingsAsync(dic); + } } } diff --git a/bReader.Server/Services/SettingService.cs b/bReader.Server/Services/SettingService.cs index cd180e9..9ccf2fb 100644 --- a/bReader.Server/Services/SettingService.cs +++ b/bReader.Server/Services/SettingService.cs @@ -31,7 +31,7 @@ public async Task SaveSettingsAsync(Dictionary settings) using var context = _factory.CreateDbContext(); //use a lookuptable exchanging space with time var lookup = await context.Settings.AsNoTracking().ToDictionaryAsync(x => x.Key, x => x.Id); - var sets = settings.Select(x => new AppSetting() { Id = lookup[x.Key], Key = x.Key, Value = x.Value }); + var sets = settings.Select(x => new AppSetting() { Id = lookup.ContainsKey(x.Key)?lookup[x.Key]:0, Key = x.Key, Value = x.Value }); context.UpdateRange(sets); this.cached = settings;// updated cached data return await context.SaveChangesAsync() > 0; diff --git a/bReader.Shared/Components/AppSettingPanel.razor b/bReader.Shared/Components/AppSettingPanel.razor index 03bbfd3..fe5e4c4 100644 --- a/bReader.Shared/Components/AppSettingPanel.razor +++ b/bReader.Shared/Components/AppSettingPanel.razor @@ -10,7 +10,6 @@ protected async override Task OnInitializedAsync() { this.AppSettings = await SettingService.GetSettingsAsync(); - InitDic(AppSettings); await base.OnInitializedAsync(); } protected async Task SaveSettingsAsync() @@ -18,8 +17,4 @@ saveSuccess = await SettingService.SaveSettingsAsync(AppSettings); savecomplete = true; } - private void InitDic(Dictionary dic) - { - this.AppSettings.AddIfNone("SourceUpdatePeriod", "60"); - } } diff --git a/bReader.Shared/Components/NavMenu.razor b/bReader.Shared/Components/NavMenu.razor index d2b3804..e2b8964 100644 --- a/bReader.Shared/Components/NavMenu.razor +++ b/bReader.Shared/Components/NavMenu.razor @@ -9,8 +9,10 @@