diff --git a/Program.cs b/Program.cs index fc2de84..9b782c0 100644 --- a/Program.cs +++ b/Program.cs @@ -268,29 +268,44 @@ private static async Task ProcessRewards() try { + var records = new List(); + + // Read the existing rewards data using (var reader = new StreamReader(_rewardsCsvPath)) using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture))) { - var records = csv.GetRecords().ToList(); + records = csv.GetRecords().ToList(); + } - foreach (var record in records) + foreach (var record in records) + { + if (record.RewardName == "recuerdate") { - if (record.RewardName == "recuerdate") + if (int.TryParse(record.Quantity, out int quantity)) { - if (int.TryParse(record.Quantity, out int quantity)) + var imagesSent = 0; + for (int i = 0; i < quantity; i++) { - for (int i = 0; i < quantity; i++) - { - await PostRandomImageUrl(); - } - } - else - { - Console.WriteLine($"Invalid Quantity value for record with RewardName '{record.RewardName}'."); + await PostRandomImageUrl(); + imagesSent++; } + + // Update the Quantity field + record.Quantity = (quantity - imagesSent).ToString(); + } + else + { + Console.WriteLine($"Invalid Quantity value for record with RewardName '{record.RewardName}'."); } } } + + // Write the updated rewards data back to the CSV file + using (var writer = new StreamWriter(_rewardsCsvPath)) + using (var csv = new CsvWriter(writer, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture))) + { + csv.WriteRecords(records); + } } catch (Exception ex) {