Beispiel #1
0
        private async Task <SettingInfo> InsertOrUpdateOrDeleteSettingValueAsync(string name, string value, int?tenantId, long?userId)
        {
            var settingDefinition = this._settingDefinitionManager.GetSettingDefinition(name);
            var settingValue      = await this.SettingStore.GetSettingOrNullAsync(tenantId, userId, name);

            //Determine defaultValue
            var defaultValue = settingDefinition.DefaultValue;

            if (settingDefinition.IsInherited)
            {
                //For Tenant and User, Application's value overrides Setting Definition's default value.
                if (tenantId.HasValue || userId.HasValue)
                {
                    var applicationValue = await this.GetSettingValueForApplicationOrNullAsync(name);

                    if (applicationValue != null)
                    {
                        defaultValue = applicationValue.Value;
                    }
                }

                //For User, Tenants's value overrides Application's default value.
                if (userId.HasValue && tenantId.HasValue)
                {
                    var tenantValue = await this.GetSettingValueForTenantOrNullAsync(tenantId.Value, name);

                    if (tenantValue != null)
                    {
                        defaultValue = tenantValue.Value;
                    }
                }
            }

            //No need to store on database if the value is the default value
            if (value == defaultValue)
            {
                if (settingValue != null)
                {
                    await this.SettingStore.DeleteAsync(settingValue);
                }

                return(null);
            }

            //If it's not default value and not stored on database, then insert it
            if (settingValue == null)
            {
                settingValue = new SettingInfo
                {
                    TenantId = tenantId,
                    UserId   = userId,
                    Name     = name,
                    Value    = value
                };

                await this.SettingStore.CreateAsync(settingValue);

                return(settingValue);
            }

            //It's same value in database, no need to update
            if (settingValue.Value == value)
            {
                return(settingValue);
            }

            //Update the setting on database.
            settingValue.Value = value;
            await this.SettingStore.UpdateAsync(settingValue);

            return(settingValue);
        }