From ff6dd14eb15399130088746d583bed9ef82f13b9 Mon Sep 17 00:00:00 2001 From: Winnie Date: Thu, 5 Sep 2024 17:26:56 +0200 Subject: [PATCH] Update the csv accordingly --- Program.cs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) 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) {