From 4257aa107c03788c5e30b844b0c4954bb57e8895 Mon Sep 17 00:00:00 2001 From: Thomas Ryan Date: Tue, 13 Aug 2024 12:24:30 -0700 Subject: [PATCH] Fixed the CSV export function on the lookups page. --- .../Controllers/LookupController.cs | 75 +++++++++++++++---- NumberSearch.Mvc/Views/Lookup/Index.cshtml | 23 +++--- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/NumberSearch.Mvc/Controllers/LookupController.cs b/NumberSearch.Mvc/Controllers/LookupController.cs index 0dbe6af8..f5779a08 100644 --- a/NumberSearch.Mvc/Controllers/LookupController.cs +++ b/NumberSearch.Mvc/Controllers/LookupController.cs @@ -55,8 +55,8 @@ public async Task IndexAsync(string dialedNumber) [HttpPost] [ValidateAntiForgeryToken] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - [OutputCache(Duration = 3600, VaryByQueryKeys = new string[] { "dialedNumber", "csv" })] - public async Task BulkPortAsync([Bind("dialedNumber,csv")] string dialedNumber, bool csv) + [OutputCache(Duration = 3600, VaryByQueryKeys = ["dialedNumber"])] + public async Task BulkPortAsync([Bind("dialedNumber")] string dialedNumber) { // Add portable numbers to cart in bulk if (!string.IsNullOrWhiteSpace(dialedNumber)) @@ -92,20 +92,6 @@ await Parallel.ForEachAsync(parsedNumbers, async (number, token) => // Separate wireless numbers out from the rest. var wirelessPortable = results.Where(x => x.Wireless && x.Portable).ToArray(); - if (csv) - { - var builder = new StringBuilder(); - builder.AppendLine("PortedDialedNumber,City,State,DateIngested,Wireless,Portable,LastPorted,SPID,LATA,LEC,LECType,LIDBName,LRN,OCN,Activation"); - foreach (var number in results) - { - builder.AppendLine($"{number.PortedDialedNumber},{number.City},{number.State},{number.DateIngested},{number.Wireless},{number.Portable}," + - $"{number.LrnLookup.LastPorted},{number.LrnLookup.SPID},{number.LrnLookup.LATA},{number.LrnLookup.LEC},{number.LrnLookup.LECType}," + - $"{number.LrnLookup.LIDBName},{number.LrnLookup.LRN},{number.LrnLookup.OCN},{number.LrnLookup.LastPorted}"); - } - - return File(Encoding.UTF8.GetBytes(builder.ToString()), "text/csv", $"AccelerateNetworksPhoneNumbers{DateTime.Now.ToString("yyyyMMddTHHmmss")}.csv"); - } - // Add all the numbers to the cart. foreach (var portableNumber in portableNumbers) { @@ -148,6 +134,63 @@ await Parallel.ForEachAsync(parsedNumbers, async (number, token) => } } + [HttpPost] + [ValidateAntiForgeryToken] + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + [OutputCache(Duration = 3600, VaryByQueryKeys = ["dialedNumber"])] + public async Task ToCSVAsync([Bind("dialedNumber")] string dialedNumber) + { + // Add portable numbers to cart in bulk + if (!string.IsNullOrWhiteSpace(dialedNumber)) + { + var parsedNumbers = dialedNumber.ExtractDialedNumbers(); + + if (!parsedNumbers.Any()) + { + return View("Index", new LookupResults + { + Message = "No dialed phone numbers found. Please try a different query. πŸ₯ΊπŸ‘‰πŸ‘ˆ" + }); + } + + var cart = Cart.GetFromSession(HttpContext.Session); + + // If they have an invalid Session we don't want to waste any time running queries for them. + if (string.IsNullOrWhiteSpace(HttpContext.Session.Id) || !HttpContext.Session.IsAvailable || cart is null) + { + return View("Index"); + } + + var results = new List(); + await Parallel.ForEachAsync(parsedNumbers, async (number, token) => + { + var result = await VerifyPortabilityAsync(number); + results.Add(result); + }); + + var portableNumbers = results.Where(x => x.Portable && x.Wireless is false).ToArray(); + var notPortable = results.Where(x => x.Portable is false).Select(x => x.PortedDialedNumber).ToArray(); + + // Separate wireless numbers out from the rest. + var wirelessPortable = results.Where(x => x.Wireless && x.Portable).ToArray(); + + var builder = new StringBuilder(); + builder.AppendLine("DialedNumber,City,State,DateIngested,Wireless,Portable,LastPorted,SPID,LATA,LEC,LECType,LIDBName,LRN,OCN,Activation"); + foreach (var number in results) + { + builder.AppendLine($"{number.PortedDialedNumber},{number.City},{number.State},{number.DateIngested},{number.Wireless},{number.Portable}," + + $"{number.LrnLookup.LastPorted},{number.LrnLookup.SPID},{number.LrnLookup.LATA},{number.LrnLookup.LEC},{number.LrnLookup.LECType}," + + $"{number.LrnLookup.LIDBName},{number.LrnLookup.LRN},{number.LrnLookup.OCN},{number.LrnLookup.LastPorted}"); + } + + return File(Encoding.UTF8.GetBytes(builder.ToString()), "text/csv", $"AccelerateNetworksPhoneNumbers{DateTime.Now.ToString("yyyyMMddTHHmmss")}.csv"); + } + else + { + return View("Index"); + } + } + public async Task VerifyPortabilityAsync(string dialedNumber) { var checkParse = PhoneNumbersNA.PhoneNumber.TryParse(dialedNumber, out var phoneNumber); diff --git a/NumberSearch.Mvc/Views/Lookup/Index.cshtml b/NumberSearch.Mvc/Views/Lookup/Index.cshtml index fdaad81a..ef5dae3d 100644 --- a/NumberSearch.Mvc/Views/Lookup/Index.cshtml +++ b/NumberSearch.Mvc/Views/Lookup/Index.cshtml @@ -347,7 +347,11 @@
- Export to CSV +
+ @Html.AntiForgeryToken() + + +
@@ -558,18 +562,15 @@ -
- @Html.AntiForgeryToken() - -
-
+
+
+ + @Html.AntiForgeryToken() + -
+
- +
}