|
- using Campaign_Tracker.Server.LegacyData.Models;
-
- namespace Campaign_Tracker.Server.LegacyData;
-
- /// <summary>
- /// In-memory implementation of <see cref="ILegacyDataAccess"/> for use in
- /// development environments without an Access database, and as the test double
- /// for integration tests.
- ///
- /// Accepts seeded collections via constructor so tests can inject specific
- /// records without coupling to file-system or network state.
- /// </summary>
- public sealed class InMemoryLegacyDataAccess : ILegacyDataAccess
- {
- private readonly IReadOnlyList<LegacyJurisdiction> _jurisdictions;
- private readonly IReadOnlyList<LegacyContact> _contacts;
- private readonly IReadOnlyList<LegacyKit> _kits;
- private readonly IReadOnlyList<LegacyKitLabel> _kitLabels;
-
- public InMemoryLegacyDataAccess(
- IReadOnlyList<LegacyJurisdiction>? jurisdictions = null,
- IReadOnlyList<LegacyContact>? contacts = null,
- IReadOnlyList<LegacyKit>? kits = null,
- IReadOnlyList<LegacyKitLabel>? kitLabels = null)
- {
- _jurisdictions = jurisdictions ?? DefaultJurisdictions;
- _contacts = contacts ?? DefaultContacts;
- _kits = kits ?? DefaultKits;
- _kitLabels = kitLabels ?? DefaultKitLabels;
- }
-
- // ── Jurisdiction ──────────────────────────────────────────────────────────
-
- public Task<LegacyJurisdiction?> GetJurisdictionAsync(
- string jCode, CancellationToken cancellationToken = default)
- {
- var result = _jurisdictions.FirstOrDefault(j =>
- string.Equals(j.JCode, jCode, StringComparison.OrdinalIgnoreCase));
- return Task.FromResult(result);
- }
-
- public Task<IReadOnlyList<LegacyJurisdiction>> GetAllJurisdictionsAsync(
- CancellationToken cancellationToken = default) =>
- Task.FromResult(_jurisdictions);
-
- // ── Contact ───────────────────────────────────────────────────────────────
-
- public Task<LegacyContact?> GetContactByIdAsync(
- int id, CancellationToken cancellationToken = default)
- {
- var result = _contacts.FirstOrDefault(c => c.Id == id);
- return Task.FromResult(result);
- }
-
- public Task<IReadOnlyList<LegacyContact>> GetContactsByJurisdictionAsync(
- string jCode, CancellationToken cancellationToken = default)
- {
- IReadOnlyList<LegacyContact> result = _contacts
- .Where(c => string.Equals(c.JurisCode, jCode, StringComparison.OrdinalIgnoreCase))
- .ToList();
- return Task.FromResult(result);
- }
-
- // ── Kit ───────────────────────────────────────────────────────────────────
-
- public Task<LegacyKit?> GetKitByIdAsync(
- int id, CancellationToken cancellationToken = default)
- {
- var result = _kits.FirstOrDefault(k => k.Id == id);
- return Task.FromResult(result);
- }
-
- public Task<IReadOnlyList<LegacyKit>> GetKitsByJurisdictionAsync(
- string jCode, CancellationToken cancellationToken = default)
- {
- IReadOnlyList<LegacyKit> result = _kits
- .Where(k => string.Equals(k.JCode, jCode, StringComparison.OrdinalIgnoreCase))
- .ToList();
- return Task.FromResult(result);
- }
-
- // ── KitLabel ──────────────────────────────────────────────────────────────
-
- public Task<IReadOnlyList<LegacyKitLabel>> GetKitLabelsByKitAsync(
- int kitId, CancellationToken cancellationToken = default)
- {
- IReadOnlyList<LegacyKitLabel> result = _kitLabels
- .Where(l => l.KitId == kitId)
- .ToList();
- return Task.FromResult(result);
- }
-
- // ── Default seed data (representative dev/test records) ──────────────────
-
- private static readonly IReadOnlyList<LegacyJurisdiction> DefaultJurisdictions =
- [
- new("FAIR01", "Fairview Borough", "100 Main St", "Fairview, PA 16415", null, null),
- new("LAKE02", "Lake Township", "200 Lake Rd", "Lake City, PA 16423", null, null),
- new("PINE03", "Pine County", "300 Pine Ave", "Edinboro, PA 16412", null, null),
- ];
-
- private static readonly IReadOnlyList<LegacyContact> DefaultContacts =
- [
- new(1, "FAIR01", "Jane Doe", "Election Director", "jdoe@fairview.gov",
- "814-555-0101", null,
- "100 Main St", null, "Fairview, PA 16415",
- null, null, null, "Fairview Borough", "01"),
- new(2, "LAKE02", "John Smith", "Clerk", "jsmith@laketownship.gov",
- "814-555-0202", "814-555-0203",
- "200 Lake Rd", null, "Lake City, PA 16423",
- null, null, null, "Lake Township", "02"),
- ];
-
- private static readonly IReadOnlyList<LegacyKit> DefaultKits =
- [
- new(101, "FAIR01", "JOB-2026-001", "Inkjet", "Active", "fair01_primary_2026.mdb",
- Cass: true, InkJetJob: true,
- CreatedOn: new DateTime(2026, 3, 1, 0, 0, 0, DateTimeKind.Utc),
- ExportedToSnailWorks: null, LabelsPrinted: null,
- OfficeCopiesAmount: 50, InboundStid: "STI001", OutboundStid: "STO001"),
- new(102, "LAKE02", "JOB-2026-002", "OfficeCopy", "Pending", null,
- Cass: false, InkJetJob: false,
- CreatedOn: new DateTime(2026, 3, 5, 0, 0, 0, DateTimeKind.Utc),
- ExportedToSnailWorks: null, LabelsPrinted: null,
- OfficeCopiesAmount: 25, InboundStid: null, OutboundStid: null),
- ];
-
- private static readonly IReadOnlyList<LegacyKitLabel> DefaultKitLabels =
- [
- new(201, KitId: 101,
- InBoundImb: "0041234500012345678", InBoundImbDigits: "01-234-56789-12",
- InBoundSerial: "SN001",
- OutboundImb: "0041234500098765432", OutboundImbDigits: "01-234-56789-98",
- OutboundSerial: "SN002", SetNumber: 1),
- new(202, KitId: 101,
- InBoundImb: "0041234500022345678", InBoundImbDigits: "01-234-56789-22",
- InBoundSerial: "SN003",
- OutboundImb: "0041234500088765432", OutboundImbDigits: "01-234-56789-88",
- OutboundSerial: "SN004", SetNumber: 2),
- ];
- }
|