Skip to content

Commit

Permalink
Fix update operation (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMacocian authored Nov 25, 2023
1 parent 4bd094b commit c72375e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions GuildWarsPartySearch/Services/Database/TableStorageDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public async Task<bool> 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;
Expand All @@ -95,9 +97,23 @@ public async Task<bool> SetPartySearches(Campaign campaign, Continent continent,
};
});

var actions = new List<TableTransactionAction>();
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}");
Expand Down

0 comments on commit c72375e

Please sign in to comment.