void AddData(StoreDbContext dbContext)
        {
            if (!dbContext.Products.Any())
            {
                var product = new ProductEntity("default", "description");
                dbContext.Products.Add(product);

                dbContext.SaveChanges();

                var appFeature = new FeatureEntity(product.Id, "app-feature", enabled: true);

                var toggle = new ToggleEntity(appFeature.Id, "Esquio.Toggles.XToggle");

                var stringparameter = new ParameterEntity(toggle.Id, "strparam", "value1");
                toggle.Parameters.Add(stringparameter);

                var intparameter = new ParameterEntity(toggle.Id, "intparam", "1");
                toggle.Parameters.Add(intparameter);

                appFeature.Toggles.Add(toggle);

                dbContext.Features.Add(appFeature);

                dbContext.SaveChanges();
            }
        }
示例#2
0
        public async Task <int> Handle(AddToggleRequest request, CancellationToken cancellationToken)
        {
            var feature = await _storeDbContext
                          .Features
                          .Include(t => t.Toggles)
                          .Where(t => t.Id == request.FeatureId)
                          .SingleOrDefaultAsync(cancellationToken);


            if (feature != null)
            {
                var alreadyExistToggleType = feature.Toggles
                                             .Any(t => t.Type == request.ToggleType);

                if (!alreadyExistToggleType)
                {
                    var toggle = new ToggleEntity(feature.Id, request.ToggleType);

                    foreach (var item in request.Parameters)
                    {
                        toggle.Parameters.Add(
                            new ParameterEntity(toggle.Id, item.Name, item.Value));
                    }

                    feature.Toggles.Add(toggle);
                    await _storeDbContext.SaveChangesAsync(cancellationToken);

                    return(toggle.Id);
                }

                Log.ToggleTypeAlreadyExist(_logger, request.ToggleType, feature.Name);
                throw new InvalidOperationException($"Toggle with type {request.ToggleType} already exist on this feature.");
            }

            Log.FeatureNotExist(_logger, request.FeatureId.ToString());
            throw new InvalidOperationException($"The feature with id {request.FeatureId} does not exist in the store.");
        }
示例#3
0
        public async Task <int> Handle(AddToggleRequest request, CancellationToken cancellationToken)
        {
            var feature = await _storeDbContext
                          .Features
                          .Include(f => f.ProductEntity) //-> this is only needed for "history"
                          .Include(t => t.Toggles)
                          .Where(t => t.Name == request.FeatureName && t.ProductEntity.Name == request.ProductName)
                          .SingleOrDefaultAsync(cancellationToken);

            if (feature != null)
            {
                var alreadyExistToggleType = feature.Toggles
                                             .Any(t => t.Type == request.ToggleType);

                if (!alreadyExistToggleType)
                {
                    var toggle = new ToggleEntity(feature.Id, request.ToggleType);

                    var allowedRings = await _storeDbContext
                                       .Deployments
                                       .Where(p => p.ProductEntityId == feature.ProductEntityId)
                                       .ToListAsync();

                    var defaultRing = allowedRings
                                      .Where(r => r.ByDefault)
                                      .SingleOrDefault();

                    var selectedRing = defaultRing;

                    if (!string.IsNullOrEmpty(request.DeploymentName))
                    {
                        selectedRing = allowedRings
                                       .Where(r => r.Name == request.DeploymentName)
                                       .SingleOrDefault();
                    }

                    if (selectedRing != null)
                    {
                        foreach (var item in request.Parameters)
                        {
                            toggle.AddOrUpdateParameter(selectedRing, defaultRing, item.Name, item.Value);
                        }

                        feature.Toggles
                        .Add(toggle);

                        await _storeDbContext.SaveChangesAsync(cancellationToken);

                        return(toggle.Id);
                    }

                    Log.RingNotExist(_logger, request.DeploymentName, request.ProductName);
                    throw new InvalidOperationException($"Deployment {request.DeploymentName} does not exist for product {request.ProductName}.");
                }

                Log.ToggleTypeAlreadyExist(_logger, request.ToggleType, feature.Name);
                throw new InvalidOperationException($"Toggle with type {request.ToggleType} already exist on this feature.");
            }

            Log.FeatureNotExist(_logger, request.FeatureName);
            throw new InvalidOperationException($"The feature {request.FeatureName} does not exist in the store.");
        }
示例#4
0
 public ParameterEntityBuilder WithToggle(ToggleEntity toggle)
 {
     _toggleId = toggle.Id;
     return(this);
 }