Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

83 wiersze
3.6KB

  1. using Campaign_Tracker.Server.LegacyData;
  2. namespace Campaign_Tracker.Server.ExtensionData;
  3. /// <summary>
  4. /// Validates a <see cref="LegacyLinkReference"/> by resolving it through the
  5. /// anti-corruption layer (<see cref="ILegacyDataAccess"/>).
  6. /// Each link type maps to a unique primary-key lookup, guaranteeing no ambiguity (AC #2, AC #3).
  7. /// </summary>
  8. public sealed class LegacyLinkValidator : ILegacyLinkValidator
  9. {
  10. private readonly ILegacyDataAccess _legacyData;
  11. public LegacyLinkValidator(ILegacyDataAccess legacyData)
  12. {
  13. _legacyData = legacyData;
  14. }
  15. public async Task<LegacyLinkValidationResult> ValidateAsync(
  16. LegacyLinkReference reference,
  17. CancellationToken cancellationToken = default)
  18. {
  19. return reference.Type switch
  20. {
  21. LegacyLinkType.JurisdictionJCode => await ValidateJurisdictionAsync(reference.Value, cancellationToken),
  22. LegacyLinkType.KitId => await ValidateKitAsync(reference.Value, cancellationToken),
  23. LegacyLinkType.ContactId => await ValidateContactAsync(reference.Value, cancellationToken),
  24. _ => LegacyLinkValidationResult.Failure($"Unknown legacy link type '{reference.Type}'."),
  25. };
  26. }
  27. private async Task<LegacyLinkValidationResult> ValidateJurisdictionAsync(
  28. string jCode, CancellationToken cancellationToken)
  29. {
  30. if (string.IsNullOrWhiteSpace(jCode))
  31. return LegacyLinkValidationResult.Failure("JCode is required and cannot be blank.");
  32. var record = await _legacyData.GetJurisdictionAsync(jCode, cancellationToken);
  33. return record is null
  34. ? LegacyLinkValidationResult.Failure(
  35. $"No legacy jurisdiction found for JCode '{jCode}'. Verify the identifier and try again.")
  36. : LegacyLinkValidationResult.Success();
  37. }
  38. private async Task<LegacyLinkValidationResult> ValidateKitAsync(
  39. string rawId, CancellationToken cancellationToken)
  40. {
  41. if (!int.TryParse(rawId, System.Globalization.NumberStyles.Integer,
  42. System.Globalization.CultureInfo.InvariantCulture, out var id))
  43. return LegacyLinkValidationResult.Failure(
  44. $"Kit identifier '{rawId}' is not a valid integer ID.");
  45. if (id <= 0)
  46. return LegacyLinkValidationResult.Failure(
  47. $"Kit identifier '{rawId}' must be greater than zero.");
  48. var record = await _legacyData.GetKitByIdAsync(id, cancellationToken);
  49. return record is null
  50. ? LegacyLinkValidationResult.Failure(
  51. $"No legacy kit found for ID {id}. Verify the identifier and try again.")
  52. : LegacyLinkValidationResult.Success();
  53. }
  54. private async Task<LegacyLinkValidationResult> ValidateContactAsync(
  55. string rawId, CancellationToken cancellationToken)
  56. {
  57. if (!int.TryParse(rawId, System.Globalization.NumberStyles.Integer,
  58. System.Globalization.CultureInfo.InvariantCulture, out var id))
  59. return LegacyLinkValidationResult.Failure(
  60. $"Contact identifier '{rawId}' is not a valid integer ID.");
  61. if (id <= 0)
  62. return LegacyLinkValidationResult.Failure(
  63. $"Contact identifier '{rawId}' must be greater than zero.");
  64. var record = await _legacyData.GetContactByIdAsync(id, cancellationToken);
  65. return record is null
  66. ? LegacyLinkValidationResult.Failure(
  67. $"No legacy contact found for ID {id}. Verify the identifier and try again.")
  68. : LegacyLinkValidationResult.Success();
  69. }
  70. }

Powered by TurnKey Linux.