Skip to content

Commit

Permalink
Fix problem of save widget
Browse files Browse the repository at this point in the history
  • Loading branch information
OmidID committed Sep 30, 2020
1 parent b1fd113 commit b198a6a
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/DotNetBlog.Core/Service/WidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ public class WidgetService
{
private static readonly string CacheKey = "Cache_Widget";

//private static readonly Dictionary<WidgetType, string> DefaultNames = new Dictionary<WidgetType, string>
//{
// { WidgetType.Administration, "管理" },
// { WidgetType.Category, "分类" },
// { WidgetType.RecentComment, "最新评论" },
// { WidgetType.MonthStatistics, "归档" },
// { WidgetType.Page, "页面" },
// { WidgetType.Search, "搜索" },
// { WidgetType.Tag, "标签" },
// { WidgetType.RecentTopic, "最新文章" },
// { WidgetType.Link, "友情链接" }
//};

private static readonly Dictionary<WidgetType, Type> DefaultWidgetConfigTypes = new Dictionary<WidgetType, Type>
{
{ WidgetType.Administration, typeof(AdministrationWidgetConfigModel) },
Expand Down Expand Up @@ -109,27 +96,27 @@ public async Task<List<WidgetModel>> Query()

public async Task<OperationResult> Save(List<WidgetModel> 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)
Expand Down

0 comments on commit b198a6a

Please sign in to comment.