diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs index 1c4aea3d..3f24a566 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs @@ -6,6 +6,7 @@ using AgileConfig.Server.Apisite.Filters; using AgileConfig.Server.Apisite.Metrics; using AgileConfig.Server.Apisite.Models; +using AgileConfig.Server.Apisite.Models.Mapping; using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Data.Entity; using AgileConfig.Server.IService; @@ -21,26 +22,23 @@ public class ConfigController : Controller { private readonly IConfigService _configService; private readonly IAppService _appService; - private readonly IUserService _userService; private readonly IMemoryCache _cacheMemory; - private readonly ITinyEventBus _tinyEventBus; private readonly IMeterService _meterService; + private readonly Controllers.ConfigController _configController; public ConfigController( IConfigService configService, IAppService appService, - IUserService userService, IMemoryCache cacheMemory, - ITinyEventBus tinyEventBus, - IMeterService meterService + IMeterService meterService, + Controllers.ConfigController configController ) { _configService = configService; _appService = appService; - _userService = userService; _cacheMemory = cacheMemory; - _tinyEventBus = tinyEventBus; _meterService = meterService; + _configController = configController; } /// @@ -52,11 +50,9 @@ IMeterService meterService /// [TypeFilter(typeof(AppBasicAuthenticationAttribute))] [HttpGet("app/{appId}")] - public async Task>> GetAppConfig(string appId, [FromQuery] string env) + public async Task>> GetAppConfig(string appId, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(appId); var app = await _appService.GetAsync(appId); if (!app.Enabled) @@ -64,7 +60,7 @@ public async Task>> GetAppConfig(string appId, [F return NotFound(); } - var cacheKey = $"ConfigController_APPCONFIG_{appId}_{env}"; + var cacheKey = $"ConfigController_AppConfig_{appId}_{env.Value}"; List configs = null; _cacheMemory?.TryGetValue(cacheKey, out configs); if (configs != null) @@ -72,21 +68,8 @@ public async Task>> GetAppConfig(string appId, [F return configs; } - var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env); - var vms = appConfigs.Select(c => - { - return new ApiConfigVM() - { - Id = c.Id, - AppId = c.AppId, - Group = c.Group, - Key = c.Key, - Value = c.Value, - Status = c.Status, - OnlineStatus = c.OnlineStatus, - EditStatus = c.EditStatus - }; - }).ToList(); + var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env.Value); + var vms = appConfigs.Select(x => x.ToApiConfigVM()).ToList(); //增加5s的缓存,防止同一个app同时启动造成db的压力过大 var cacheOp = new MemoryCacheEntryOptions() @@ -106,26 +89,13 @@ public async Task>> GetAppConfig(string appId, [F /// [TypeFilter(typeof(AdmBasicAuthenticationAttribute))] [HttpGet()] - public async Task>> GetConfigs(string appId, string env) + public async Task>> GetConfigs(string appId, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(appId); - var configs = await _configService.GetByAppIdAsync(appId, env); + var configs = await _configService.GetByAppIdAsync(appId, env.Value); - return configs.Select(config => new ApiConfigVM() - { - Id = config.Id, - AppId = config.AppId, - Group = config.Group, - Key = config.Key, - Value = config.Value, - Status = config.Status, - Description = config.Description, - OnlineStatus = config.OnlineStatus, - EditStatus = config.EditStatus - }).ToList(); + return configs.Select(x => x.ToApiConfigVM()).ToList(); } /// @@ -136,30 +106,17 @@ public async Task>> GetConfigs(string appId, stri /// [TypeFilter(typeof(AdmBasicAuthenticationAttribute))] [HttpGet("{id}")] - public async Task> GetConfig(string id, string env) + public async Task> GetConfig(string id, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(id); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(id); - var config = await _configService.GetAsync(id, env); + var config = await _configService.GetAsync(id, env.Value); if (config == null || config.Status == ConfigStatus.Deleted) { return NotFound(); } - return new ApiConfigVM() - { - Id = config.Id, - AppId = config.AppId, - Group = config.Group, - Key = config.Key, - Value = config.Value, - Status = config.Status, - Description = config.Description, - OnlineStatus = config.OnlineStatus, - EditStatus = config.EditStatus - }; + return config.ToApiConfigVM(); } /// @@ -185,27 +142,13 @@ public async Task Add([FromBody] ApiConfigVM model, EnvString env }); } - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; + _configController.ControllerContext.HttpContext = HttpContext; - var result = (await ctrl.Add(new ConfigVM() - { - Id = model.Id, - AppId = model.AppId, - Group = model.Group, - Key = model.Key, - Value = model.Value, - Description = model.Description - }, env)) as JsonResult; + var result = (await _configController.Add(model.ToConfigVM(), env)) as JsonResult; - dynamic obj = result.Value; + dynamic obj = result?.Value; - if (obj.success == true) + if (obj?.success == true) { return Created("/api/config/" + obj.data.Id, ""); } @@ -213,7 +156,7 @@ public async Task Add([FromBody] ApiConfigVM model, EnvString env Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -240,27 +183,12 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E }); } - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; - + _configController.ControllerContext.HttpContext = HttpContext; model.Id = id; - var result = (await ctrl.Edit(new ConfigVM() - { - Id = model.Id, - AppId = model.AppId, - Group = model.Group, - Key = model.Key, - Value = model.Value, - Description = model.Description - }, env)) as JsonResult; - - dynamic obj = result.Value; - if (obj.success == true) + var result = (await _configController.Edit(model.ToConfigVM(), env)) as JsonResult; + + dynamic obj = result?.Value; + if (obj?.success == true) { return Ok(); } @@ -268,7 +196,7 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -284,18 +212,12 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E [HttpDelete("{id}")] public async Task Delete(string id, EnvString env) { - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; - - var result = (await ctrl.Delete(id, env)) as JsonResult; - - dynamic obj = result.Value; - if (obj.success == true) + _configController.ControllerContext.HttpContext = HttpContext; + + var result = (await _configController.Delete(id, env)) as JsonResult; + + dynamic obj = result?.Value; + if (obj?.success == true) { return NoContent(); } @@ -303,7 +225,7 @@ public async Task Delete(string id, EnvString env) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs index 586f92c8..ee21cc94 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs @@ -52,4 +52,25 @@ public class ApiConfigVM : IAppIdModel /// public string Description { get; set; } } + + public static class ApiConfigVMExtension + { + public static ConfigVM ToConfigVM(this ApiConfigVM model) + { + if (model is null) + { + return null; + } + + return new ConfigVM() + { + Id = model.Id, + AppId = model.AppId, + Group = model.Group, + Key = model.Key, + Value = model.Value, + Description = model.Description + }; + } + } } diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs index c312e868..506f4c5a 100644 --- a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -149,4 +149,29 @@ public static ApiServiceInfoVM ToApiServiceInfoVM(this ServiceInfo serviceInfo) } } + public static class ConfigExtension + { + public static ApiConfigVM ToApiConfigVM(this Config config) + { + if (config == null) + { + return null; + } + + var vm = new ApiConfigVM() + { + Id = config.Id, + AppId = config.AppId, + Group = config.Group, + Key = config.Key, + Value = config.Value, + Status = config.Status, + OnlineStatus = config.OnlineStatus, + EditStatus = config.EditStatus + }; + + return vm; + } + } + }