/src/GiveCRM.BusinessLogic/MailingListService.cs

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