Пример #1
0
 /// <summary>
 /// Alloweds for all applications.
 /// </summary>
 /// <param name="applications">The applications.</param>
 /// <returns>The ToggleBuilder object.</returns>
 public ToggleBuilder AllowedForAllApplications(IEnumerable <Application> applications)
 {
     if (applications == null)
     {
         throw new ArgumentNullException(nameof(applications));
     }
     foreach (var app in applications)
     {
         var toggleConfig = new ToggleConfig
         {
             Id            = Guid.NewGuid().ToString(),
             Version       = "v1",
             ApplicationId = app.Id,
             ToggleId      = _toggle.Id,
             Application   = app,
             Toggle        = _toggle,
             Value         = true,
             CreatedAt     = DateTime.UtcNow,
             UpdateAt      = DateTime.UtcNow
         };
         if (_toggle.Configs == null)
         {
             _toggle.Configs = new List <ToggleConfig>();
         }
         if (app.Configs == null)
         {
             app.Configs = new List <ToggleConfig>();
         }
         _toggle.Configs.Add(toggleConfig);
         app.Configs.Add(toggleConfig);
     }
     return(this);
 }
        private IFeatureToggle ConvertToFeatureToggle(ToggleConfig config)
        {
            IFeatureToggle toggle;

            if (config.IsEstablished)
            {
                toggle = new EstablishedFeatureToggle(config.Name);
            }
            else if ((config.FromDate != null) || (config.ToDate != null))
            {
                toggle = new DateRangeToggle(config.Name, config.Enabled, config.FromDate, config.ToDate);
            }
            else
            {
                toggle = new BooleanToggle(config.Name, config.Enabled);
            }

            return string.IsNullOrEmpty(config.Dependencies)
                ? toggle
                : new DependencyToggle(toggle);
        }
Пример #3
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)
            }));
        }