Skip to content

Commit

Permalink
Refactor IfEnvEmptySetDefault to a static method
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Sep 8, 2024
1 parent 9287c2c commit 7eee7d9
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 54 deletions.
36 changes: 18 additions & 18 deletions src/AgileConfig.Server.Apisite/Controllers/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<IActionResult> 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)
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<IActionResult> AddRange([FromBody] List<ConfigVM> model, [From
throw new ArgumentNullException("model");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var configs = await _configService.GetByAppIdAsync(model.First().AppId, env);

Expand Down Expand Up @@ -177,7 +177,7 @@ public async Task<IActionResult> 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)
Expand Down Expand Up @@ -272,7 +272,7 @@ private bool IsOnlyUpdateDescription(Config newConfig, Config oldConfig)
[HttpGet]
public async Task<IActionResult> All(string env)
{
_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var configs = await _configService.GetAllConfigsAsync(env);

Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task<IActionResult> 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();
Expand Down Expand Up @@ -361,7 +361,7 @@ public async Task<IActionResult> Get(string id, string env)
throw new ArgumentNullException("id");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var config = await _configService.GetAsync(id, env);

Expand All @@ -383,7 +383,7 @@ public async Task<IActionResult> 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)
Expand Down Expand Up @@ -428,7 +428,7 @@ public async Task<IActionResult> DeleteSome([FromBody] List<string> ids, string
throw new ArgumentNullException("ids");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

List<Config> deleteConfigs = new List<Config>();

Expand Down Expand Up @@ -481,7 +481,7 @@ public async Task<IActionResult> Rollback(string publishTimelineId, string env)
throw new ArgumentNullException("publishTimelineId");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var result = await _configService.RollbackAsync(publishTimelineId, env);

Expand All @@ -506,7 +506,7 @@ public async Task<IActionResult> 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<object>();
Expand Down Expand Up @@ -549,7 +549,7 @@ public async Task<IActionResult> 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);
Expand Down Expand Up @@ -631,7 +631,7 @@ public async Task<IActionResult> ExportJson(string appId, string env)
throw new ArgumentNullException("appId");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var configs = await _configService.GetByAppIdAsync(appId, env);

Expand Down Expand Up @@ -659,7 +659,7 @@ public async Task<IActionResult> 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)
Expand Down Expand Up @@ -693,7 +693,7 @@ public async Task<IActionResult> PublishHistory(string appId, string env)
throw new ArgumentNullException("appId");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var history = await _configService.GetPublishDetailListAsync(appId, env);

Expand Down Expand Up @@ -724,7 +724,7 @@ public async Task<IActionResult> 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<string>() { configId }, env);

Expand All @@ -747,7 +747,7 @@ public async Task<IActionResult> CancelSomeEdit([FromBody] List<string> ids, str
throw new ArgumentNullException("ids");
}

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var result = await _configService.CancelEdit(ids, env);

Expand Down Expand Up @@ -809,7 +809,7 @@ public async Task<IActionResult> 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 格式展示的时候不需要删除的配置
Expand Down Expand Up @@ -840,7 +840,7 @@ public async Task<IActionResult> 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 格式展示的时候不需要删除的配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task<IActionResult> Delete(string id)
[HttpPost("publish")]
public async Task<IActionResult> Publish(string appId, string env)
{
_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var ctrl = new Controllers.ConfigController(
_configService,
Expand Down Expand Up @@ -289,7 +289,7 @@ public async Task<ActionResult<IEnumerable<ApiPublishTimelineVM>>> PublishHistor
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var history = await _configService.GetPublishTimelineHistoryAsync(appId, env);

Expand Down Expand Up @@ -319,7 +319,7 @@ public async Task<ActionResult<IEnumerable<ApiPublishTimelineVM>>> PublishHistor
[HttpPost("rollback")]
public async Task<IActionResult> Rollback(string historyId, string env)
{
_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var ctrl = new Controllers.ConfigController(
_configService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [F
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var app = await _appService.GetAsync(appId);
if (!app.Enabled)
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetConfigs(string appId, stri
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

_configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);

var configs = await _configService.GetByAppIdAsync(appId, env);

Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task<ActionResult<ApiConfigVM>> 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)
Expand Down Expand Up @@ -193,7 +193,7 @@ public async Task<IActionResult> 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()
{
Expand Down Expand Up @@ -250,7 +250,7 @@ public async Task<IActionResult> 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()
Expand Down Expand Up @@ -296,7 +296,7 @@ public async Task<IActionResult> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static string GetEnvFromArgs(IDictionary<string, object> args, IConfigSe
var env = "";
if (envArg == null)
{
configService.IfEnvEmptySetDefault(ref env);
ISettingService.IfEnvEmptySetDefault(ref env);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Expand Down
7 changes: 0 additions & 7 deletions src/AgileConfig.Server.IService/IConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ namespace AgileConfig.Server.IService
{
public interface IConfigService: IDisposable
{
/// <summary>
/// 如果环境为空,返回默认环境,默认环境为数据库设置的环境列表的第一个
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
string IfEnvEmptySetDefault(ref string env);

/// <summary>
/// 发布当前待发布的配置项,如果传ids那么就是只发布这些项目,如果不传ids,那么就是全部发布
/// </summary>
Expand Down
19 changes: 18 additions & 1 deletion src/AgileConfig.Server.IService/ISettingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Setting> GetAsync(string id);
Task<bool> AddAsync(Setting setting);

Expand Down
16 changes: 0 additions & 16 deletions src/AgileConfig.Server.Service/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> AddAsync(Config config, string env)
{
if (config.Value == null)
Expand Down
51 changes: 51 additions & 0 deletions test/AgileConfig.Server.ServiceTests/ISettingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 7eee7d9

Please sign in to comment.