public UpdateStepCommandValidator(
            IJourneyValidator journeyValidator,
            IStepValidator stepValidator,
            IModeValidator modeValidator,
            IResponsibleValidator responsibleValidator,
            IRowVersionValidator rowVersionValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => BeAnExistingStepAsync(command.JourneyId, command.StepId, token))
            .WithMessage(_ => "Journey and/or step doesn't exist!")
            .MustAsync((command, token) => HaveUniqueStepTitleInJourneyAsync(command.JourneyId, command.StepId, command.Title, token))
            .WithMessage(command => $"Another step with title already exists in a journey! Step={command.Title}")
            .MustAsync((command, token) => NotBeAVoidedStepAsync(command.StepId, token))
            .WithMessage(command => $"Step is voided! Step={command.StepId}")
            .MustAsync((command, token) => BeAnExistingModeAsync(command.ModeId, token))
            .WithMessage(command => $"Mode doesn't exist! Mode={command.ModeId}")
            .MustAsync((command, token) => NotChangedToAVoidedModeAsync(command.ModeId, command.StepId, token))
            .WithMessage(command => $"Mode is voided! Mode={command.ModeId}")
            .MustAsync((command, token) => NotBeAnExistingAndVoidedResponsibleAsync(command.ResponsibleCode, token))
            .WithMessage(command => $"Responsible is voided! ResponsibleCode={command.ResponsibleCode}")
            .MustAsync((command, token) => NotHaveOtherStepsWithSameAutoTransferMethodInJourneyAsync(command.JourneyId, command.StepId, command.AutoTransferMethod, token))
            .WithMessage(command => $"Same auto transfer method can not be set on multiple steps in a journey! Method={command.AutoTransferMethod}")
            .Must(command => HaveAValidRowVersion(command.RowVersion))
            .WithMessage(command => $"Not a valid row version! Row version={command.RowVersion}");

            async Task <bool> BeAnExistingStepAsync(int journeyId, int stepId, CancellationToken token)
            => await journeyValidator.ExistsStepAsync(journeyId, stepId, token);

            async Task <bool> HaveUniqueStepTitleInJourneyAsync(int journeyId, int stepId, string stepTitle, CancellationToken token) =>
            !await journeyValidator.OtherStepExistsWithSameTitleAsync(journeyId, stepId, stepTitle, token);

            async Task <bool> NotBeAVoidedStepAsync(int stepId, CancellationToken token)
            => !await stepValidator.IsVoidedAsync(stepId, token);

            async Task <bool> BeAnExistingModeAsync(int modeId, CancellationToken token)
            => await modeValidator.ExistsAsync(modeId, token);

            async Task <bool> NotChangedToAVoidedModeAsync(int modeId, int stepId, CancellationToken token)
            => await stepValidator.HasModeAsync(modeId, stepId, token) ||
            !await modeValidator.IsVoidedAsync(modeId, token);

            async Task <bool> NotBeAnExistingAndVoidedResponsibleAsync(string responsibleCode, CancellationToken token)
            => !await responsibleValidator.ExistsAndIsVoidedAsync(responsibleCode, token);

            bool HaveAValidRowVersion(string rowVersion)
            => rowVersionValidator.IsValid(rowVersion);

            async Task <bool> NotHaveOtherStepsWithSameAutoTransferMethodInJourneyAsync(int journeyId, int stepId, AutoTransferMethod autoTransferMethod, CancellationToken token)
            => autoTransferMethod == AutoTransferMethod.None || !await journeyValidator.HasOtherStepWithAutoTransferMethodAsync(journeyId, stepId, autoTransferMethod, token);
        }
Пример #2
0
        public CreateStepCommandValidator(
            IJourneyValidator journeyValidator,
            IModeValidator modeValidator,
            IResponsibleValidator responsibleValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => BeAnExistingJourney(command.JourneyId, token))
            .WithMessage(command => $"Journey doesn't exist! Journey={command.JourneyId}")
            .MustAsync((command, token) => NotBeAVoidedJourney(command.JourneyId, token))
            .WithMessage(command => $"Journey is voided! Journey={command.JourneyId}")
            .MustAsync((command, token) => BeAnExistingModeAsync(command.ModeId, token))
            .WithMessage(command => $"Mode doesn't exist! Mode={command.ModeId}")
            .MustAsync((command, token) => NotBeAVoidedModeAsync(command.ModeId, token))
            .WithMessage(command => $"Mode is voided! Mode={command.ModeId}")
            .MustAsync((command, token) => NotBeAnExistingAndVoidedResponsibleAsync(command.ResponsibleCode, token))
            .WithMessage(command => $"Responsible is voided! Responsible={command.ResponsibleCode}")
            .MustAsync((command, token) => HaveUniqueStepTitleAsync(command.JourneyId, command.Title, token))
            .WithMessage(command => $"Step with title already exists in journey! Step={command.Title}")
            .MustAsync((command, token) => NotBeAnyStepWithSameAutoTransferMethodInJourneyAsync(command.JourneyId, command.AutoTransferMethod, token))
            .WithMessage(command => $"Same auto transfer method can not be set on multiple steps in a journey! Method={command.AutoTransferMethod}");

            async Task <bool> HaveUniqueStepTitleAsync(int journeyId, string stepTitle, CancellationToken token)
            => !await journeyValidator.AnyStepExistsWithSameTitleAsync(journeyId, stepTitle, token);

            async Task <bool> BeAnExistingJourney(int journeyId, CancellationToken token)
            => await journeyValidator.ExistsAsync(journeyId, token);

            async Task <bool> BeAnExistingModeAsync(int modeId, CancellationToken token)
            => await modeValidator.ExistsAsync(modeId, token);

            async Task <bool> NotBeAnExistingAndVoidedResponsibleAsync(string responsibleCode, CancellationToken token)
            => !await responsibleValidator.ExistsAndIsVoidedAsync(responsibleCode, token);

            async Task <bool> NotBeAVoidedJourney(int journeyId, CancellationToken token)
            => !await journeyValidator.IsVoidedAsync(journeyId, token);

            async Task <bool> NotBeAVoidedModeAsync(int modeId, CancellationToken token)
            => !await modeValidator.IsVoidedAsync(modeId, token);

            async Task <bool> NotBeAnyStepWithSameAutoTransferMethodInJourneyAsync(int journeyId, AutoTransferMethod autoTransferMethod, CancellationToken token)
            => autoTransferMethod == AutoTransferMethod.None || !await journeyValidator.HasAnyStepWithAutoTransferMethodAsync(journeyId, autoTransferMethod, token);
        }