コード例 #1
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);
        }
        public DeleteRequirementDefinitionCommandValidator(
            IRequirementTypeValidator requirementTypeValidator,
            IRequirementDefinitionValidator requirementDefinitionValidator,
            IRowVersionValidator rowVersionValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync(BeAnExistingRequirementDefinitionAsync)
            .WithMessage(command => "Requirement type and/or requirement definition doesn't exist!")
            .MustAsync((command, token) => BeAVoidedRequirementDefinitionAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Requirement definition is not voided! Requirement definition={command.RequirementDefinitionId}")
            .MustAsync((command, token) => NotHaveAnyFieldsAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Requirement definition has fields! Requirement definition={command.RequirementDefinitionId}")
            .MustAsync((command, token) => NotHaveAnyTagRequirementsAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Tag requirement with this requirement definition exists! Requirement definition={command.RequirementDefinitionId}")
            .MustAsync((command, token) => NotHaveAnyTagFunctionRequirementsAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Tag function requirement with this requirement definition exists! Requirement definition={command.RequirementDefinitionId}")
            .Must(command => HaveAValidRowVersion(command.RowVersion))
            .WithMessage(command => $"Not a valid row version! Row version={command.RowVersion}");

            async Task <bool> BeAnExistingRequirementDefinitionAsync(DeleteRequirementDefinitionCommand command, CancellationToken token)
            => await requirementTypeValidator.RequirementDefinitionExistsAsync(command.RequirementTypeId, command.RequirementDefinitionId, token);
            async Task <bool> BeAVoidedRequirementDefinitionAsync(int requirementDefinitionId, CancellationToken token)
            => await requirementDefinitionValidator.IsVoidedAsync(requirementDefinitionId, token);
            async Task <bool> NotHaveAnyFieldsAsync(int requirementDefinitionId, CancellationToken token)
            => !await requirementDefinitionValidator.HasAnyFieldsAsync(requirementDefinitionId, token);
            async Task <bool> NotHaveAnyTagRequirementsAsync(int requirementDefinitionId, CancellationToken token)
            => !await requirementDefinitionValidator.TagRequirementsExistAsync(requirementDefinitionId, token);
            async Task <bool> NotHaveAnyTagFunctionRequirementsAsync(int requirementDefinitionId, CancellationToken token)
            => !await requirementDefinitionValidator.TagFunctionRequirementsExistAsync(requirementDefinitionId, token);

            bool HaveAValidRowVersion(string rowVersion)
            => rowVersionValidator.IsValid(rowVersion);
        }
        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);
        }
コード例 #4
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);
        }
コード例 #5
0
        public CreateRequirementTypeCommandValidator(IRequirementTypeValidator requirementTypeValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync((command, token) => NotExistsARequirementTypeWithSameCode(command.Code, token))
            .WithMessage("Requirement type with this code already exists!")
            .MustAsync((command, token) => NotExistsARequirementTypeWithSameTitle(command.Title, token))
            .WithMessage("Requirement type with this title already exists!");

            async Task <bool> NotExistsARequirementTypeWithSameCode(string code, CancellationToken token)
            => !await requirementTypeValidator.ExistsWithSameCodeAsync(code, token);

            async Task <bool> NotExistsARequirementTypeWithSameTitle(string title, CancellationToken token)
            => !await requirementTypeValidator.ExistsWithSameTitleAsync(title, token);
        }
        public VoidRequirementDefinitionCommandValidator(
            IRequirementTypeValidator requirementTypeValidator,
            IRequirementDefinitionValidator requirementDefinitionValidator,
            IRowVersionValidator rowVersionValidator
            )
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync(BeAnExistingRequirementDefinitionAsync)
            .WithMessage(command => "Requirement type and/or requirement definition doesn't exist!")
            .MustAsync((command, token) => NotBeAVoidedRequirementDefinitionAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Requirement definition is already voided! Requirement definition={command.RequirementDefinitionId}")
            .Must(command => HaveAValidRowVersion(command.RowVersion))
            .WithMessage(command => $"Not a valid row version! Row version={command.RowVersion}");

            async Task <bool> BeAnExistingRequirementDefinitionAsync(VoidRequirementDefinitionCommand command, CancellationToken token)
            => await requirementTypeValidator.RequirementDefinitionExistsAsync(command.RequirementTypeId, command.RequirementDefinitionId, token);
            async Task <bool> NotBeAVoidedRequirementDefinitionAsync(int requirementDefinitionId, CancellationToken token)
            => !await requirementDefinitionValidator.IsVoidedAsync(requirementDefinitionId, token);

            bool HaveAValidRowVersion(string rowVersion)
            => rowVersionValidator.IsValid(rowVersion);
        }
