Пример #1
0
        public void CreateToggleWithConfig_Ok()
        {
            var toggle = new ToggleBuilder().WithId("625c3b92-c94e-435a-84e5-66e02c346fb4").WithName("isButtonGreen").Build();

            Assert.IsNotNull(toggle.Name);
            Assert.IsNotNull(toggle.Id);
            Assert.IsNotNull(toggle.CreatedAt);
            Assert.IsNotNull(toggle.UpdateAt);
            Assert.IsNull(toggle.Configs);

            toggle.Configs = new List <ToggleConfig>
            {
                new ToggleConfig
                {
                    Id            = "af898a6d-758c-40ef-b6ce-0ebc63df460c",
                    Version       = "v1",
                    ApplicationId = "ece9a845-1898-485a-9ef2-0bafc7db5570",
                    ToggleId      = toggle.Id
                }
            };

            Assert.IsNotNull(toggle.Configs);
            foreach (var config in toggle.Configs)
            {
                Assert.IsNotNull(config.Version);
                Assert.IsNotNull(config.Id);
                Assert.IsNotNull(config.CreatedAt);
                Assert.IsNotNull(config.UpdateAt);
            }
        }
Пример #2
0
        /// <summary>
        /// Posts a toggle asynchronous.
        /// </summary>
        /// <param name="toogDto">The toog dto.</param>
        /// <returns>The toggle,</returns>
        /// <exception cref="ArgumentNullException">toogDto</exception>
        public async Task <Response <ToggleDto> > PostToggleAsync(ToggleDto toogDto)
        {
            if (toogDto == null)
            {
                throw new ArgumentNullException(nameof(toogDto));
            }

            if (string.IsNullOrEmpty(toogDto.Name))
            {
                throw new ArgumentNullException(nameof(toogDto.Name));
            }

            if (toogDto.Applications == null && !toogDto.Applications.Any())
            {
                throw new ArgumentNullException(nameof(toogDto.Applications));
            }

            //get all repositories
            var toggleRepository       = GetToggleRepository();
            var toggleConfigRepository = _repositoryFactory.CreateRepository <ToggleConfig>(_unitOfWork);
            var applicationRepository  = _repositoryFactory.CreateRepository <Application>(_unitOfWork) as ApplicationRepository;


            // define toggle
            var toggle = new ToggleBuilder().WithId(toogDto.Id ?? Guid.NewGuid().ToString())
                         .WithName(toogDto.Name)
                         .Build();

            // get apps from database to set the new toggle
            var apps = applicationRepository.GetApplications(toogDto.Applications.Select(c => c.Id).ToList());

            foreach (var app in apps)
            {
                app.IsToggleServiceAllowed = true;
                var toggleConfig = new ToggleConfig
                {
                    Id            = Guid.NewGuid().ToString(),
                    Version       = toogDto.Version,
                    Value         = true,
                    ToggleId      = toggle.Id,
                    Toggle        = toggle,
                    ApplicationId = app.Id,
                    Application   = app,
                    CreatedAt     = DateTime.UtcNow,
                    UpdateAt      = DateTime.UtcNow
                };
                if (toggle.Configs == null)
                {
                    toggle.Configs = new List <ToggleConfig>();
                }
                if (app.Configs == null)
                {
                    app.Configs = new List <ToggleConfig>();
                }

                // set toggle configs
                app.Configs.Add(toggleConfig);
                toggle.Configs.Add(toggleConfig);
                applicationRepository.Update(app);
                toggleConfigRepository.Insert(toggleConfig);
            }

            toggleRepository.Insert(toggle);
            await _unitOfWork.SaveAsync();

            return(await Task.FromResult(new Response <ToggleDto> {
                IsValid = true, Result = ToToggleDto(toggle)
            }));
        }