public static Transaction FromString()

in Hands-on lab/lab-files/TransactionGenerator/Transaction.cs [99:162]


        public static Transaction FromString(string line, string header)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                throw new ArgumentException($"{nameof(line)} cannot be null, empty, or only whitespace");
            }

            var tokens = line.Split(',');
            if (tokens.Length != 28)
            {
                throw new ArgumentException($"Invalid record: {line}");
            }

            var tx = new Transaction
            {
                CsvString = line,
                CsvHeader = header
            };
            try
            {
                // Generate a random Cosmos DB document ID to allow for re-inserting the same data each time the generator runs.
                tx.id = Guid.NewGuid().ToString();
                tx.TransactionID = tokens[0];
                tx.AccountID = tokens[1];
                tx.TransactionAmountUSD = double.TryParse(tokens[2], out var dresult) ? dresult : 0.0;
                tx.TransactionAmount = double.TryParse(tokens[3], out dresult) ? dresult : 0.0;
                tx.TransactionCurrencyCode = tokens[4];
                tx.LocalHour = int.TryParse(tokens[5], out var iresult) ? iresult : 0;
                tx.TransactionIPaddress = tokens[6];
                tx.IpState = tokens[7];
                tx.IpPostcode = tokens[8];
                tx.IpCountryCode = string.IsNullOrWhiteSpace(tokens[9]) ? "unk" : tokens[9];
                tx.IsProxyIP = bool.TryParse(tokens[10], out var bresult) && bresult;
                tx.BrowserLanguage = tokens[11];
                tx.PaymentInstrumentType = tokens[12];
                tx.CardType = tokens[13];
                tx.PaymentBillingPostalCode = tokens[14];
                tx.PaymentBillingState = tokens[15];
                tx.PaymentBillingCountryCode = tokens[16];
                tx.CvvVerifyResult = tokens[17];
                tx.DigitalItemCount = int.TryParse(tokens[18], out iresult) ? iresult : 0;
                tx.PhysicalItemCount = int.TryParse(tokens[19], out iresult) ? iresult : 0;
                tx.AccountPostalCode = tokens[20];
                tx.AccountState = tokens[21];
                tx.AccountCountry = tokens[22];
                tx.AccountAge = int.TryParse(tokens[23], out iresult) ? iresult : 0;
                tx.IsUserRegistered = bool.TryParse(tokens[24], out bresult) && bresult;
                tx.PaymentInstrumentAgeInAccount = double.TryParse(tokens[25], out dresult) ? dresult : 0;
                tx.NumPaymentRejects1dPerUser = int.TryParse(tokens[26], out iresult) ? iresult : 0;
                tx.TransactionDateTime = DateTime.TryParse(tokens[27], out var dtresult) ? dtresult : DateTime.UtcNow;

                // Set the TTL value to 60 days, which deletes the document in Cosmos DB after around two months, saving
                // storage costs while meeting Woodgrove Bank's requirement to keep the streaming data available for
                // that amount of time so they can reprocess or query the raw data within the collection as needed.
                // TODO 7: Complete the code to set the time to live (TTL) value to 60 days (in seconds).
                // COMPLETE THIS CODE ... tx.TimeToLive = ...

                return tx;
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Invalid record: {line}", ex);
            }
        }