Merge pull request #5619 from sbwalker/dev
improve performance of UpdateSettingsAsync method
This commit is contained in:
@ -429,49 +429,20 @@ namespace Oqtane.Services
|
|||||||
|
|
||||||
public async Task UpdateSettingsAsync(Dictionary<string, string> settings, string entityName, int entityId)
|
public async Task UpdateSettingsAsync(Dictionary<string, string> settings, string entityName, int entityId)
|
||||||
{
|
{
|
||||||
var settingsList = await GetSettingsAsync(entityName, entityId, "");
|
var settingsList = new List<Setting>();
|
||||||
|
|
||||||
foreach (KeyValuePair<string, string> kvp in settings)
|
foreach (KeyValuePair<string, string> kvp in settings)
|
||||||
{
|
{
|
||||||
string value = kvp.Value;
|
var setting = new Setting();
|
||||||
bool modified = false;
|
setting.EntityName = entityName;
|
||||||
bool isprivate = false;
|
setting.EntityId = entityId;
|
||||||
|
setting.SettingName = kvp.Key;
|
||||||
// manage settings modified with SetSetting method
|
setting.SettingValue = kvp.Value;
|
||||||
if (value.StartsWith("[Private]"))
|
setting.IsPrivate = true;
|
||||||
{
|
settingsList.Add(setting);
|
||||||
modified = true;
|
|
||||||
isprivate = true;
|
|
||||||
value = value.Substring(9);
|
|
||||||
}
|
|
||||||
if (value.StartsWith("[Public]"))
|
|
||||||
{
|
|
||||||
modified = true;
|
|
||||||
isprivate = false;
|
|
||||||
value = value.Substring(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
Setting setting = settingsList.FirstOrDefault(item => item.SettingName.Equals(kvp.Key, StringComparison.OrdinalIgnoreCase));
|
|
||||||
if (setting == null)
|
|
||||||
{
|
|
||||||
setting = new Setting();
|
|
||||||
setting.EntityName = entityName;
|
|
||||||
setting.EntityId = entityId;
|
|
||||||
setting.SettingName = kvp.Key;
|
|
||||||
setting.SettingValue = value;
|
|
||||||
setting.IsPrivate = isprivate;
|
|
||||||
setting = await AddSettingAsync(setting);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (setting.SettingValue != value || (modified && setting.IsPrivate != isprivate))
|
|
||||||
{
|
|
||||||
setting.SettingValue = value;
|
|
||||||
setting.IsPrivate = isprivate;
|
|
||||||
setting = await UpdateSettingAsync(setting);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await PutJsonAsync<List<Setting>>($"{Apiurl}/{entityName}/{entityId}", settingsList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddOrUpdateSettingAsync(string entityName, int entityId, string settingName, string settingValue, bool isPrivate)
|
public async Task AddOrUpdateSettingAsync(string entityName, int entityId, string settingName, string settingValue, bool isPrivate)
|
||||||
|
|||||||
@ -17,6 +17,8 @@ using Microsoft.Extensions.Options;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Oqtane.Migrations.Tenant;
|
using Oqtane.Migrations.Tenant;
|
||||||
|
using Google.Protobuf.WellKnownTypes;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Oqtane.Controllers
|
namespace Oqtane.Controllers
|
||||||
{
|
{
|
||||||
@ -204,7 +206,60 @@ namespace Oqtane.Controllers
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Error, this, LogFunction.Update, "User Not Authorized To Add Or Update Setting For EntityName {EntityName} EntityId {EntityId} SettingName {SettingName}", entityName, entityId, settingName);
|
_logger.Log(LogLevel.Error, this, LogFunction.Update, "User Not Authorized To Add Or Update Setting For {EntityName}:{EntityId}", entityName, entityId);
|
||||||
|
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT api/<controller>/site/1
|
||||||
|
[HttpPut("{entityName}/{entityId}")]
|
||||||
|
public void Put(string entityName, int entityId, [FromBody] List<Setting> settings)
|
||||||
|
{
|
||||||
|
if (ModelState.IsValid && IsAuthorized(entityName,entityId, PermissionNames.Edit))
|
||||||
|
{
|
||||||
|
var existingSettings = _settings.GetSettings(entityName, entityId).ToList();
|
||||||
|
foreach (Setting setting in settings)
|
||||||
|
{
|
||||||
|
bool modified = false;
|
||||||
|
|
||||||
|
// manage settings modified with SetSetting method
|
||||||
|
if (setting.SettingValue.StartsWith("[Private]"))
|
||||||
|
{
|
||||||
|
modified = true;
|
||||||
|
setting.IsPrivate = true;
|
||||||
|
setting.SettingValue = setting.SettingValue.Substring(9);
|
||||||
|
}
|
||||||
|
if (setting.SettingValue.StartsWith("[Public]"))
|
||||||
|
{
|
||||||
|
modified = true;
|
||||||
|
setting.IsPrivate = false;
|
||||||
|
setting.SettingValue = setting.SettingValue.Substring(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
Setting existingSetting = existingSettings.FirstOrDefault(item => item.SettingName.Equals(setting.SettingName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (existingSetting == null)
|
||||||
|
{
|
||||||
|
_settings.AddSetting(setting);
|
||||||
|
AddSyncEvent(setting.EntityName, setting.EntityId, setting.SettingId, SyncEventActions.Create);
|
||||||
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Setting Created {Setting}", setting);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (existingSetting.SettingValue != setting.SettingValue || (modified && existingSetting.IsPrivate != setting.IsPrivate))
|
||||||
|
{
|
||||||
|
existingSetting.SettingValue = setting.SettingValue;
|
||||||
|
existingSetting.IsPrivate = setting.IsPrivate;
|
||||||
|
_settings.UpdateSetting(existingSetting);
|
||||||
|
AddSyncEvent(setting.EntityName, setting.EntityId, setting.SettingId, SyncEventActions.Update);
|
||||||
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Setting Updated {Setting}", setting);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Log(LogLevel.Error, this, LogFunction.Update, "User Not Authorized To Update Settings For {EntityName}:{EntityId}", entityName, entityId);
|
||||||
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user