public async Task <IActionResult> ExportAddresses(EmailAddressesViewModel viewModel) { if (string.IsNullOrEmpty(viewModel.SignUpSource)) { ModelState.AddModelError(nameof(viewModel.SignUpSource), "Please select a list to export."); return(await ShowAddressView(viewModel)); } var json = JsonConvert.SerializeObject(await _emailReminderService .GetAllSubscribersAsync(viewModel.SignUpSource)); var filename = $"EmailList-{viewModel.SignUpSource}.json"; using (var ms = new MemoryStream()) { using (var writer = new StreamWriter(ms)) { await writer.WriteAsync(json); await writer.FlushAsync(); writer.Close(); return(File(ms.ToArray(), "text/json", FileUtility.EnsureValidFilename(filename))); } } }
private async Task <IActionResult> ShowAddressView(EmailAddressesViewModel viewModel) { var emailAddressesViewModel = viewModel ?? new EmailAddressesViewModel(); if (!emailAddressesViewModel.HasSources) { var allEmailReminders = await _emailManagementService.GetEmailListsAsync(); var selectListMailingLists = allEmailReminders.Select(_ => new SelectListItem { Text = $"{_.Data} ({_.Count})", Value = _.Data }); emailAddressesViewModel.SignUpSources = new SelectList(selectListMailingLists, nameof(SelectListItem.Value), nameof(SelectListItem.Text)); emailAddressesViewModel.HasSources = allEmailReminders.Count > 0; } return(View(emailAddressesViewModel)); }
public async Task <IActionResult> ImportAddresses(EmailAddressesViewModel viewModel) { if (viewModel?.UploadedFile == null) { ShowAlertDanger("You must upload a JSON file of email records."); ModelState.AddModelError(nameof(viewModel.UploadedFile), "A .json file is required."); return(RedirectToAction(nameof(EmailManagementController.Addresses))); } using (var reader = new StreamReader(viewModel.UploadedFile.OpenReadStream())) { var issues = new List <string>(); int recordNumber = 1; var success = 0; try { var jsonString = await reader.ReadToEndAsync(); foreach (var emailReminder in JsonConvert .DeserializeObject <ICollection <EmailReminder> >(jsonString)) { try { if (await _emailReminderService .ImportEmailToListAsync(GetActiveUserId(), emailReminder)) { success++; } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { _logger.LogError("Issue in {Filename} on record {RecordNumber}: {ErrorMessage}", viewModel.UploadedFile.FileName, recordNumber, ex.Message); issues.Add($"Issue in item {recordNumber}: {ex.Message}"); } #pragma warning restore CA1031 // Do not catch general exception types if (recordNumber % 50 == 0) { await _emailReminderService.SaveImportAsync(); } recordNumber++; } await _emailReminderService.SaveImportAsync(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { _logger.LogError("Import failed for {Filename} on record {RecordNumber}: {ErrorMessage}", viewModel.UploadedFile.FileName, recordNumber, ex.Message); ShowAlertDanger($"Failed to import addresses: {ex.Message}"); return(RedirectToAction(nameof(EmailManagementController.Addresses))); } #pragma warning restore CA1031 // Do not catch general exception types var response = new StringBuilder(); if (success > 0) { response.Append("Successfully imported <strong>") .Append(success) .Append(" addresses</strong>."); } else { response.Append("All addresses were already present on the list."); } if (issues.Count == 0) { ShowAlertSuccess(response.ToString()); } else { response.Append(" The following issues occurred with the import:<ul>"); foreach (var issue in issues) { response.Append("<li>").Append(issue).Append("</li>"); } response.Append("</ul>"); ShowAlertWarning(response.ToString()); } return(RedirectToAction(nameof(EmailManagementController.Addresses))); } }