/src/GiveCRM.Web/Services/MailingListService.cs

https://github.com/NathanGloyn/GiveCRM
C# | 82 lines | 76 code | 6 blank | 0 comment | 2 complexity | 158ddebf6f0b5ebd0f309cc30d0477a6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using GiveCRM.ImportExport;
  7. using GiveCRM.ImportExport.Cells;
  8. using GiveCRM.Models;
  9. namespace GiveCRM.Web.Services
  10. {
  11. public class MailingListService : IMailingListService
  12. {
  13. private readonly IExcelExport _excelExport;
  14. public MailingListService(IExcelExport excelExport)
  15. {
  16. _excelExport = excelExport;
  17. }
  18. public void WriteToStream(IEnumerable<Member> members, Stream stream, OutputFormat outputFormat)
  19. {
  20. var rows = GetHeaderRows().Concat(GetDataRows(members));
  21. _excelExport.WriteDataToExport(rows, new CellFormatter(), ExcelFileType.XLS);
  22. _excelExport.ExportToStream(stream);
  23. }
  24. private List<List<Cell>> GetDataRows(IEnumerable<Member> members)
  25. {
  26. return members.Select(m => new List<Cell>
  27. {
  28. new Cell {Value = m.Reference},
  29. new Cell {Value = m.Title},
  30. new Cell {Value= m.Salutation},
  31. new Cell {Value= m.FirstName},
  32. new Cell {Value= m.LastName},
  33. new Cell {Value= m.EmailAddress},
  34. new Cell {Value= m.AddressLine1},
  35. new Cell {Value= m.AddressLine2},
  36. new Cell {Value= m.City},
  37. new Cell {Value= m.Region},
  38. new Cell {Value= m.PostalCode},
  39. new Cell {Value= m.Country},
  40. new Cell {Value= FirstTelephoneNumber(m, PhoneNumberType.Home)},
  41. new Cell {Value= FirstTelephoneNumber(m, PhoneNumberType.Mobile)},
  42. new Cell {Value= FirstTelephoneNumber(m, PhoneNumberType.Work)},
  43. }).ToList();
  44. }
  45. private string FirstTelephoneNumber(Member member, PhoneNumberType phoneNumberType)
  46. {
  47. PhoneNumber phoneNumber = member.PhoneNumbers.FirstOrDefault(number => number.PhoneNumberType == phoneNumberType);
  48. return phoneNumber != null ? phoneNumber.Number : "";
  49. }
  50. private List<List<Cell>> GetHeaderRows()
  51. {
  52. var ret = new List<List<Cell>>
  53. {
  54. new List<Cell>
  55. {
  56. new Cell {Value = "Reference", IsBold = true},
  57. new Cell {Value = "Title", IsBold = true},
  58. new Cell {Value = "Salutation", IsBold = true},
  59. new Cell {Value = "Firstname", IsBold = true},
  60. new Cell {Value = "Surname", IsBold = true},
  61. new Cell {Value = "Email", IsBold = true},
  62. new Cell {Value = "Address 1", IsBold = true},
  63. new Cell {Value = "Address 2", IsBold = true},
  64. new Cell {Value = "City", IsBold = true},
  65. new Cell {Value = "Region", IsBold = true},
  66. new Cell {Value = "Postal Code", IsBold = true},
  67. new Cell {Value = "Country", IsBold = true},
  68. new Cell {Value = "Home Telephone", IsBold = true},
  69. new Cell {Value = "Mobile Telephone", IsBold = true},
  70. new Cell {Value = "Work Telephone", IsBold = true},
  71. }
  72. };
  73. return ret;
  74. }
  75. }
  76. }