Ejemplo n.º 1
0
        private async Task <string> TrySaveFlags(FeatureFlagsViewModel model, string contentId)
        {
            var flags = new FeatureFlags(
                model.Features.ToDictionary(f => f.Name, f => f.Status),
                model.Flights.ToDictionary(f => f.Name, f => f.AsFlight()));

            var result = await _storage.TrySaveAsync(flags, contentId);

            switch (result)
            {
            case FeatureFlagSaveResult.Ok:
                // The flags have been persisted. Refresh this instance's cache immediately.
                await _cache.RefreshAsync();

                var refreshSeconds = _config.FeatureFlagsRefreshInterval.TotalSeconds;
                TempData["Message"] = $"Your feature flags have been saved! It may take up to {refreshSeconds} seconds for this change to propagate everywhere.";
                return(null);

            case FeatureFlagSaveResult.Conflict:
                return("Your changes were not applied as the feature flags were modified by someone else. Please reload the page and try again.");

            default:
                return($"Unknown save result '{result}'.");
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Index(FeatureFlagsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _storage.TrySaveAsync(model.Flags, model.ContentId);

                switch (result.Type)
                {
                case FeatureFlagSaveResultType.Ok:
                    // The flags have been persisted. Refresh this instance's cache immediately.
                    await _cache.RefreshAsync();

                    var refreshSeconds = _config.FeatureFlagsRefreshInterval.TotalSeconds;

                    TempData["Message"] = $"Your feature flags have been saved! It may take up to {refreshSeconds} seconds for this change to propagate everywhere.";
                    return(Redirect(Url.Action(actionName: "Index", controllerName: "Features")));

                case FeatureFlagSaveResultType.Conflict:
                    TempData["ErrorMessage"] = "Your changes were not applied as the feature flags were modified by someone else. " +
                                               "Please reload the page and try again.";
                    break;

                case FeatureFlagSaveResultType.Invalid:
                    ModelState.AddModelError(nameof(model.Flags), $"Invalid flags: {result.Message}");
                    break;

                default:
                    TempData["ErrorMessage"] = $"Unknown save result '{result}': {result.Message}.";
                    break;
                }
            }

            return(View(model));
        }