From 70aaad38e5a81c502521ce185325004c19d3314f Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Sun, 5 Aug 2018 16:20:19 -0500 Subject: [PATCH] minor cleanup --- src/FilterLists.Agent/Program.cs | 48 +++++++-------- .../V1/Controllers/ListsController.cs | 8 +-- .../V1/Controllers/RulesController.cs | 6 +- src/FilterLists.Data/Entities/BaseEntity.cs | 2 +- src/FilterLists.Data/Entities/IBaseEntity.cs | 9 +++ ...{BaseJunction.cs => BaseJunctionEntity.cs} | 2 +- .../Entities/Junctions/FilterListLanguage.cs | 2 +- .../Junctions/FilterListMaintainer.cs | 2 +- .../Entities/Junctions/Fork.cs | 2 +- .../Entities/Junctions/Merge.cs | 2 +- .../Entities/Junctions/SnapshotRule.cs | 2 +- .../Entities/Junctions/SoftwareSyntax.cs | 2 +- .../BaseJunctionTypeConfiguration.cs | 2 +- src/FilterLists.Data/FilterListsDbContext.cs | 60 ++++++++----------- .../Extensions/SeedFilterListsDbContext.cs | 24 ++++---- .../Snapshot/SnapshotBatchDe.cs | 32 +++++----- .../Snapshot/SnapshotDe.cs | 46 +++++++------- 17 files changed, 122 insertions(+), 129 deletions(-) create mode 100644 src/FilterLists.Data/Entities/IBaseEntity.cs rename src/FilterLists.Data/Entities/Junctions/{BaseJunction.cs => BaseJunctionEntity.cs} (72%) diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index b78ca91c4..adeb5d8fb 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -12,59 +12,53 @@ namespace FilterLists.Agent public static class Program { private const int BatchSize = 1; - private static TelemetryClient _telemetryClient; - private static ServiceProvider _serviceProvider; - private static IConfigurationRoot _configurationRoot; + private const string AiConfigSetting = "ApplicationInsights:InstrumentationKey"; + private static IConfigurationRoot configurationRoot; + private static ServiceProvider serviceProvider; + private static TelemetryClient telemetryClient; - public static int Main() + public static void Main() { - InstantiateConfigurationRoot(); + BuildConfigurationRoot(); InstantiateTelemetryClient(); - InstantiateServiceProvider(); + BuildServiceProvider(); CaptureSnapshots(BatchSize); - return 0; } - private static void InstantiateConfigurationRoot() + private static void BuildConfigurationRoot() { - _configurationRoot = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", false, true) - .Build(); + configurationRoot = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", true) + .Build(); } private static void InstantiateTelemetryClient() { - TelemetryConfiguration.Active.InstrumentationKey = - _configurationRoot["ApplicationInsights:InstrumentationKey"]; - _telemetryClient = new TelemetryClient(); + TelemetryConfiguration.Active.InstrumentationKey = configurationRoot[AiConfigSetting]; + telemetryClient = new TelemetryClient(); } - private static void InstantiateServiceProvider() + private static void BuildServiceProvider() { var serviceCollection = new ServiceCollection(); - ConfigureServices(serviceCollection); - _serviceProvider = serviceCollection.BuildServiceProvider(); - } - - private static void ConfigureServices(IServiceCollection serviceCollection) - { - serviceCollection.AddFilterListsServices(_configurationRoot); + serviceCollection.AddFilterListsServices(configurationRoot); + serviceProvider = serviceCollection.BuildServiceProvider(); } private static void CaptureSnapshots(int batchSize) { - var snapshotService = _serviceProvider.GetService(); + var snapshotService = serviceProvider.GetService(); Log("Capturing FilterList snapshots..."); snapshotService.CaptureAsync(batchSize).Wait(); - Log("\nSnapshots captured."); - _telemetryClient.Flush(); + Log(Environment.NewLine + "Snapshots captured."); + telemetryClient.Flush(); } private static void Log(string message) { Console.WriteLine(message); - _telemetryClient.TrackTrace(message); + telemetryClient.TrackTrace(message); } } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ListsController.cs b/src/FilterLists.Api/V1/Controllers/ListsController.cs index 310c568e7..3d96c79e1 100644 --- a/src/FilterLists.Api/V1/Controllers/ListsController.cs +++ b/src/FilterLists.Api/V1/Controllers/ListsController.cs @@ -9,20 +9,20 @@ namespace FilterLists.Api.V1.Controllers { public class ListsController : BaseController { - private readonly FilterListService _filterListService; + private readonly FilterListService filterListService; public ListsController(SeedService seedService, FilterListService filterListService) : base(seedService) { - _filterListService = filterListService; + this.filterListService = filterListService; } [HttpGet] - public async Task Index() => Json(await _filterListService.GetAllSummariesAsync()); + public async Task Index() => Json(await filterListService.GetAllSummariesAsync()); [HttpGet] [Route("{id}")] //TODO: respond with appropriate exception if negative id queried - public async Task GetById(int id) => Json(await _filterListService.GetDetailsAsync((uint)id)); + public async Task GetById(int id) => Json(await filterListService.GetDetailsAsync((uint)id)); [HttpGet("seed")] public async Task Seed() => Json(await SeedService.GetAllAsync()); diff --git a/src/FilterLists.Api/V1/Controllers/RulesController.cs b/src/FilterLists.Api/V1/Controllers/RulesController.cs index b1037c0cc..fb1cb8e37 100644 --- a/src/FilterLists.Api/V1/Controllers/RulesController.cs +++ b/src/FilterLists.Api/V1/Controllers/RulesController.cs @@ -6,14 +6,14 @@ namespace FilterLists.Api.V1.Controllers { public class RulesController : BaseController { - private readonly RuleService _ruleService; + private readonly RuleService ruleService; public RulesController(RuleService ruleService) { - _ruleService = ruleService; + this.ruleService = ruleService; } [HttpGet] - public async Task Index() => Json(await _ruleService.GetCountAll()); + public async Task Index() => Json(await ruleService.GetCountAll()); } } \ No newline at end of file diff --git a/src/FilterLists.Data/Entities/BaseEntity.cs b/src/FilterLists.Data/Entities/BaseEntity.cs index e90a99a87..731e6c3c3 100644 --- a/src/FilterLists.Data/Entities/BaseEntity.cs +++ b/src/FilterLists.Data/Entities/BaseEntity.cs @@ -2,7 +2,7 @@ namespace FilterLists.Data.Entities { - public class BaseEntity + public class BaseEntity : IBaseEntity { public uint Id { get; set; } public DateTime CreatedDateUtc { get; set; } diff --git a/src/FilterLists.Data/Entities/IBaseEntity.cs b/src/FilterLists.Data/Entities/IBaseEntity.cs new file mode 100644 index 000000000..698faad3f --- /dev/null +++ b/src/FilterLists.Data/Entities/IBaseEntity.cs @@ -0,0 +1,9 @@ +using System; + +namespace FilterLists.Data.Entities +{ + public interface IBaseEntity + { + DateTime CreatedDateUtc { get; set; } + } +} \ No newline at end of file diff --git a/src/FilterLists.Data/Entities/Junctions/BaseJunction.cs b/src/FilterLists.Data/Entities/Junctions/BaseJunctionEntity.cs similarity index 72% rename from src/FilterLists.Data/Entities/Junctions/BaseJunction.cs rename to src/FilterLists.Data/Entities/Junctions/BaseJunctionEntity.cs index 6f45b4f98..1c0c0cc9f 100644 --- a/src/FilterLists.Data/Entities/Junctions/BaseJunction.cs +++ b/src/FilterLists.Data/Entities/Junctions/BaseJunctionEntity.cs @@ -2,7 +2,7 @@ namespace FilterLists.Data.Entities.Junctions { - public class BaseJunction + public class BaseJunctionEntity : IBaseEntity { public DateTime CreatedDateUtc { get; set; } } diff --git a/src/FilterLists.Data/Entities/Junctions/FilterListLanguage.cs b/src/FilterLists.Data/Entities/Junctions/FilterListLanguage.cs index 02bb1ce64..bda2a0ff2 100644 --- a/src/FilterLists.Data/Entities/Junctions/FilterListLanguage.cs +++ b/src/FilterLists.Data/Entities/Junctions/FilterListLanguage.cs @@ -1,6 +1,6 @@ namespace FilterLists.Data.Entities.Junctions { - public class FilterListLanguage : BaseJunction + public class FilterListLanguage : BaseJunctionEntity { public uint FilterListId { get; set; } public FilterList FilterList { get; set; } diff --git a/src/FilterLists.Data/Entities/Junctions/FilterListMaintainer.cs b/src/FilterLists.Data/Entities/Junctions/FilterListMaintainer.cs index c9a5518e4..67922e7ac 100644 --- a/src/FilterLists.Data/Entities/Junctions/FilterListMaintainer.cs +++ b/src/FilterLists.Data/Entities/Junctions/FilterListMaintainer.cs @@ -1,6 +1,6 @@ namespace FilterLists.Data.Entities.Junctions { - public class FilterListMaintainer : BaseJunction + public class FilterListMaintainer : BaseJunctionEntity { public uint FilterListId { get; set; } public FilterList FilterList { get; set; } diff --git a/src/FilterLists.Data/Entities/Junctions/Fork.cs b/src/FilterLists.Data/Entities/Junctions/Fork.cs index 13c49736e..dca5c67ac 100644 --- a/src/FilterLists.Data/Entities/Junctions/Fork.cs +++ b/src/FilterLists.Data/Entities/Junctions/Fork.cs @@ -1,6 +1,6 @@ namespace FilterLists.Data.Entities.Junctions { - public class Fork : BaseJunction + public class Fork : BaseJunctionEntity { public uint ForkFilterListId { get; set; } public FilterList ForkFilterList { get; set; } diff --git a/src/FilterLists.Data/Entities/Junctions/Merge.cs b/src/FilterLists.Data/Entities/Junctions/Merge.cs index f02e7ec53..997ea1824 100644 --- a/src/FilterLists.Data/Entities/Junctions/Merge.cs +++ b/src/FilterLists.Data/Entities/Junctions/Merge.cs @@ -1,6 +1,6 @@ namespace FilterLists.Data.Entities.Junctions { - public class Merge : BaseJunction + public class Merge : BaseJunctionEntity { public uint MergeFilterListId { get; set; } public FilterList MergeFilterList { get; set; } diff --git a/src/FilterLists.Data/Entities/Junctions/SnapshotRule.cs b/src/FilterLists.Data/Entities/Junctions/SnapshotRule.cs index d42bd0484..634d0f61a 100644 --- a/src/FilterLists.Data/Entities/Junctions/SnapshotRule.cs +++ b/src/FilterLists.Data/Entities/Junctions/SnapshotRule.cs @@ -2,7 +2,7 @@ namespace FilterLists.Data.Entities.Junctions { - public class SnapshotRule : BaseJunction + public class SnapshotRule : BaseJunctionEntity { public DateTime ModifiedDateUtc { get; set; } public uint AddedBySnapshotId { get; set; } diff --git a/src/FilterLists.Data/Entities/Junctions/SoftwareSyntax.cs b/src/FilterLists.Data/Entities/Junctions/SoftwareSyntax.cs index c1cdc7f1f..2520a8b94 100644 --- a/src/FilterLists.Data/Entities/Junctions/SoftwareSyntax.cs +++ b/src/FilterLists.Data/Entities/Junctions/SoftwareSyntax.cs @@ -1,6 +1,6 @@ namespace FilterLists.Data.Entities.Junctions { - public class SoftwareSyntax : BaseJunction + public class SoftwareSyntax : BaseJunctionEntity { public uint SoftwareId { get; set; } public Software Software { get; set; } diff --git a/src/FilterLists.Data/EntityTypeConfigurations/Junctions/BaseJunctionTypeConfiguration.cs b/src/FilterLists.Data/EntityTypeConfigurations/Junctions/BaseJunctionTypeConfiguration.cs index 959847b01..a162c3237 100644 --- a/src/FilterLists.Data/EntityTypeConfigurations/Junctions/BaseJunctionTypeConfiguration.cs +++ b/src/FilterLists.Data/EntityTypeConfigurations/Junctions/BaseJunctionTypeConfiguration.cs @@ -5,7 +5,7 @@ namespace FilterLists.Data.EntityTypeConfigurations.Junctions { public class BaseJunctionTypeConfiguration : IEntityTypeConfiguration - where TJunction : BaseJunction + where TJunction : BaseJunctionEntity { public virtual void Configure(EntityTypeBuilder entityTypeBuilder) { diff --git a/src/FilterLists.Data/FilterListsDbContext.cs b/src/FilterLists.Data/FilterListsDbContext.cs index 58db3ca61..1b592cc0c 100644 --- a/src/FilterLists.Data/FilterListsDbContext.cs +++ b/src/FilterLists.Data/FilterListsDbContext.cs @@ -12,27 +12,6 @@ public FilterListsDbContext(DbContextOptions options) : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - ApplyConfigurationsEntities(modelBuilder); - ApplyConfigurationsJunctions(modelBuilder); - } - - #region Entities - - private static void ApplyConfigurationsEntities(ModelBuilder modelBuilder) - { - modelBuilder.ApplyConfiguration(new FilterListTypeConfiguration()); - modelBuilder.ApplyConfiguration(new LanguageTypeConfiguration()); - modelBuilder.ApplyConfiguration(new LicenseTypeConfiguration()); - modelBuilder.ApplyConfiguration(new MaintainerTypeConfiguration()); - modelBuilder.ApplyConfiguration(new RuleTypeConfiguration()); - modelBuilder.ApplyConfiguration(new SnapshotTypeConfiguration()); - modelBuilder.ApplyConfiguration(new SoftwareTypeConfiguration()); - modelBuilder.ApplyConfiguration(new SyntaxTypeConfiguration()); - } - public DbSet FilterLists { get; set; } public DbSet Languages { get; set; } public DbSet Licenses { get; set; } @@ -42,20 +21,6 @@ private static void ApplyConfigurationsEntities(ModelBuilder modelBuilder) public DbSet Software { get; set; } public DbSet Syntaxes { get; set; } - #endregion - - #region Junctions - - private static void ApplyConfigurationsJunctions(ModelBuilder modelBuilder) - { - modelBuilder.ApplyConfiguration(new FilterListLanguageTypeConfiguration()); - modelBuilder.ApplyConfiguration(new FilterListMaintainerTypeConfiguration()); - modelBuilder.ApplyConfiguration(new ForkTypeConfiguration()); - modelBuilder.ApplyConfiguration(new MergeTypeConfiguration()); - modelBuilder.ApplyConfiguration(new SnapshotRuleTypeConfiguration()); - modelBuilder.ApplyConfiguration(new SoftwareSyntaxTypeConfiguration()); - } - public DbSet FilterListLanguages { get; set; } public DbSet FilterListMaintainers { get; set; } public DbSet Forks { get; set; } @@ -63,6 +28,29 @@ private static void ApplyConfigurationsJunctions(ModelBuilder modelBuilder) public DbSet SnapshotRules { get; set; } public DbSet SoftwareSyntaxes { get; set; } - #endregion + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + ApplyConfigurations(modelBuilder); + } + + private static void ApplyConfigurations(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfiguration(new FilterListTypeConfiguration()); + modelBuilder.ApplyConfiguration(new LanguageTypeConfiguration()); + modelBuilder.ApplyConfiguration(new LicenseTypeConfiguration()); + modelBuilder.ApplyConfiguration(new MaintainerTypeConfiguration()); + modelBuilder.ApplyConfiguration(new RuleTypeConfiguration()); + modelBuilder.ApplyConfiguration(new SnapshotTypeConfiguration()); + modelBuilder.ApplyConfiguration(new SoftwareTypeConfiguration()); + modelBuilder.ApplyConfiguration(new SyntaxTypeConfiguration()); + + modelBuilder.ApplyConfiguration(new FilterListLanguageTypeConfiguration()); + modelBuilder.ApplyConfiguration(new FilterListMaintainerTypeConfiguration()); + modelBuilder.ApplyConfiguration(new ForkTypeConfiguration()); + modelBuilder.ApplyConfiguration(new MergeTypeConfiguration()); + modelBuilder.ApplyConfiguration(new SnapshotRuleTypeConfiguration()); + modelBuilder.ApplyConfiguration(new SoftwareSyntaxTypeConfiguration()); + } } } \ No newline at end of file diff --git a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs index 570fc16a4..a25e57a13 100644 --- a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs +++ b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs @@ -27,12 +27,12 @@ public static void SeedOrUpdate(this FilterListsDbContext dbContext, string data dbContext.InsertOnDuplicateKeyUpdate(dataPath); } - private static void InsertOnDuplicateKeyUpdate(this DbContext dbContext, string dataPath) - where TEntityType : class + private static void InsertOnDuplicateKeyUpdate(this DbContext dbContext, string dataPath) + where TEntity : IBaseEntity { - var entityType = dbContext.Model.FindEntityType(typeof(TEntityType)); + var entityType = dbContext.Model.FindEntityType(typeof(TEntity)); var properties = GetPropertiesLessValueGeneratedTimestamps(entityType); - var values = CreateValues(properties, dataPath); + var values = CreateValues(properties, dataPath); if (values == "") return; var columns = string.Join(", ", properties.Select(x => x.Name)); var updates = CreateUpdates(properties); @@ -50,28 +50,30 @@ private static List GetPropertiesLessValueGeneratedTimestamps(IEntity .ToList(); } - private static string CreateValues(IReadOnlyCollection properties, string dataPath) + private static string CreateValues(IReadOnlyCollection properties, string dataPath) + where TEntity : IBaseEntity { - return GetSeedRows(dataPath) + return GetSeedRows(dataPath) .Select(row => CreateRowValues(properties, row)) .Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues); } - private static List GetSeedRows(string dataPath) + private static List GetSeedRows(string dataPath) where TEntity : IBaseEntity { try { - return JsonConvert.DeserializeObject>( - File.ReadAllText(dataPath + Path.DirectorySeparatorChar + typeof(TEntityType).Name + ".json")); + return JsonConvert.DeserializeObject>( + File.ReadAllText(dataPath + Path.DirectorySeparatorChar + typeof(TEntity).Name + ".json")); } catch (FileNotFoundException e) { Console.WriteLine(e.Message); - return new List(); + return new List(); } } - private static string CreateRowValues(IEnumerable properties, TEntityType row) + private static string CreateRowValues(IEnumerable properties, TEntity row) + where TEntity : IBaseEntity { return (from property in properties let value = row.GetType().GetProperty(property.Name)?.GetValue(row) diff --git a/src/FilterLists.Services/Snapshot/SnapshotBatchDe.cs b/src/FilterLists.Services/Snapshot/SnapshotBatchDe.cs index 2985e70ec..05a53034d 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotBatchDe.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotBatchDe.cs @@ -10,43 +10,43 @@ namespace FilterLists.Services.Snapshot { public class SnapshotBatchDe { - private readonly FilterListsDbContext _dbContext; - private readonly IEnumerable _rawRules; - private readonly Data.Entities.Snapshot _snapshot; - private IQueryable _rules; + private readonly FilterListsDbContext dbContext; + private readonly IEnumerable rawRules; + private readonly Data.Entities.Snapshot snapshot; + private IQueryable rules; public SnapshotBatchDe(FilterListsDbContext dbContext, Data.Entities.Snapshot snapshot, IEnumerable rawRules) { - _dbContext = dbContext; - _snapshot = snapshot; - _rawRules = rawRules; + this.dbContext = dbContext; + this.snapshot = snapshot; + this.rawRules = rawRules; } public async Task SaveSnapshotBatchAsync() { AddRules(); AddSnapshotRules(); - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); } private void AddRules() { - var existingRules = _dbContext.Rules.Where(rule => _rawRules.Contains(rule.Raw)); - var newRawRules = _rawRules.Except(existingRules.Select(r => r.Raw)); + var existingRules = dbContext.Rules.Where(rule => rawRules.Contains(rule.Raw)); + var newRawRules = rawRules.Except(existingRules.Select(r => r.Raw)); var newRules = newRawRules.Select(newRawRule => new Rule {Raw = newRawRule}).ToList(); - _dbContext.Rules.AddRange(newRules); - _rules = existingRules.Concat(newRules); + dbContext.Rules.AddRange(newRules); + rules = existingRules.Concat(newRules); } private void AddSnapshotRules() { var snapshotRules = new List(); - foreach (var rule in _rules) + foreach (var rule in rules) snapshotRules.Add(new SnapshotRule {Rule = rule}); - if (_snapshot.AddedSnapshotRules == null) - _snapshot.AddedSnapshotRules = new List(); - _snapshot.AddedSnapshotRules.AddRange(snapshotRules); + if (snapshot.AddedSnapshotRules == null) + snapshot.AddedSnapshotRules = new List(); + snapshot.AddedSnapshotRules.AddRange(snapshotRules); } } } \ No newline at end of file diff --git a/src/FilterLists.Services/Snapshot/SnapshotDe.cs b/src/FilterLists.Services/Snapshot/SnapshotDe.cs index fac26e3e4..a8dfcf46f 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotDe.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotDe.cs @@ -16,14 +16,14 @@ public class SnapshotDe @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"; private const int BatchSize = 1000; - private readonly FilterListsDbContext _dbContext; - private readonly FilterListViewUrlDto _list; - private Data.Entities.Snapshot _snapshot; + private readonly FilterListsDbContext dbContext; + private readonly FilterListViewUrlDto list; + private Data.Entities.Snapshot snapshot; public SnapshotDe(FilterListsDbContext dbContext, FilterListViewUrlDto list) { - _dbContext = dbContext; - _list = list; + this.dbContext = dbContext; + this.list = list; } public async Task SaveSnapshotAsync() @@ -42,14 +42,14 @@ private async Task CaptureSnapshot() { await AddSnapshot(); var content = await TryGetContent(); - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); return content; } private async Task AddSnapshot() { - _snapshot = new Data.Entities.Snapshot {FilterListId = _list.Id}; - await _dbContext.Snapshots.AddAsync(_snapshot); + snapshot = new Data.Entities.Snapshot {FilterListId = list.Id}; + await dbContext.Snapshots.AddAsync(snapshot); } private async Task TryGetContent() @@ -60,13 +60,13 @@ private async Task TryGetContent() } catch (WebException we) { - _snapshot.HttpStatusCode = ((int) ((HttpWebResponse) we.Response).StatusCode).ToString(); + snapshot.HttpStatusCode = ((int) ((HttpWebResponse) we.Response).StatusCode).ToString(); return null; } catch (Exception) { //TODO: log exception (#148) - _snapshot.HttpStatusCode = null; + snapshot.HttpStatusCode = null; return null; } } @@ -76,9 +76,9 @@ private async Task GetContent() using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgentString); - using (var httpResponseMessage = await httpClient.GetAsync(_list.ViewUrl)) + using (var httpResponseMessage = await httpClient.GetAsync(list.ViewUrl)) { - _snapshot.HttpStatusCode = ((int) httpResponseMessage.StatusCode).ToString(); + snapshot.HttpStatusCode = ((int) httpResponseMessage.StatusCode).ToString(); if (httpResponseMessage.IsSuccessStatusCode) return await httpResponseMessage.Content.ReadAsStringAsync(); } @@ -105,7 +105,7 @@ private static IEnumerable GetRawRules(string content) private IEnumerable GetSnapshotBatches(IEnumerable rawRules) { return rawRules.Batch(BatchSize) - .Select(rawRuleBatch => new SnapshotBatchDe(_dbContext, _snapshot, rawRuleBatch)); + .Select(rawRuleBatch => new SnapshotBatchDe(dbContext, snapshot, rawRuleBatch)); } private static async Task SaveSnapshotBatches(IEnumerable snapshotBatches) @@ -119,36 +119,36 @@ private async Task DedupSnapshotRules() var existingSnapshotRules = GetExistingSnapshotRules(); UpdateRemovedSnapshotRules(existingSnapshotRules); RemoveDuplicateSnapshotRules(existingSnapshotRules); - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); } private IQueryable GetExistingSnapshotRules() { - return _dbContext.SnapshotRules.Where(sr => - sr.AddedBySnapshot.FilterListId == _list.Id && sr.AddedBySnapshot != _snapshot && + return dbContext.SnapshotRules.Where(sr => + sr.AddedBySnapshot.FilterListId == list.Id && sr.AddedBySnapshot != snapshot && sr.RemovedBySnapshot == null); } private void UpdateRemovedSnapshotRules(IQueryable existingSnapshotRules) { - var newSnapshotRules = _dbContext.SnapshotRules.Where(sr => sr.AddedBySnapshot == _snapshot); + var newSnapshotRules = dbContext.SnapshotRules.Where(sr => sr.AddedBySnapshot == snapshot); var removedSnapshotRules = existingSnapshotRules.Where(sr => !newSnapshotRules.Any(nsr => nsr.Rule == sr.Rule)); - removedSnapshotRules.ToList().ForEach(sr => sr.RemovedBySnapshot = _snapshot); + removedSnapshotRules.ToList().ForEach(sr => sr.RemovedBySnapshot = snapshot); } private void RemoveDuplicateSnapshotRules(IQueryable existingSnapshotRules) { - var duplicateSnapshotRules = _dbContext.SnapshotRules.Where(sr => - sr.AddedBySnapshot == _snapshot && + var duplicateSnapshotRules = dbContext.SnapshotRules.Where(sr => + sr.AddedBySnapshot == snapshot && existingSnapshotRules.Any(esr => esr.Rule == sr.Rule)); - _dbContext.SnapshotRules.RemoveRange(duplicateSnapshotRules); + dbContext.SnapshotRules.RemoveRange(duplicateSnapshotRules); } private async Task SetCompleted() { - _snapshot.IsCompleted = true; - await _dbContext.SaveChangesAsync(); + snapshot.IsCompleted = true; + await dbContext.SaveChangesAsync(); } } } \ No newline at end of file