diff --git a/GuildWarsPartySearch/Services/Database/TableStorageDatabase.cs b/GuildWarsPartySearch/Services/Database/TableStorageDatabase.cs index 7bc6c14..c274991 100644 --- a/GuildWarsPartySearch/Services/Database/TableStorageDatabase.cs +++ b/GuildWarsPartySearch/Services/Database/TableStorageDatabase.cs @@ -75,6 +75,8 @@ public async Task SetPartySearches(Campaign campaign, Continent continent, var scopedLogger = this.logger.CreateScopedLogger(nameof(this.SetPartySearches), partitionKey); try { + scopedLogger.LogInformation("Retrieving existing entries"); + var existingEntries = await this.GetPartySearches(campaign, continent, region, map, district, cancellationToken); var entries = partySearch.Select(e => { var rowKey = e.CharName ?? string.Empty; @@ -95,9 +97,23 @@ public async Task SetPartySearches(Campaign campaign, Continent continent, }; }); + var actions = new List(); + if (existingEntries is not null) + { + scopedLogger.LogInformation("Patching nonexisting entries"); + // Find all existing entries that don't exist in the update. For those, queue a delete transaction + actions.AddRange(existingEntries + .Where(e => entries.FirstOrDefault(e2 => e.CharName == e2.CharName) is null) + .Select(e => new TableTransactionAction(TableTransactionActionType.Delete, new PartySearchTableEntity + { + PartitionKey = partitionKey, + RowKey = e.CharName ?? string.Empty, + }))); + } + scopedLogger.LogInformation("Batch transaction"); - var transactions = entries.Select(e => new TableTransactionAction(TableTransactionActionType.UpsertReplace, e)); - var responses = await this.tableClient.SubmitTransactionAsync(transactions, cancellationToken); + actions.AddRange(entries.Select(e => new TableTransactionAction(TableTransactionActionType.UpsertReplace, e))); + var responses = await this.tableClient.SubmitTransactionAsync(actions, cancellationToken); foreach(var response in responses.Value) { scopedLogger.LogInformation($"[{response.Status}] {response.ReasonPhrase}");