コード例 #1
0
ファイル: AddPatterns.cs プロジェクト: fossabot/squidex
        public async Task UpdateAsync()
        {
            var ids = await appRepository.QueryAppIdsAsync();

            foreach (var id in ids)
            {
                var app = await stateFactory.GetSingleAsync <AppDomainObject>(id);

                if (app.Snapshot.Patterns.Count == 0)
                {
                    foreach (var pattern in initialPatterns.Values)
                    {
                        var command =
                            new AddPattern
                        {
                            Actor     = app.Snapshot.CreatedBy,
                            AppId     = app.Snapshot.Id,
                            Name      = pattern.Name,
                            PatternId = Guid.NewGuid(),
                            Pattern   = pattern.Pattern,
                            Message   = pattern.Message
                        };

                        app.AddPattern(command);
                    }

                    await app.WriteAsync();
                }
            }
        }
コード例 #2
0
ファイル: AddPatterns.cs プロジェクト: vtan31/squidex
        public async Task UpdateAsync()
        {
            var ids = await grainFactory.GetGrain <IAppsByNameIndex>(SingleGrain.Id).GetAppIdsAsync();

            foreach (var id in ids)
            {
                var app = grainFactory.GetGrain <IAppGrain>(id);

                var state = await app.GetStateAsync();

                if (state.Value.Patterns.Count == 0)
                {
                    foreach (var pattern in initialPatterns.Values)
                    {
                        var command =
                            new AddPattern
                        {
                            Actor     = state.Value.CreatedBy,
                            AppId     = state.Value.Id,
                            Name      = pattern.Name,
                            PatternId = Guid.NewGuid(),
                            Pattern   = pattern.Pattern,
                            Message   = pattern.Message
                        };

                        await app.ExecuteAsync(command);
                    }
                }
            }
        }
コード例 #3
0
ファイル: GuardAppPattern.cs プロジェクト: llenroc/squidex
        public static void CanAdd(AppPatterns patterns, AddPattern command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot add pattern.", error =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    error(new ValidationError("Pattern name can not be empty.", nameof(command.Name)));
                }

                if (patterns.Values.Any(x => x.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    error(new ValidationError("Pattern name is already assigned.", nameof(command.Name)));
                }

                if (string.IsNullOrWhiteSpace(command.Pattern))
                {
                    error(new ValidationError("Pattern can not be empty.", nameof(command.Pattern)));
                }
                else if (!command.Pattern.IsValidRegex())
                {
                    error(new ValidationError("Pattern is not a valid regular expression.", nameof(command.Pattern)));
                }

                if (patterns.Values.Any(x => x.Pattern == command.Pattern))
                {
                    error(new ValidationError("Pattern already exists.", nameof(command.Pattern)));
                }
            });
        }
コード例 #4
0
        public async Task UpdateAsync()
        {
            var ids = await indexForApps.GetIdsAsync();

            foreach (var id in ids)
            {
                var app = await indexForApps.GetAppAsync(id);

                if (app != null && app.Patterns.Count == 0)
                {
                    foreach (var pattern in initialPatterns.Values)
                    {
                        var command =
                            new AddPattern
                        {
                            Actor     = app.CreatedBy,
                            AppId     = id,
                            Name      = pattern.Name,
                            PatternId = Guid.NewGuid(),
                            Pattern   = pattern.Pattern,
                            Message   = pattern.Message
                        };

                        await commandBus.PublishAsync(command);
                    }
                }
            }
        }
コード例 #5
0
        public void CanAdd_should_throw_exception_if_pattern_not_valid()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = "[0-9{1}"
            };

            Assert.Throws <ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command));
        }
コード例 #6
0
        public void CanAdd_should_throw_exception_if_pattern_empty()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = string.Empty
            };

            Assert.Throws <ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command));
        }
コード例 #7
0
        public void CanAdd_should_not_throw_exception_if_command_is_valid()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = ".*"
            };

            GuardAppPatterns.CanAdd(patterns_0, command);
        }
コード例 #8
0
        public AppDomainObject AddPattern(AddPattern command)
        {
            ThrowIfNotCreated();

            RaiseEvent(SimpleMapper.Map(command, new AppPatternAdded()));

            return(this);
        }
コード例 #9
0
        public void CanAdd_should_throw_exception_if_id_empty_guid()
        {
            var command = new AddPattern {
                Name = string.Empty, Pattern = ".*"
            };

            Assert.Throws <ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command));
        }
コード例 #10
0
        protected Task On(AddPattern command, CommandContext context)
        {
            return(handler.UpdateSyncedAsync <AppDomainObject>(context, a =>
            {
                GuardAppPattern.CanAdd(a.Snapshot.Patterns, command);

                a.AddPattern(command);
            }));
        }
コード例 #11
0
        public void CanAdd_should_throw_exception_if_pattern_not_valid()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = "[0-9{1}"
            };

            ValidationAssert.Throws(() => GuardAppPatterns.CanAdd(patterns_0, command),
                                    new ValidationError("Pattern is not a valid value.", "Pattern"));
        }
コード例 #12
0
        public void CanAdd_should_throw_exception_if_pattern_empty()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = string.Empty
            };

            ValidationAssert.Throws(() => GuardAppPatterns.CanAdd(patterns_0, command),
                                    new ValidationError("Pattern is required.", "Pattern"));
        }
