SupplierValidator.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using FluentValidation;
  2. using GreenTree.Strohrmann.ERP.Services.Geolocator;
  3. using GreenTree.Strohrmann.ERP.Web.Models.Business;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace GreenTree.Strohrmann.ERP.Web.Validators
  9. {
  10. public class SupplierValidator : AbstractValidator<SupplierModel>
  11. {
  12. #region DI fields
  13. // The global geocoding service
  14. private readonly IGeocodingService _geocodingService;
  15. #endregion
  16. #region Ctor
  17. /// <summary>
  18. /// Initializes a new instance of the SupplierValidator class
  19. /// </summary>
  20. /// <param name="geocodingService">Global geocoding service.</param>
  21. public SupplierValidator(IGeocodingService geocodingService)
  22. {
  23. _geocodingService = geocodingService;
  24. RuleFor(m => m.Name)
  25. .NotEmpty();
  26. RuleFor(m => m.Address)
  27. .NotEmpty();
  28. RuleFor(m => m.ZipCode)
  29. .NotEmpty();
  30. RuleFor(m => m.Town)
  31. .NotEmpty();
  32. RuleFor(m => m.Country)
  33. .NotEmpty();
  34. RuleFor(m => m.Address)
  35. .Custom((a, context) =>
  36. {
  37. var model = context.InstanceToValidate as SupplierModel;
  38. if (model == null)
  39. {
  40. context.AddFailure("Unbekannter Fehler.");
  41. return;
  42. }
  43. var addressValid = _geocodingService.IsValidAddress(
  44. String.Format("{0} {1} {2}", model.Address, model.ZipCode, model.Town));
  45. if (!addressValid)
  46. {
  47. context.AddFailure("Adresse kann nicht gefunden werden. Bitte überprüfen Sie die Adresseingaben.");
  48. return;
  49. }
  50. });
  51. RuleFor(x => x.PhoneFirst)
  52. .NotNull()
  53. .DependentRules(() =>
  54. {
  55. RuleFor(x => x.MailFirst)
  56. .NotNull();
  57. });
  58. }
  59. #endregion
  60. }
  61. }