Beispiel #1
0
        public PreserveCommandValidator(IProjectValidator projectValidator, ITagValidator tagValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => NotBeAClosedProjectForTagAsync(command.TagId, token))
            .WithMessage(command => $"Project for tag is closed! Tag={command.TagId}")
            .MustAsync(BeAnExistingRequirementAsync)
            .WithMessage((x, id) => "Tag and/or requirement doesn't exist!")
            .MustAsync((command, token) => NotBeAVoidedTagAsync(command.TagId, token))
            .WithMessage((x, id) => $"Tag is voided! Tag={id}")
            .MustAsync((command, token) => PreservationIsStartedAsync(command.TagId, token))
            .WithMessage((x, id) => $"Tag must have status {PreservationStatus.Active} to preserve! Tag={id}")
            .MustAsync((command, token) => RequirementIsReadyToBePreservedAsync(command.TagId, command.RequirementId, token))
            .WithMessage((command, _) =>
                         $"Tag doesn't have this requirement ready to be preserved! Tag={command.TagId}. Requirement={command.RequirementId}");

            async Task <bool> NotBeAClosedProjectForTagAsync(int tagId, CancellationToken token)
            => !await projectValidator.IsClosedForTagAsync(tagId, token);

            async Task <bool> BeAnExistingRequirementAsync(PreserveCommand command, CancellationToken token)
            => await tagValidator.ExistsRequirementAsync(command.TagId, command.RequirementId, token);

            async Task <bool> NotBeAVoidedTagAsync(int tagId, CancellationToken token)
            => !await tagValidator.IsVoidedAsync(tagId, token);

            async Task <bool> PreservationIsStartedAsync(int tagId, CancellationToken token)
            => await tagValidator.VerifyPreservationStatusAsync(tagId, PreservationStatus.Active, token);

            async Task <bool> RequirementIsReadyToBePreservedAsync(int tagId, int requirementId, CancellationToken token)
            => await tagValidator.RequirementIsReadyToBePreservedAsync(tagId, requirementId, token);
        }
        public UploadFieldValueAttachmentCommandValidator(
            IProjectValidator projectValidator,
            ITagValidator tagValidator,
            IFieldValidator fieldValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => NotBeAClosedProjectForTagAsync(command.TagId, token))
            .WithMessage(command => $"Project for tag is closed! Tag={command.TagId}")
            .MustAsync(BeAnExistingRequirementAsync)
            .WithMessage(command => "Tag and/or requirement doesn't exist!")
            .MustAsync(BeAnExistingFieldForRequirementAsync)
            .WithMessage(command => "Field doesn't exist in requirement!")
            .MustAsync((command, token) => NotBeAVoidedTagAsync(command.TagId, token))
            .WithMessage(command => $"Tag is voided! Tag={command.TagId}")
            .MustAsync((command, token) => HasRequirementWithActivePeriodAsync(command.TagId, command.RequirementId, token))
            .WithMessage(command =>
                         $"Tag doesn't have this requirement with active period! Tag={command.TagId}. Requirement={command.RequirementId}")
            .MustAsync((command, token) => BeAFieldForAttachmentAsync(command.FieldId, token))
            .WithMessage(command => $"Field values can not be recorded for field type! Field={command.FieldId}")
            .MustAsync((command, token) => NotBeAVoidedFieldAsync(command.FieldId, token))
            .WithMessage(command => $"Field is voided! Field={command.FieldId}");

            async Task <bool> NotBeAClosedProjectForTagAsync(int tagId, CancellationToken token)
            => !await projectValidator.IsClosedForTagAsync(tagId, token);

            async Task <bool> BeAnExistingRequirementAsync(UploadFieldValueAttachmentCommand command, CancellationToken token)
            => await tagValidator.ExistsRequirementAsync(command.TagId, command.RequirementId, token);

            async Task <bool> BeAnExistingFieldForRequirementAsync(UploadFieldValueAttachmentCommand command, CancellationToken token)
            => await tagValidator.ExistsFieldForRequirementAsync(command.TagId, command.RequirementId, command.FieldId, token);

            async Task <bool> NotBeAVoidedTagAsync(int tagId, CancellationToken token)
            => !await tagValidator.IsVoidedAsync(tagId, token);

            async Task <bool> HasRequirementWithActivePeriodAsync(int tagId, int requirementId, CancellationToken token)
            => await tagValidator.HasRequirementWithActivePeriodAsync(tagId, requirementId, token);

            async Task <bool> NotBeAVoidedFieldAsync(int fieldId, CancellationToken token)
            => !await fieldValidator.IsVoidedAsync(fieldId, token);

            async Task <bool> BeAFieldForAttachmentAsync(int fieldId, CancellationToken token)
            => await fieldValidator.IsValidForAttachmentAsync(fieldId, token);
        }