Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

142 строки
6.4KB

  1. using Campaign_Tracker.Server.LegacyData.Models;
  2. namespace Campaign_Tracker.Server.LegacyData;
  3. /// <summary>
  4. /// In-memory implementation of <see cref="ILegacyDataAccess"/> for use in
  5. /// development environments without an Access database, and as the test double
  6. /// for integration tests.
  7. ///
  8. /// Accepts seeded collections via constructor so tests can inject specific
  9. /// records without coupling to file-system or network state.
  10. /// </summary>
  11. public sealed class InMemoryLegacyDataAccess : ILegacyDataAccess
  12. {
  13. private readonly IReadOnlyList<LegacyJurisdiction> _jurisdictions;
  14. private readonly IReadOnlyList<LegacyContact> _contacts;
  15. private readonly IReadOnlyList<LegacyKit> _kits;
  16. private readonly IReadOnlyList<LegacyKitLabel> _kitLabels;
  17. public InMemoryLegacyDataAccess(
  18. IReadOnlyList<LegacyJurisdiction>? jurisdictions = null,
  19. IReadOnlyList<LegacyContact>? contacts = null,
  20. IReadOnlyList<LegacyKit>? kits = null,
  21. IReadOnlyList<LegacyKitLabel>? kitLabels = null)
  22. {
  23. _jurisdictions = jurisdictions ?? DefaultJurisdictions;
  24. _contacts = contacts ?? DefaultContacts;
  25. _kits = kits ?? DefaultKits;
  26. _kitLabels = kitLabels ?? DefaultKitLabels;
  27. }
  28. // ── Jurisdiction ──────────────────────────────────────────────────────────
  29. public Task<LegacyJurisdiction?> GetJurisdictionAsync(
  30. string jCode, CancellationToken cancellationToken = default)
  31. {
  32. var result = _jurisdictions.FirstOrDefault(j =>
  33. string.Equals(j.JCode, jCode, StringComparison.OrdinalIgnoreCase));
  34. return Task.FromResult(result);
  35. }
  36. public Task<IReadOnlyList<LegacyJurisdiction>> GetAllJurisdictionsAsync(
  37. CancellationToken cancellationToken = default) =>
  38. Task.FromResult(_jurisdictions);
  39. // ── Contact ───────────────────────────────────────────────────────────────
  40. public Task<LegacyContact?> GetContactByIdAsync(
  41. int id, CancellationToken cancellationToken = default)
  42. {
  43. var result = _contacts.FirstOrDefault(c => c.Id == id);
  44. return Task.FromResult(result);
  45. }
  46. public Task<IReadOnlyList<LegacyContact>> GetContactsByJurisdictionAsync(
  47. string jCode, CancellationToken cancellationToken = default)
  48. {
  49. IReadOnlyList<LegacyContact> result = _contacts
  50. .Where(c => string.Equals(c.JurisCode, jCode, StringComparison.OrdinalIgnoreCase))
  51. .ToList();
  52. return Task.FromResult(result);
  53. }
  54. // ── Kit ───────────────────────────────────────────────────────────────────
  55. public Task<LegacyKit?> GetKitByIdAsync(
  56. int id, CancellationToken cancellationToken = default)
  57. {
  58. var result = _kits.FirstOrDefault(k => k.Id == id);
  59. return Task.FromResult(result);
  60. }
  61. public Task<IReadOnlyList<LegacyKit>> GetKitsByJurisdictionAsync(
  62. string jCode, CancellationToken cancellationToken = default)
  63. {
  64. IReadOnlyList<LegacyKit> result = _kits
  65. .Where(k => string.Equals(k.JCode, jCode, StringComparison.OrdinalIgnoreCase))
  66. .ToList();
  67. return Task.FromResult(result);
  68. }
  69. // ── KitLabel ──────────────────────────────────────────────────────────────
  70. public Task<IReadOnlyList<LegacyKitLabel>> GetKitLabelsByKitAsync(
  71. int kitId, CancellationToken cancellationToken = default)
  72. {
  73. IReadOnlyList<LegacyKitLabel> result = _kitLabels
  74. .Where(l => l.KitId == kitId)
  75. .ToList();
  76. return Task.FromResult(result);
  77. }
  78. // ── Default seed data (representative dev/test records) ──────────────────
  79. private static readonly IReadOnlyList<LegacyJurisdiction> DefaultJurisdictions =
  80. [
  81. new("FAIR01", "Fairview Borough", "100 Main St", "Fairview, PA 16415", null, null),
  82. new("LAKE02", "Lake Township", "200 Lake Rd", "Lake City, PA 16423", null, null),
  83. new("PINE03", "Pine County", "300 Pine Ave", "Edinboro, PA 16412", null, null),
  84. ];
  85. private static readonly IReadOnlyList<LegacyContact> DefaultContacts =
  86. [
  87. new(1, "FAIR01", "Jane Doe", "Election Director", "jdoe@fairview.gov",
  88. "814-555-0101", null,
  89. "100 Main St", null, "Fairview, PA 16415",
  90. null, null, null, "Fairview Borough", "01"),
  91. new(2, "LAKE02", "John Smith", "Clerk", "jsmith@laketownship.gov",
  92. "814-555-0202", "814-555-0203",
  93. "200 Lake Rd", null, "Lake City, PA 16423",
  94. null, null, null, "Lake Township", "02"),
  95. ];
  96. private static readonly IReadOnlyList<LegacyKit> DefaultKits =
  97. [
  98. new(101, "FAIR01", "JOB-2026-001", "Inkjet", "Active", "fair01_primary_2026.mdb",
  99. Cass: true, InkJetJob: true,
  100. CreatedOn: new DateTime(2026, 3, 1, 0, 0, 0, DateTimeKind.Utc),
  101. ExportedToSnailWorks: null, LabelsPrinted: null,
  102. OfficeCopiesAmount: 50, InboundStid: "STI001", OutboundStid: "STO001"),
  103. new(102, "LAKE02", "JOB-2026-002", "OfficeCopy", "Pending", null,
  104. Cass: false, InkJetJob: false,
  105. CreatedOn: new DateTime(2026, 3, 5, 0, 0, 0, DateTimeKind.Utc),
  106. ExportedToSnailWorks: null, LabelsPrinted: null,
  107. OfficeCopiesAmount: 25, InboundStid: null, OutboundStid: null),
  108. ];
  109. private static readonly IReadOnlyList<LegacyKitLabel> DefaultKitLabels =
  110. [
  111. new(201, KitId: 101,
  112. InBoundImb: "0041234500012345678", InBoundImbDigits: "01-234-56789-12",
  113. InBoundSerial: "SN001",
  114. OutboundImb: "0041234500098765432", OutboundImbDigits: "01-234-56789-98",
  115. OutboundSerial: "SN002", SetNumber: 1),
  116. new(202, KitId: 101,
  117. InBoundImb: "0041234500022345678", InBoundImbDigits: "01-234-56789-22",
  118. InBoundSerial: "SN003",
  119. OutboundImb: "0041234500088765432", OutboundImbDigits: "01-234-56789-88",
  120. OutboundSerial: "SN004", SetNumber: 2),
  121. ];
  122. }

Powered by TurnKey Linux.