public DeleteRequirementTypeCommandValidator(
            IRequirementTypeValidator requirementTypeValidator,
            IRowVersionValidator rowVersionValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => BeAnExistingRequirementTypeAsync(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type doesn't exist! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) => NotHaveAnyRequirementDefinitions(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type has requirement definitions! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) => BeAVoidedRequirementTypeAsync(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type is not voided! Requirement type={command.RequirementTypeId}")
            .Must(command => HaveAValidRowVersion(command.RowVersion))
            .WithMessage(command => $"Not a valid row version! Row version={command.RowVersion}");

            async Task <bool> BeAnExistingRequirementTypeAsync(int requirementTypeId, CancellationToken token)
            => await requirementTypeValidator.ExistsAsync(requirementTypeId, token);
            async Task <bool> NotHaveAnyRequirementDefinitions(int requirementTypeId, CancellationToken token)
            => !await requirementTypeValidator.AnyRequirementDefinitionExistsAsync(requirementTypeId, token);
            async Task <bool> BeAVoidedRequirementTypeAsync(int requirementTypeId, CancellationToken token)
            => await requirementTypeValidator.IsVoidedAsync(requirementTypeId, token);

            bool HaveAValidRowVersion(string rowVersion)
            => rowVersionValidator.IsValid(rowVersion);
        }
Esempio n. 2
0
        public CreateRequirementDefinitionCommandValidator(IRequirementTypeValidator requirementTypeValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => RequirementTypeMustExists(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type doesn't exist! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) => RequirementTypeMustNotBeVoided(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type is voided! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) =>
                       RequirementDefinitionTitleMustBeUniqueOnType(command.RequirementTypeId, command.Title, command.Fields, token))
            .WithMessage(command => $"A requirement definition with this title already exists on the requirement type! Requirement type={command.RequirementTypeId}");

            async Task <bool> RequirementTypeMustExists(int reqTypeId, CancellationToken token)
            => await requirementTypeValidator.ExistsAsync(reqTypeId, token);

            async Task <bool> RequirementTypeMustNotBeVoided(int reqTypeId, CancellationToken token)
            => !await requirementTypeValidator.IsVoidedAsync(reqTypeId, token);

            async Task <bool> RequirementDefinitionTitleMustBeUniqueOnType(
                int reqTypeId,
                string title,
                IList <FieldsForCommand> fields,
                CancellationToken token)
            => !await requirementTypeValidator.AnyRequirementDefinitionExistsWithSameTitleAsync(
                reqTypeId,
                title,
                fields.Select(f => f.FieldType).Distinct().ToList(),
                token);
        }
Esempio n. 3
0
        public UpdateRequirementTypeCommandValidator(
            IRequirementTypeValidator requirementTypeValidator,
            IRowVersionValidator rowVersionValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => BeAnExistingRequirementTypeAsync(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type doesn't exist! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) => NotBeAVoidedRequirementTypeAsync(command.RequirementTypeId, token))
            .WithMessage(command => $"Requirement type is voided! Requirement type={command.RequirementTypeId}")
            .MustAsync((command, token) => BeAUniqueCodeAsync(command.RequirementTypeId, command.Code, token))
            .WithMessage(command => $"Another requirement type with this code already exists! Code={command.Code}")
            .MustAsync((command, token) => BeAUniqueTitleAsync(command.RequirementTypeId, command.Title, token))
            .WithMessage(command => $"Another requirement type with this title already exists! Title={command.Title}")
            .Must(command => HaveAValidRowVersion(command.RowVersion))
            .WithMessage(command => $"Not a valid row version! Row version={command.RowVersion}");

            async Task <bool> BeAnExistingRequirementTypeAsync(int requirementTypeId, CancellationToken token)
            => await requirementTypeValidator.ExistsAsync(requirementTypeId, token);
            async Task <bool> NotBeAVoidedRequirementTypeAsync(int requirementTypeId, CancellationToken token)
            => !await requirementTypeValidator.IsVoidedAsync(requirementTypeId, token);
            async Task <bool> BeAUniqueCodeAsync(int requirementTypeId, string code, CancellationToken token)
            => !await requirementTypeValidator.ExistsWithSameCodeInAnotherTypeAsync(requirementTypeId, code, token);
            async Task <bool> BeAUniqueTitleAsync(int requirementTypeId, string title, CancellationToken token)
            => !await requirementTypeValidator.ExistsWithSameTitleInAnotherTypeAsync(requirementTypeId, title, token);

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