コード例 #13
0
        public void CanAdd_should_throw_exception_if_name_empty()
        {
            var command = new AddPattern {
                PatternId = patternId, Name = string.Empty, Pattern = ".*"
            };

            ValidationAssert.Throws(() => GuardAppPatterns.CanAdd(command, App(patterns_0)),
                                    new ValidationError("Name is required.", "Name"));
        }
コード例 #14
0
        public async Task Should_assign_app_id_to_app_self_command()
        {
            var command = new AddPattern();
            var context = new CommandContext(command, commandBus);

            await sut.HandleAsync(context);

            Assert.Equal(appId.Id, command.AppId);
        }
コード例 #15
0
        public void CanAdd_should_throw_exception_if_name_exists()
        {
            var patterns_1 = patterns_0.Add(Guid.NewGuid(), "any", "[a-z]", "Message");

            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = ".*"
            };

            Assert.Throws <ValidationException>(() => GuardAppPattern.CanAdd(patterns_1, command));
        }
コード例 #16
0
        public async Task Should_not_override_app_id()
        {
            var command = new AddPattern {
                AppId = Guid.NewGuid()
            };
            var context = new CommandContext(command, commandBus);

            await sut.HandleAsync(context);

            Assert.NotEqual(appId.Id, command.AppId);
        }
コード例 #17
0
        public void CanAdd_should_throw_exception_if_name_exists()
        {
            var patterns_1 = patterns_0.Add(DomainId.NewGuid(), "any", "[a-z]", "Message");

            var command = new AddPattern {
                PatternId = patternId, Name = "any", Pattern = ".*"
            };

            ValidationAssert.Throws(() => GuardAppPatterns.CanAdd(patterns_1, command),
                                    new ValidationError("A pattern with the same name already exists."));
        }
コード例 #18
0
        public void CanAdd_should_throw_exception_if_pattern_exists()
        {
            var patterns_1 = patterns_0.Add(DomainId.NewGuid(), "any", "[a-z]", "Message");

            var command = new AddPattern {
                PatternId = patternId, Name = "other", Pattern = "[a-z]"
            };

            ValidationAssert.Throws(() => GuardAppPatterns.CanAdd(patterns_1, command),
                                    new ValidationError("This pattern already exists but with another name."));
        }
コード例 #19
0
        public async Task AddPattern_should_create_events_and_update_state()
        {
            var command = new AddPattern {
                PatternId = patternId3, Name = "Any", Pattern = ".*", Message = "Msg"
            };

            await ExecuteCreateAsync();

            var result = await sut.ExecuteAsync(CreateCommand(command));

            result.ShouldBeEquivalent(sut.Snapshot);

            Assert.Equal(initialPatterns.Count + 1, sut.Snapshot.Patterns.Count);

            LastEvents
            .ShouldHaveSameEvents(
                CreateEvent(new AppPatternAdded {
                PatternId = patternId3, Name = "Any", Pattern = ".*", Message = "Msg"
            })
                );
        }
コード例 #20
0
        public static void CanAdd(AddPattern command, IAppEntity app)
        {
            Guard.NotNull(command, nameof(command));

            var patterns = app.Patterns;

            Validate.It(e =>
            {
                if (command.PatternId == DomainId.Empty)
                {
                    e(Not.Defined(nameof(command.PatternId)), nameof(command.PatternId));
                }

                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined(nameof(command.Name)), nameof(command.Name));
                }

                if (patterns.Values.Any(x => x.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e(T.Get("apps.patterns.nameAlreadyExists"));
                }

                if (string.IsNullOrWhiteSpace(command.Pattern))
                {
                    e(Not.Defined(nameof(command.Pattern)), nameof(command.Pattern));
                }
                else if (!command.Pattern.IsValidRegex())
                {
                    e(Not.Valid(nameof(command.Pattern)), nameof(command.Pattern));
                }

                if (patterns.Values.Any(x => x.Pattern == command.Pattern))
                {
                    e(T.Get("apps.patterns.patternAlreadyExists"));
                }
            });
        }
コード例 #21
0
        public static void CanAdd(AppPatterns patterns, AddPattern command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot add pattern.", e =>
            {
                if (command.PatternId == Guid.Empty)
                {
                    e(Not.Defined("Id"), nameof(command.PatternId));
                }

                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined("Name"), nameof(command.Name));
                }

                if (patterns.Values.Any(x => x.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e("A pattern with the same name already exists.");
                }

                if (string.IsNullOrWhiteSpace(command.Pattern))
                {
                    e(Not.Defined("Pattern"), nameof(command.Pattern));
                }
                else if (!command.Pattern.IsValidRegex())
                {
                    e(Not.Valid("Pattern"), nameof(command.Pattern));
                }

                if (patterns.Values.Any(x => x.Pattern == command.Pattern))
                {
                    e("This pattern already exists but with another name.");
                }
            });
        }
コード例 #22
0
 private void AddPattern(AddPattern command)
 {
     Raise(command, new AppPatternAdded());
 }
コード例 #23
0
 public void AddPattern(AddPattern command)
 {
     Raise(command, new AppPatternAdded());
 }
コード例 #24
0
 public static AppPatternDto FromCommand(AddPattern command)
 {
     return(SimpleMapper.Map(command, new AppPatternDto()));
 }
コード例 #25
0
 public void AddPattern(AddPattern command)
 {
     RaiseEvent(SimpleMapper.Map(command, new AppPatternAdded()));
 }
コード例 #26
0
 private void MainWindow_AddPattern(object sender, RoutedEventArgs e)
 {
     AddPattern?.Invoke(this, e);
 }