示例#1
0
        // Helper methods

        //private async Task<GetEntityResponse<Settings>> GetImpl(GetByIdArguments args)
        //{
        //    var settings = await _repo.Settings
        //        .Select(args.Select)
        //        .Expand(args.Expand)
        //        .OrderBy("PrimaryLanguageId")
        //        .FirstOrDefaultAsync();

        //    if (settings == null)
        //    {
        //        // Programmer mistake
        //        throw new BadRequestException("Settings have not been initialized");
        //    }

        //    var result = new GetEntityResponse<Settings>
        //    {
        //        Result = settings,
        //    };

        //    return result;
        //}

        //private void ValidateAndPreprocessSettings(SettingsForSave entity)
        //{
        //    if (!string.IsNullOrWhiteSpace(entity.SecondaryLanguageId) || !string.IsNullOrWhiteSpace(entity.TernaryLanguageId))
        //    {
        //        if (string.IsNullOrWhiteSpace(entity.PrimaryLanguageSymbol))
        //        {
        //            ModelState.AddModelError(nameof(entity.PrimaryLanguageSymbol),
        //                _localizer[Services.Utilities.Constants.Error_RequiredField0, _localizer["Settings_PrimaryLanguageSymbol"]]);
        //        }
        //    }

        //    if (string.IsNullOrWhiteSpace(entity.SecondaryLanguageId))
        //    {
        //        entity.SecondaryLanguageSymbol = null;
        //    }
        //    else
        //    {
        //        if (string.IsNullOrWhiteSpace(entity.SecondaryLanguageSymbol))
        //        {
        //            ModelState.AddModelError(nameof(entity.SecondaryLanguageSymbol),
        //                _localizer[Services.Utilities.Constants.Error_RequiredField0, _localizer["Settings_SecondaryLanguageSymbol"]]);
        //        }

        //        if (entity.SecondaryLanguageId == entity.PrimaryLanguageId)
        //        {
        //            ModelState.AddModelError(nameof(entity.SecondaryLanguageId),
        //                _localizer["Error_SecondaryLanguageCannotBeTheSameAsPrimaryLanguage"]);
        //        }
        //    }

        //    if (string.IsNullOrWhiteSpace(entity.TernaryLanguageId))
        //    {
        //        entity.TernaryLanguageSymbol = null;
        //    }
        //    else
        //    {
        //        if (string.IsNullOrWhiteSpace(entity.TernaryLanguageSymbol))
        //        {
        //            ModelState.AddModelError(nameof(entity.TernaryLanguageSymbol),
        //                _localizer[Services.Utilities.Constants.Error_RequiredField0, _localizer["Settings_TernaryLanguageSymbol"]]);
        //        }

        //        if (entity.TernaryLanguageId == entity.PrimaryLanguageId)
        //        {
        //            ModelState.AddModelError(nameof(entity.TernaryLanguageId),
        //                _localizer["Error_TernaryLanguageCannotBeTheSameAsPrimaryLanguage"]);
        //        }

        //        if (entity.TernaryLanguageId == entity.SecondaryLanguageId)
        //        {
        //            ModelState.AddModelError(nameof(entity.TernaryLanguageId),
        //                _localizer["Error_TernaryLanguageCannotBeTheSameAsSecondaryLanguage"]);
        //        }
        //    }

        //    // Make sure the color is a valid HTML color
        //    // Credit: https://bit.ly/2ToV6x4
        //    if (!string.IsNullOrWhiteSpace(entity.BrandColor) && !Regex.IsMatch(entity.BrandColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
        //    {
        //        ModelState.AddModelError(nameof(entity.BrandColor),
        //            _localizer["Error_TheField0MustBeAValidColorFormat", _localizer["Settings_BrandColor"]]);
        //    }
        //}

        public static async Task <Versioned <AdminSettingsForClient> > LoadSettingsForClient(AdminRepository repo, CancellationToken cancellation)
        {
            var settings = await repo.Settings__Load(cancellation);

            if (settings == null)
            {
                // This should never happen
                throw new BadRequestException("AdminSettings have not been initialized");
            }

            // Prepare the settings for client
            AdminSettingsForClient settingsForClient = new AdminSettingsForClient();

            foreach (var forClientProp in typeof(AdminSettingsForClient).GetProperties())
            {
                var settingsProp = typeof(AdminSettings).GetProperty(forClientProp.Name);
                if (settingsProp != null)
                {
                    var value = settingsProp.GetValue(settings);
                    forClientProp.SetValue(settingsForClient, value);
                }
            }

            // Tag the settings for client with their current version
            var result = new Versioned <AdminSettingsForClient>
                         (
                version: settings.SettingsVersion.ToString(),
                data: settingsForClient
                         );

            return(result);
        }
示例#2
0
        public async Task <Versioned <AdminSettingsForClient> > SettingsForClient(CancellationToken cancellation)
        {
            await Initialize(cancellation);

            // Simply retrieves the cached settings, which were refreshed by AdminApiAttribute
            var adminSettings = await _repo.Settings__Load(cancellation);

            if (adminSettings == null)
            {
                throw new ServiceException("Admin Settings were not initialized.");
            }

            var adminSettingsForClient = new AdminSettingsForClient
            {
                CreatedAt = adminSettings.CreatedAt
            };

            var result = new Versioned <AdminSettingsForClient>
                         (
                data: adminSettingsForClient,
                version: adminSettings.SettingsVersion.ToString()
                         );

            return(result);
        }