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((command, token) => BeAnExistingTagAsync(command.TagId, token))
            .WithMessage(command => $"Tag doesn't exist! Tag={command.TagId}")
            .MustAsync((command, token) => NotBeAVoidedTag(command.TagId, token))
            .WithMessage(command => $"Tag is voided! Tag={command.TagId}")
            .MustAsync((command, token) => PreservationIsStartedAsync(command.TagId, token))
            .WithMessage(command => $"Tag must have status {PreservationStatus.Active} to preserve! Tag={command.TagId}")
            .MustAsync((command, token) => BeReadyToBePreservedAsync(command.TagId, token))
            .WithMessage(command => $"Tag is not ready to be preserved! Tag={command.TagId}");

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

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

            async Task <bool> NotBeAVoidedTag(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> BeReadyToBePreservedAsync(int tagId, CancellationToken token)
            => await tagValidator.IsReadyToBePreservedAsync(tagId, token);
        }
Beispiel #2
0
        public BulkPreserveCommandValidator(
            IProjectValidator projectValidator,
            ITagValidator tagValidator)
        {
            CascadeMode = CascadeMode.Stop;

            RuleFor(command => command.TagIds)
            .Must(ids => ids != null && ids.Any())
            .WithMessage("At least 1 tag must be given!")
            .Must(BeUniqueTags)
            .WithMessage("Tags must be unique!")
            .MustAsync(BeInSameProjectAsync)
            .WithMessage("Tags must be in same project!")
            .MustAsync(NotBeAClosedProjectForTagAsync)
            .WithMessage("Project is closed!");

            When(command => command.TagIds.Any() && BeUniqueTags(command.TagIds), () =>
            {
                RuleForEach(command => command.TagIds)
                .MustAsync((_, tagId, __, token) => BeAnExistingTagAsync(tagId, token))
                .WithMessage((_, id) => $"Tag doesn't exist! Tag={id}")
                .MustAsync((_, tagId, __, token) => NotBeAVoidedTagAsync(tagId, token))
                .WithMessage((_, id) => $"Tag is voided! Tag={id}")
                .MustAsync((_, tagId, __, token) => PreservationIsStartedAsync(tagId, token))
                .WithMessage((_, id) => $"Tag must have status {PreservationStatus.Active} to preserve! Tag={id}")
                .MustAsync((_, tagId, __, token) => BeReadyToBePreservedAsync(tagId, token))
                .WithMessage((_, id) => $"Tag is not ready to be bulk preserved! Tag={id}");
            });

            bool BeUniqueTags(IEnumerable <int> tagIds)
            {
                var ids = tagIds.ToList();

                return(ids.Distinct().Count() == ids.Count);
            }

            async Task <bool> BeInSameProjectAsync(IEnumerable <int> tagIds, CancellationToken token)
            => await projectValidator.AllTagsInSameProjectAsync(tagIds, token);

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

            async Task <bool> BeAnExistingTagAsync(int tagId, CancellationToken token)
            => await tagValidator.ExistsAsync(tagId, 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> BeReadyToBePreservedAsync(int tagId, CancellationToken token)
            => await tagValidator.IsReadyToBePreservedAsync(tagId, token);
        }