コード例 #7
0
        public UpdateRequirementDefinitionCommandValidator(
            IRequirementTypeValidator requirementTypeValidator,
            IRequirementDefinitionValidator requirementDefinitionValidator,
            IFieldValidator fieldValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command)
            .MustAsync(BeAnExistingRequirementDefinitionAsync)
            .WithMessage(command => "Requirement type and/or requirement definition doesn't exist!")
            .MustAsync((command, token) => NotBeAVoidedRequirementDefinitionAsync(command.RequirementDefinitionId, token))
            .WithMessage(command => $"Requirement definition is voided! Requirement definition={command.Title}")
            .MustAsync(RequirementDefinitionTitleMustBeUniqueOnType)
            .WithMessage(command => $"A requirement definition with this title already exists on the requirement type! Requirement type={command.Title}")
            .MustAsync((command, token) => AllFieldsToBeDeletedAreVoidedAsync(command.RequirementDefinitionId, command.UpdateFields, token))
            .WithMessage(command => $"Fields to be deleted must be voided! Requirement definition={command.Title}")
            .MustAsync((command, token) => NoFieldsToBeDeletedShouldBeInUseAsync(command.RequirementDefinitionId, command.UpdateFields, token))
            .WithMessage(command => $"Fields to be deleted can not be in use! Requirement definition={command.Title}");

            RuleForEach(command => command.UpdateFields)
            .MustAsync((command, field, __, token) => BeAnExistingField(command, field.Id, token))
            .WithMessage(command => "Field doesn't exist in requirement!")
            .MustAsync((command, field, __, token) => BeSameFieldTypeOnExistingFieldsAsync(field, token))
            .WithMessage((_, field) => $"Cannot change field type on existing fields! Field={field.Id}");

            async Task <bool> BeAnExistingRequirementDefinitionAsync(UpdateRequirementDefinitionCommand command, CancellationToken token)
            => await requirementTypeValidator.RequirementDefinitionExistsAsync(command.RequirementTypeId, command.RequirementDefinitionId, token);

            async Task <bool> NotBeAVoidedRequirementDefinitionAsync(int requirementDefinitionId, CancellationToken token)
            => !await requirementDefinitionValidator.IsVoidedAsync(requirementDefinitionId, token);

            async Task <bool> RequirementDefinitionTitleMustBeUniqueOnType(
                UpdateRequirementDefinitionCommand command,
                CancellationToken token)
            {
                var fieldTypesFromUpdated = command.UpdateFields.Select(uf => uf.FieldType).ToList();
                var fieldTypesFromNew     = command.NewFields.Select(nf => nf.FieldType).ToList();

                return(!await requirementTypeValidator.OtherRequirementDefinitionExistsWithSameTitleAsync(
                           command.RequirementTypeId,
                           command.RequirementDefinitionId,
                           command.Title,
                           fieldTypesFromUpdated.Concat(fieldTypesFromNew).Distinct().ToList(),
                           token));
            }

            async Task <bool> BeAnExistingField(UpdateRequirementDefinitionCommand command, int fieldId, CancellationToken token)
            => await requirementTypeValidator.FieldExistsAsync(command.RequirementTypeId, command.RequirementDefinitionId, fieldId, token);

            async Task <bool> BeSameFieldTypeOnExistingFieldsAsync(UpdateFieldsForCommand field, CancellationToken token)
            => await fieldValidator.VerifyFieldTypeAsync(field.Id, field.FieldType, token);

            async Task <bool> AllFieldsToBeDeletedAreVoidedAsync(int requirementDefinitionId, IList <UpdateFieldsForCommand> updateFields, CancellationToken token)
            {
                var updateFieldIds = updateFields.Select(u => u.Id).ToList();

                return(await requirementDefinitionValidator.AllExcludedFieldsAreVoidedAsync(requirementDefinitionId, updateFieldIds, token));
            }

            async Task <bool> NoFieldsToBeDeletedShouldBeInUseAsync(int requirementDefinitionId, IList <UpdateFieldsForCommand> updateFields, CancellationToken token)
            {
                var updateFieldIds = updateFields.Select(u => u.Id).ToList();

                return(!await requirementDefinitionValidator.AnyExcludedFieldsIsInUseAsync(requirementDefinitionId, updateFieldIds, token));
            }
        }