UserExamValidator.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Exam.ExamManages;
  2. using Exam.Infrastructure.Extensions;
  3. using Exam.Infrastructure.Validation.Validation;
  4. using FluentValidation;
  5. using Hotline.Repository.SqlSugar.Exam.Core.Constants;
  6. using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
  7. using Hotline.Repository.SqlSugar.Validate;
  8. namespace Exam.Repository.Sqlsugar.Validators.ExamManages
  9. {
  10. public class UserExamValidator:BaseValidator<UserExam>
  11. {
  12. private readonly IUserExamRepository _userExamRepository;
  13. public UserExamValidator(IUserExamRepository userExamRepository)
  14. {
  15. RuleSet(ValidatorTypeConstants.Create, () =>
  16. {
  17. BaseValidateRule();
  18. ValidateRuleWithAdd();
  19. });
  20. RuleSet(ValidatorTypeConstants.Modify, () =>
  21. {
  22. BaseValidateRule();
  23. ValidateRuleWithModify();
  24. });
  25. RuleSet(ValidatorTypeConstants.Remove, () =>
  26. {
  27. ValidateRuleWithRemove();
  28. });
  29. this._userExamRepository = userExamRepository;
  30. }
  31. protected override void BaseValidateRule()
  32. {
  33. base.BaseValidateRule();
  34. }
  35. protected override void ValidateRuleWithAdd()
  36. {
  37. base.ValidateRuleWithAdd();
  38. RuleFor(m => m.CreationTime).NotEmpty().WithMessage(x => string.Format(ErrorMessage.IsRequired, x.GetType().GetDescription(nameof(UserExam.CreationTime))));
  39. }
  40. protected override void ValidateRuleWithModify()
  41. {
  42. base.ValidateRuleWithModify();
  43. RuleFor(m => m.LastModificationTime).NotEmpty().WithMessage(x => string.Format(ErrorMessage.IsRequired, x.GetType().GetDescription(nameof(UserExam.LastModificationTime))));
  44. }
  45. private void ValidateRuleWithRemove()
  46. {
  47. RuleFor(m => m.Id).Must(v=>!_userExamRepository.Queryable().Where(x=>x.Id == v && x.ExamStatus != Hotline.Share.Enums.Exams.EExamStatus.NoStart).Any()).WithMessage(ExamErrorMessage.DeleteUnableUserExam);
  48. }
  49. }
  50. }