Skip to content

Commit

Permalink
Fixed the CSV export function on the lookups page.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Aug 13, 2024
1 parent eae3b27 commit 4257aa1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
75 changes: 59 additions & 16 deletions NumberSearch.Mvc/Controllers/LookupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public async Task<IActionResult> IndexAsync(string dialedNumber)
[HttpPost]
[ValidateAntiForgeryToken]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[OutputCache(Duration = 3600, VaryByQueryKeys = new string[] { "dialedNumber", "csv" })]
public async Task<IActionResult> BulkPortAsync([Bind("dialedNumber,csv")] string dialedNumber, bool csv)
[OutputCache(Duration = 3600, VaryByQueryKeys = ["dialedNumber"])]
public async Task<IActionResult> BulkPortAsync([Bind("dialedNumber")] string dialedNumber)
{
// Add portable numbers to cart in bulk
if (!string.IsNullOrWhiteSpace(dialedNumber))
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<IActionResult> 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<PortedPhoneNumber>();
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<PortedPhoneNumber> VerifyPortabilityAsync(string dialedNumber)
{
var checkParse = PhoneNumbersNA.PhoneNumber.TryParse(dialedNumber, out var phoneNumber);
Expand Down
23 changes: 12 additions & 11 deletions NumberSearch.Mvc/Views/Lookup/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@
</div>
<div class="row">
<div class="col">
<a class="btn btn-outline-primary mt-3" href="/Lookup/BulkPort?dialedNumber=@Model?.DialedNumber&csv=true">Export to CSV</a>
<form method="post" action="/Lookup/ToCSV">
@Html.AntiForgeryToken()
<input type="hidden" name="dialedNumber" value="@Model?.DialedNumber">
<button type="submit" class="btn btn-outline-primary mt-3">Export to CSV</button>
</form>
</div>
</div>
</div>
Expand Down Expand Up @@ -558,18 +562,15 @@
</tbody>
</table>
</div>
<form method="post" class="justify-content-center" asp-controller="Lookup" asp-action="BulkPort">
@Html.AntiForgeryToken()
<div class="form-group" hidden>
<textarea hidden class="form-control" aria-label="With textarea" type="text" name="dialedNumber" placeholder="Up to 200 Numbers per Search" style="height:8rem;">@Model?.DialedNumber</textarea>
<input hidden type="text" name="csv" value="true" />
</div>
<div class="row">
<div class="col">
<div class="row">
<div class="col">
<form method="post" action="/Lookup/ToCSV">
@Html.AntiForgeryToken()
<input type="hidden" name="dialedNumber" value="@Model?.DialedNumber">
<button type="submit" class="btn btn-outline-primary mt-3">Export to CSV</button>
</div>
</form>
</div>
</form>
</div>
</div>
</div>
}
Expand Down

0 comments on commit 4257aa1

Please sign in to comment.