DeviationDataModelValidator.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. RuleFor(m => m.CustomNumber)
  19. .NotEmpty()
  20. .WithMessage("Nummer wird benötigt")
  21. .MaximumLength(10)
  22. .WithMessage("Muss unter 10 Zeichen sein");
  23. RuleFor(m => m)
  24. .Must(m => CustomNumberDoesNotExistInSite(m))
  25. .WithMessage("VA-Nummer in Baustelle bereits vorhanden.");
  26. RuleFor(m => m.Description)
  27. .NotEmpty()
  28. .WithMessage("Beschreibung wird benötigt");
  29. RuleFor(m => m.ReceiptDate)
  30. .NotEmpty()
  31. .WithMessage("Einreichdatum wird benötigt");
  32. RuleFor(m => m.StatusId)
  33. .NotEmpty()
  34. .WithMessage("Ein Status muss gewählt werden");
  35. RuleFor(m => m.KindId)
  36. .NotEmpty()
  37. .WithMessage("Eine Art muss gewählt werden");
  38. }
  39. private bool CustomNumberDoesNotExistInSite(DeviationDataModel model)
  40. {
  41. var siteService = Singleton<IContainer>.Instance.Resolve<ISiteService>();
  42. var appendixService = Singleton<IContainer>.Instance.Resolve<IAppendixService>();
  43. if (model == null) return false;
  44. var allDeviations = new List<Core.Domain.Deviation.Deviation>();
  45. if (model.SiteId.HasValue)
  46. {
  47. var site = siteService.GetSiteById(model.SiteId.Value);
  48. if (site != null)
  49. allDeviations.AddRange(
  50. site.Appendices
  51. .SelectMany(a => a.Deviations));
  52. if (site != null)
  53. allDeviations.AddRange(
  54. site.Deviations);
  55. }
  56. else if (model.AppendixId.HasValue)
  57. {
  58. var appendix = appendixService.GetAppendixById(model.AppendixId.Value);
  59. if (appendix != null && appendix.Site != null)
  60. allDeviations.AddRange(
  61. appendix.Site.Appendices
  62. .SelectMany(a => a.Deviations));
  63. if (appendix != null && appendix.Site != null)
  64. allDeviations.AddRange(
  65. appendix.Site.Deviations);
  66. }
  67. var existingDeviation =
  68. allDeviations
  69. .FirstOrDefault(d => d.CustomNumber == model.CustomNumber);
  70. if (existingDeviation != null && existingDeviation.Id == model.Id) return true;
  71. if (existingDeviation != null && existingDeviation.Id != model.Id) return false;
  72. if (existingDeviation == null) return true;
  73. return false;
  74. }
  75. }
  76. }