diff --git a/src/DotNetBlog.Core/Service/WidgetService.cs b/src/DotNetBlog.Core/Service/WidgetService.cs index 239b933..fbcbcab 100644 --- a/src/DotNetBlog.Core/Service/WidgetService.cs +++ b/src/DotNetBlog.Core/Service/WidgetService.cs @@ -20,19 +20,6 @@ public class WidgetService { private static readonly string CacheKey = "Cache_Widget"; - //private static readonly Dictionary DefaultNames = new Dictionary - //{ - // { WidgetType.Administration, "管理" }, - // { WidgetType.Category, "分类" }, - // { WidgetType.RecentComment, "最新评论" }, - // { WidgetType.MonthStatistics, "归档" }, - // { WidgetType.Page, "页面" }, - // { WidgetType.Search, "搜索" }, - // { WidgetType.Tag, "标签" }, - // { WidgetType.RecentTopic, "最新文章" }, - // { WidgetType.Link, "友情链接" } - //}; - private static readonly Dictionary DefaultWidgetConfigTypes = new Dictionary { { WidgetType.Administration, typeof(AdministrationWidgetConfigModel) }, @@ -109,27 +96,27 @@ public async Task> Query() public async Task Save(List widgetList) { - using (var tran = await this.BlogContext.Database.BeginTransactionAsync()) + using var tran = await BlogContext.Database.BeginTransactionAsync(); + var entityList = await BlogContext.Widgets.ToListAsync(); + + BlogContext.RemoveRange(entityList); + await BlogContext.SaveChangesAsync(); + + entityList = widgetList.Select(t => new Widget { - var entityList = await this.BlogContext.Widgets.ToListAsync(); - this.BlogContext.RemoveRange(entityList); - await this.BlogContext.SaveChangesAsync(); + Type = t.Type, + Id = widgetList.IndexOf(t) + 1, + Config = JsonConvert.SerializeObject(t.Config) + }).ToList(); - entityList = widgetList.Select(t => new Widget - { - Type = t.Type, - Id = widgetList.IndexOf(t) + 1, - Config = JsonConvert.SerializeObject(t.Config) - }).ToList(); - this.BlogContext.AddRange(entityList); - await this.BlogContext.SaveChangesAsync(); + await BlogContext.AddRangeAsync(entityList); + await BlogContext.SaveChangesAsync(); - tran.Commit(); + await tran.CommitAsync(); - RemoveCache(); + RemoveCache(); - return new OperationResult(); - } + return new OperationResult(); } public WidgetConfigModelBase Transform(WidgetType type, JObject config)