Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestfulApi增加了批量更新接口 #111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,38 @@ public async Task<IActionResult> Edit(string id, [FromBody] ApiConfigVM model, s
obj.message
});
}
/// <summary>
/// 合并修改配置
/// </summary>
/// <param name="data">模型</param>
/// <param name="appId" >配置id</param>
/// <param name="env">环境</param>
/// <returns></returns>
[TypeFilter(typeof(AdmBasicAuthenticationAttribute))]
[TypeFilter(typeof(PremissionCheckByBasicAttribute), Arguments = new object[] { "Config.MergeUpdate", Functions.Config_MergeUpdate })]
[HttpPost("/api/Config/MergeUpdate")]
public async Task<IActionResult> MergeUpdate([FromBody] SaveJsonVM data, string appId, string env)
{
if (string.IsNullOrEmpty(appId))
{
throw new ArgumentNullException(nameof(appId));
}

if (data == null)
{
throw new ArgumentNullException(nameof(data));
}

if (string.IsNullOrEmpty(data.json))
{
throw new ArgumentNullException("data.json");
}
var result = await _configService.MergeUpdateJsonAsync(data.json, appId, env);
return Json(new
{
success = result
});
}
/// <summary>
/// 删除一个配置
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions AgileConfig.Server.IService/IConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public interface IConfigService: IDisposable
Task<bool> UpdateAsync(Config config, string env);

Task<bool> UpdateAsync(List<Config> configs, string env);
/// <summary>
/// 合并更新配置
/// </summary>
/// <param name="json"></param>
/// <param name="appId"></param>
/// <param name="env"></param>
/// <returns></returns>
Task<bool> MergeUpdateJsonAsync(string json, string appId, string env);

/// <summary>
/// 撤销编辑状态
Expand Down
1 change: 1 addition & 0 deletions AgileConfig.Server.IService/IPremissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Functions

public const string Config_Add = "CONFIG_ADD";
public const string Config_Edit = "CONFIG_EDIT";
public const string Config_MergeUpdate = "CONFIG_MERGEUPDATE";
public const string Config_Delete = "CONFIG_DELETE";

public const string Config_Publish = "CONFIG_PUBLISH";
Expand Down
95 changes: 95 additions & 0 deletions AgileConfig.Server.Service/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,101 @@ public async Task<bool> UpdateAsync(List<Config> configs, string env)
return result;
}

public async Task<bool> MergeUpdateJsonAsync(string json, string appId, string env)
{
if (string.IsNullOrEmpty(json))
{
throw new ArgumentNullException("json");
}


byte[] byteArray = Encoding.UTF8.GetBytes(json);
using var stream = new MemoryStream(byteArray);
var dict = JsonConfigurationFileParser.Parse(stream);
//2.比对数据库
// 如果key不存在,就创建
// 如果key存在,并且值不同,就覆盖
// 如果key存在,并且值相同,不做任何处理
using var dbcontext = FreeSqlDbContextFactory.Create(env);

var currentConfigs = await dbcontext.Configs
.Where(x => x.AppId == appId && x.Env == env && x.Status == ConfigStatus.Enabled).ToListAsync();
var addConfigs = new List<Config>();
var updateConfigs = new List<Config>();

var now = DateTime.Now;

foreach (var kv in dict)
{
var key = kv.Key;
var value = kv.Value;
var config = currentConfigs.FirstOrDefault(x => GenerateKey(x) == key);
if (config == null)
{
var gk = SplitJsonKey(key);
addConfigs.Add(new Config
{
Id = Guid.NewGuid().ToString("N"),
AppId = appId,
Env = env,
Key = gk.Item2,
Group = gk.Item1,
Value = value,
CreateTime = now,
Status = ConfigStatus.Enabled,
EditStatus = EditStatus.Add,
OnlineStatus = OnlineStatus.WaitPublish
});
}
else if (config.Value != kv.Value)
{
config.Value = value;
config.UpdateTime = now;
if (config.OnlineStatus == OnlineStatus.Online)
{
config.EditStatus = EditStatus.Edit;
config.OnlineStatus = OnlineStatus.WaitPublish;
}
else
{
if (config.EditStatus == EditStatus.Add)
{
//do nothing
}

if (config.EditStatus == EditStatus.Edit)
{
//do nothing
}

if (config.EditStatus == EditStatus.Deleted)
{
//上一次是删除状态,现在恢复为编辑状态
config.EditStatus = EditStatus.Edit;
}
}

updateConfigs.Add(config);
}
}


if (addConfigs.Count > 0)
{
await dbcontext.Configs.AddRangeAsync(addConfigs);
}

if (updateConfigs.Count > 0)
{
await dbcontext.Configs.UpdateRangeAsync(updateConfigs);
}

await dbcontext.SaveChangesAsync();

return true;
}


public async Task<bool> CancelEdit(List<string> ids, string env)
{
using var dbcontext = FreeSqlDbContextFactory.Create(env);
Expand Down
3 changes: 3 additions & 0 deletions AgileConfig.Server.Service/PremissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public PermissionService(FreeSqlContext freeSql)
"GLOBAL_" + Functions.Config_Add,
"GLOBAL_" + Functions.Config_Delete,
"GLOBAL_" + Functions.Config_Edit,
"GLOBAL_" + Functions.Config_MergeUpdate,
"GLOBAL_" + Functions.Config_Offline,
"GLOBAL_" + Functions.Config_Publish,

Expand Down Expand Up @@ -56,6 +57,7 @@ public PermissionService(FreeSqlContext freeSql)
"APP_{0}_" + Functions.Config_Add,
"APP_{0}_" + Functions.Config_Delete,
"APP_{0}_" + Functions.Config_Edit,
"APP_{0}_" + Functions.Config_MergeUpdate,
"APP_{0}_" + Functions.Config_Offline,
"APP_{0}_" + Functions.Config_Publish,
};
Expand All @@ -65,6 +67,7 @@ public PermissionService(FreeSqlContext freeSql)
"APP_{0}_" + Functions.Config_Add,
"APP_{0}_" + Functions.Config_Delete,
"APP_{0}_" + Functions.Config_Edit,
"APP_{0}_" + Functions.Config_MergeUpdate,
};

private static readonly List<string> Template_NormalUserPermissions_Publish = new List<string>
Expand Down