private async Task AddSystemUser(ResourcesContext context, CancellationToken token) { await context.Users.AddAsync(User.GetSystemUser(), token); await context.AddOrUpdateSetting(SystemUserSettingKey, "true"); await context.SaveChangesAsync(token); }
public static async Task AddOrUpdateSetting(this ResourcesContext context, string key, string value) { var settingValue = await context.Settings.FirstOrDefaultAsync(it => it.Key == key); if (settingValue == null) { await context.Settings.AddAsync(new KeyValue { Key = key, Value = value }); } else { settingValue.Value = value; } }
private async Task Seed(ResourcesContext context, CancellationToken token) { var resourcesPath = Path.GetFullPath("data/resources.json"); if (File.Exists(resourcesPath)) { var json = JObject.Parse(System.IO.File.ReadAllText(resourcesPath)); var groups = json.SelectToken("groups").Select(g => g.ToObject <Models.ResourcesGroup>()).ToList(); var existingGroups = context.Groups.Include(it => it.Resources).ToList(); var existingResources = existingGroups.SelectMany(it => it.Resources).ToList(); foreach (var group in groups) { if (!existingGroups.Contains(group)) { Log.Information("Seeding data: add group {@group}", group.Name); await context.AddAsync(group, token); } if (group.Resources != null) { foreach (var resource in group.Resources) { if (!existingResources.Contains(resource)) { Log.Information("Seeding data: add resource {@resource}", resource.Name); await context.AddAsync(resource, token); } } } } await context.AddOrUpdateSetting(DataSeededSettingKey, "true"); await context.SaveChangesAsync(token); } }
public static async Task <User> GetOrAdd(this ResourcesContext context, ClaimsPrincipal currentUser) { var user = await context.Users.FindAsync(currentUser.Identity.Name); var avatar = currentUser.GetAvatarUrl(); if (user == null) { user = new User { Login = currentUser.Identity.Name, AvatarUrl = currentUser.GetAvatarUrl() }; await context.AddAsync(user); } else if (user.AvatarUrl != avatar) { user.AvatarUrl = avatar; } return(user); }
public static async Task <string> GetSetting(this ResourcesContext context, string key) { var setting = await context.Settings.FirstOrDefaultAsync(it => it.Key == key); return(setting?.Value); }