diff --git a/src/AgileConfig.Server.Apisite/Controllers/ConfigController.cs b/src/AgileConfig.Server.Apisite/Controllers/ConfigController.cs index 673ce66c..0a073d49 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/ConfigController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/ConfigController.cs @@ -45,7 +45,7 @@ public async Task Add([FromBody] ConfigVM model, [FromQuery] stri { ArgumentNullException.ThrowIfNull(model); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var app = await _appService.GetAsync(model.AppId); if (app == null) @@ -106,7 +106,7 @@ public async Task AddRange([FromBody] List model, [From throw new ArgumentNullException("model"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetByAppIdAsync(model.First().AppId, env); @@ -177,7 +177,7 @@ public async Task Edit([FromBody] ConfigVM model, [FromQuery] str throw new ArgumentNullException("model"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var config = await _configService.GetAsync(model.Id, env); if (config == null) @@ -272,7 +272,7 @@ private bool IsOnlyUpdateDescription(Config newConfig, Config oldConfig) [HttpGet] public async Task All(string env) { - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetAllConfigsAsync(env); @@ -307,7 +307,7 @@ public async Task Search(string appId, string group, string key, throw new ArgumentException("pageIndex can not less then 1 ."); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.Search(appId, group, key, env); configs = configs.Where(c => c.Status == ConfigStatus.Enabled).ToList(); @@ -361,7 +361,7 @@ public async Task Get(string id, string env) throw new ArgumentNullException("id"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var config = await _configService.GetAsync(id, env); @@ -383,7 +383,7 @@ public async Task Delete(string id, string env) throw new ArgumentNullException("id"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var config = await _configService.GetAsync(id, env); if (config == null) @@ -428,7 +428,7 @@ public async Task DeleteSome([FromBody] List ids, string throw new ArgumentNullException("ids"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); List deleteConfigs = new List(); @@ -481,7 +481,7 @@ public async Task Rollback(string publishTimelineId, string env) throw new ArgumentNullException("publishTimelineId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var result = await _configService.RollbackAsync(publishTimelineId, env); @@ -506,7 +506,7 @@ public async Task ConfigPublishedHistory(string configId, string throw new ArgumentNullException("configId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configPublishedHistory = await _configService.GetConfigPublishedHistory(configId, env); var result = new List(); @@ -549,7 +549,7 @@ public async Task Publish([FromBody] PublishLogVM model, string e throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var appId = model.AppId; var userId = await this.GetCurrentUserId(_userService); @@ -631,7 +631,7 @@ public async Task ExportJson(string appId, string env) throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetByAppIdAsync(appId, env); @@ -659,7 +659,7 @@ public async Task WaitPublishStatus(string appId, string env) throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.Search(appId, "", "", env); configs = configs.Where(x => x.Status == ConfigStatus.Enabled && x.EditStatus != EditStatus.Commit) @@ -693,7 +693,7 @@ public async Task PublishHistory(string appId, string env) throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var history = await _configService.GetPublishDetailListAsync(appId, env); @@ -724,7 +724,7 @@ public async Task CancelEdit(string configId, string env) throw new ArgumentNullException("configId"); } - env = _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var result = await _configService.CancelEdit(new List() { configId }, env); @@ -747,7 +747,7 @@ public async Task CancelSomeEdit([FromBody] List ids, str throw new ArgumentNullException("ids"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var result = await _configService.CancelEdit(ids, env); @@ -809,7 +809,7 @@ public async Task GetKvList(string appId, string env) throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetByAppIdAsync(appId, env); // text 格式展示的时候不需要删除的配置 @@ -840,7 +840,7 @@ public async Task GetJson(string appId, string env) throw new ArgumentNullException("appId"); } - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetByAppIdAsync(appId, env); // json 格式展示的时候不需要删除的配置 diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs index aff9f058..46df0a1d 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs @@ -249,7 +249,7 @@ public async Task Delete(string id) [HttpPost("publish")] public async Task Publish(string appId, string env) { - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var ctrl = new Controllers.ConfigController( _configService, @@ -289,7 +289,7 @@ public async Task>> PublishHistor { ArgumentNullException.ThrowIfNullOrEmpty(appId); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var history = await _configService.GetPublishTimelineHistoryAsync(appId, env); @@ -319,7 +319,7 @@ public async Task>> PublishHistor [HttpPost("rollback")] public async Task Rollback(string historyId, string env) { - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var ctrl = new Controllers.ConfigController( _configService, diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs index 3198e9d2..9dd62cb6 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs @@ -56,7 +56,7 @@ public async Task>> GetAppConfig(string appId, [F { ArgumentNullException.ThrowIfNullOrEmpty(appId); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var app = await _appService.GetAsync(appId); if (!app.Enabled) @@ -110,7 +110,7 @@ public async Task>> GetConfigs(string appId, stri { ArgumentNullException.ThrowIfNullOrEmpty(appId); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var configs = await _configService.GetByAppIdAsync(appId, env); @@ -140,7 +140,7 @@ public async Task> GetConfig(string id, string env) { ArgumentNullException.ThrowIfNullOrEmpty(id); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var config = await _configService.GetAsync(id, env); if (config == null || config.Status == ConfigStatus.Deleted) @@ -193,7 +193,7 @@ public async Task Add([FromBody] ApiConfigVM model, string env) ); ctrl.ControllerContext.HttpContext = HttpContext; - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var result = (await ctrl.Add(new ConfigVM() { @@ -250,7 +250,7 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, s ); ctrl.ControllerContext.HttpContext = HttpContext; - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); model.Id = id; var result = (await ctrl.Edit(new ConfigVM() @@ -296,7 +296,7 @@ public async Task Delete(string id, string env) ); ctrl.ControllerContext.HttpContext = HttpContext; - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var result = (await ctrl.Delete(id, env)) as JsonResult; diff --git a/src/AgileConfig.Server.Apisite/Filters/PremissionCheckAttribute.cs b/src/AgileConfig.Server.Apisite/Filters/PremissionCheckAttribute.cs index a3f7c377..75a74f3f 100644 --- a/src/AgileConfig.Server.Apisite/Filters/PremissionCheckAttribute.cs +++ b/src/AgileConfig.Server.Apisite/Filters/PremissionCheckAttribute.cs @@ -21,7 +21,7 @@ private static string GetEnvFromArgs(IDictionary args, IConfigSe var env = ""; if (envArg == null) { - configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); } else { diff --git a/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/MessageHandler.cs b/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/MessageHandler.cs index c6cbc1c4..211cc9d2 100644 --- a/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/MessageHandler.cs +++ b/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/MessageHandler.cs @@ -66,7 +66,7 @@ public async Task Handle(string message, HttpRequest request, WebsocketClient cl var appId = request.Headers["appid"]; appId = HttpUtility.UrlDecode(appId); var env = request.Headers["env"].ToString(); - _configService.IfEnvEmptySetDefault(ref env); + ISettingService.IfEnvEmptySetDefault(ref env); var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritanced(appId, env); await SendMessage(client.Client, JsonConvert.SerializeObject(new WebsocketAction() { diff --git a/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/OldMessageHandler.cs b/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/OldMessageHandler.cs index f5eaaae7..e7d56f37 100644 --- a/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/OldMessageHandler.cs +++ b/src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/OldMessageHandler.cs @@ -45,7 +45,7 @@ public async Task Handle(string message, HttpRequest request, WebsocketClient cl //如果是ping,回复本地数据的md5版本 var appId = request.Headers["appid"]; var env = request.Headers["env"].ToString(); - env = _configService.IfEnvEmptySetDefault(ref env); + env = ISettingService.IfEnvEmptySetDefault(ref env); var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritanced(appId, env); await SendMessage(client.Client, $"V:{md5}"); } diff --git a/src/AgileConfig.Server.IService/IConfigService.cs b/src/AgileConfig.Server.IService/IConfigService.cs index 180525be..2e7dd22c 100644 --- a/src/AgileConfig.Server.IService/IConfigService.cs +++ b/src/AgileConfig.Server.IService/IConfigService.cs @@ -7,13 +7,6 @@ namespace AgileConfig.Server.IService { public interface IConfigService: IDisposable { - /// - /// 如果环境为空,返回默认环境,默认环境为数据库设置的环境列表的第一个 - /// - /// - /// - string IfEnvEmptySetDefault(ref string env); - /// /// 发布当前待发布的配置项,如果传ids那么就是只发布这些项目,如果不传ids,那么就是全部发布 /// diff --git a/src/AgileConfig.Server.IService/ISettingService.cs b/src/AgileConfig.Server.IService/ISettingService.cs index 2cf996a6..c6a3af54 100644 --- a/src/AgileConfig.Server.IService/ISettingService.cs +++ b/src/AgileConfig.Server.IService/ISettingService.cs @@ -5,10 +5,27 @@ namespace AgileConfig.Server.IService { - public interface ISettingService: IDisposable + public interface ISettingService : IDisposable { static string[] EnvironmentList { get; set; } + static string IfEnvEmptySetDefault(ref string env) + { + if (!string.IsNullOrEmpty(env)) + { + return env; + } + + if (EnvironmentList == null || EnvironmentList.Length == 0) + { + throw new ArgumentNullException(nameof(EnvironmentList)); + } + + env = EnvironmentList[0]; + + return env; + } + Task GetAsync(string id); Task AddAsync(Setting setting); diff --git a/src/AgileConfig.Server.Service/ConfigService.cs b/src/AgileConfig.Server.Service/ConfigService.cs index 5a22594b..d5dd4632 100644 --- a/src/AgileConfig.Server.Service/ConfigService.cs +++ b/src/AgileConfig.Server.Service/ConfigService.cs @@ -47,22 +47,6 @@ public ConfigService(IMemoryCache memoryCache, _publishTimelineRepositoryAccsssor = publishTimelineRepositoryAccessor; } - public string IfEnvEmptySetDefault(ref string env) - { - if (!string.IsNullOrEmpty(env)) - { - return env; - } - - var envList = ISettingService.EnvironmentList; - if (envList == null || envList.Length == 0) - { - return ""; - } - - return envList[0]; - } - public async Task AddAsync(Config config, string env) { if (config.Value == null) diff --git a/test/AgileConfig.Server.ServiceTests/ISettingServiceTests.cs b/test/AgileConfig.Server.ServiceTests/ISettingServiceTests.cs new file mode 100644 index 00000000..251556b4 --- /dev/null +++ b/test/AgileConfig.Server.ServiceTests/ISettingServiceTests.cs @@ -0,0 +1,51 @@ +using AgileConfig.Server.IService; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AgileConfig.Server.ServiceTests +{ + [TestClass] + public class ISettingServiceTests + { + [TestMethod] + public void IfEnvEmptySetDefaultTest() + { + // Arrange + string env = "test"; + ISettingService.EnvironmentList = new string[] { "test", "prod" }; + + // Act + ISettingService.IfEnvEmptySetDefault(ref env); + + // Assert + Assert.AreEqual("test", env); + } + + [TestMethod] + public void IfEnvEmptySetDefault_envEmpty_shouldReturn_defaul() + { + // Arrange + string env = ""; + ISettingService.EnvironmentList = new string[] { "test", "prod" }; + + // Act + ISettingService.IfEnvEmptySetDefault(ref env); + + // Assert + Assert.AreEqual("test", env); + } + + [TestMethod] + public void IfEnvEmptySetDefault_envNull_shouldReturn_defaul() + { + // Arrange + string env = null; + ISettingService.EnvironmentList = new string[] { "test", "prod" }; + + // Act + ISettingService.IfEnvEmptySetDefault(ref env); + + // Assert + Assert.AreEqual("test", env); + } + } +}