DeviationDataModelValidator.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Autofac;
  2. using FluentValidation;
  3. using GreenTree.Nachtragsmanagement.Core;
  4. using GreenTree.Nachtragsmanagement.Services.Appendix;
  5. using GreenTree.Nachtragsmanagement.Services.Deviation;
  6. using GreenTree.Nachtragsmanagement.Services.Site;
  7. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Web;
  12. namespace GreenTree.Nachtragsmanagement.Web.Validation.Deviation
  13. {
  14. public class DeviationDataModelValidator : AbstractValidator<DeviationDataModel>
  15. {
  16. public DeviationDataModelValidator()
  17. {
  18. var siteService = Singleton<IContainer>.Instance.Resolve<ISiteService>();
  19. var appendixService = Singleton<IContainer>.Instance.Resolve<IAppendixService>();
  20. RuleFor(m => m.CustomNumber)
  21. .NotEmpty()
  22. .WithMessage("Nummer wird benötigt")
  23. .MaximumLength(10)
  24. .WithMessage("Muss unter 10 Zeichen sein");
  25. RuleFor(m => m)
  26. .Must(d1 =>
  27. (d1.SiteId.HasValue &&
  28. !siteService.GetSiteById(d1.SiteId.Value).Appendices
  29. .SelectMany(d2 => d2.Deviations)
  30. .Select(d3 => d3.CustomNumber)
  31. .Contains(d1.CustomNumber)) ||
  32. (d1.AppendixId.HasValue &&
  33. !appendixService.GetAppendixById(d1.AppendixId.Value).Site.Appendices
  34. .SelectMany(d2 => d2.Deviations)
  35. .Select(d3 => d3.CustomNumber)
  36. .Contains(d1.CustomNumber)))
  37. .WithMessage("VA-Nummer in Baustelle bereits vorhanden.");
  38. RuleFor(m => m.Description)
  39. .NotEmpty()
  40. .WithMessage("Beschreibung wird benötigt");
  41. RuleFor(m => m.ReceiptDate)
  42. .NotEmpty()
  43. .WithMessage("Einreichdatum wird benötigt");
  44. RuleFor(m => m.StatusId)
  45. .NotEmpty()
  46. .WithMessage("Ein Status muss gewählt werden");
  47. RuleFor(m => m.KindId)
  48. .NotEmpty()
  49. .WithMessage("Eine Art muss gewählt werden");
  50. }
  51. }
  52. }