Skip to content

Commit

Permalink
Merge pull request #193 from dotnetcore/dev
Browse files Browse the repository at this point in the history
Refactor Config API
  • Loading branch information
kklldog authored Sep 28, 2024
2 parents 4032abe + 755229f commit f3f4d41
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 173 deletions.
25 changes: 16 additions & 9 deletions src/AgileConfig.Server.Apisite/Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public async Task<IActionResult> All()
[HttpGet]
public async Task<IActionResult> Get(string id)
{
ArgumentNullException.ThrowIfNullOrEmpty(id);
ArgumentException.ThrowIfNullOrEmpty(id);

var app = await _appService.GetAsync(id);
var vm = app.ToAppVM();
Expand All @@ -342,12 +342,19 @@ public async Task<IActionResult> Get(string id)
{
vm.inheritancedApps = (await _appService.GetInheritancedAppsAsync(id)).Select(x => x.Id).ToList();
}
else
{
return NotFound(new
{
success = false,
message = "未找到对应的应用程序。"
});
}

return Json(new
{
success = app != null,
data = vm,
message = app == null ? "未找到对应的应用程序。" : ""
success = true,
data = vm
});
}

Expand All @@ -360,12 +367,12 @@ public async Task<IActionResult> Get(string id)
[HttpPost]
public async Task<IActionResult> DisableOrEanble(string id)
{
ArgumentNullException.ThrowIfNullOrEmpty(id);
ArgumentException.ThrowIfNullOrEmpty(id);

var app = await _appService.GetAsync(id);
if (app == null)
{
return Json(new
return NotFound(new
{
success = false,
message = "未找到对应的应用程序。"
Expand All @@ -392,12 +399,12 @@ public async Task<IActionResult> DisableOrEanble(string id)
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
ArgumentNullException.ThrowIfNullOrEmpty(id);
ArgumentException.ThrowIfNullOrEmpty(id);

var app = await _appService.GetAsync(id);
if (app == null)
{
return Json(new
return NotFound(new
{
success = false,
message = "未找到对应的应用程序。"
Expand Down Expand Up @@ -472,7 +479,7 @@ public async Task<IActionResult> SaveAppAuth([FromBody] AppAuthVM model)
[HttpGet]
public async Task<IActionResult> GetUserAppAuth(string appId)
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);
ArgumentException.ThrowIfNullOrEmpty(appId);

var result = new AppAuthVM
{
Expand Down
70 changes: 41 additions & 29 deletions src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Models.Mapping;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace AgileConfig.Server.Apisite.Controllers.api
{
Expand Down Expand Up @@ -57,19 +58,22 @@ public async Task<ActionResult<IEnumerable<ApiAppVM>>> GetAll()
[HttpGet("{id}")]
public async Task<ActionResult<ApiAppVM>> GetById(string id)
{
var result = (await _appController.Get(id)) as JsonResult;
dynamic obj = result.Value;
var actionResult = await _appController.Get(id);
var status = actionResult as IStatusCodeActionResult;

if (obj.success)
var result = actionResult as JsonResult;
dynamic obj = result?.Value;

if (obj?.success ?? false)
{
AppVM appVM = obj.data;
return Json(appVM.ToApiAppVM());
}

Response.StatusCode = 400;
Response.StatusCode = status.StatusCode.Value;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down Expand Up @@ -98,17 +102,17 @@ public async Task<IActionResult> Add([FromBody] ApiAppVM model)

var result = (await _appController.Add(model.ToAppVM())) as JsonResult;

dynamic obj = result.Value;
dynamic obj = result?.Value;

if (obj.success == true)
if (obj?.success == true)
{
return Created("/api/app/" + obj.data.Id, "");
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down Expand Up @@ -137,18 +141,20 @@ public async Task<IActionResult> Edit(string id, [FromBody] ApiAppVM model)
_appController.ControllerContext.HttpContext = HttpContext;

model.Id = id;
var result = (await _appController.Edit(model.ToAppVM())) as JsonResult;
var actionResult = await _appController.Edit(model.ToAppVM());
var status = actionResult as IStatusCodeActionResult;
var result = actionResult as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success ?? false)
{
return Ok();
}

Response.StatusCode = 400;
Response.StatusCode = status.StatusCode.Value;
return Json(new
{
obj.message
obj?.message
});
}

Expand All @@ -164,18 +170,20 @@ public async Task<IActionResult> Delete(string id)
{
_appController.ControllerContext.HttpContext = HttpContext;

var result = (await _appController.Delete(id)) as JsonResult;
var actionResult = await _appController.Delete(id);
var status = actionResult as IStatusCodeActionResult;
var result = actionResult as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success ?? false)
{
return NoContent();
}

Response.StatusCode = 400;
Response.StatusCode = status.StatusCode.Value;
return Json(new
{
obj.message
obj?.message
});
}

Expand All @@ -192,21 +200,23 @@ public async Task<IActionResult> Publish(string appId, EnvString env)
{
_configController.ControllerContext.HttpContext = HttpContext;

var result = (await _configController.Publish(new PublishLogVM()
var actionResult = await _configController.Publish(new PublishLogVM()
{
AppId = appId
}, env)) as JsonResult;
}, env);
var status = actionResult as IStatusCodeActionResult;
var result = actionResult as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success ?? false)
{
return Ok();
}

Response.StatusCode = 400;
Response.StatusCode = status.StatusCode.Value;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down Expand Up @@ -244,18 +254,20 @@ public async Task<IActionResult> Rollback(string historyId, EnvString env)
{
_configController.ControllerContext.HttpContext = HttpContext;

var result = (await _configController.Rollback(historyId, env)) as JsonResult;
var actionResult = await _configController.Rollback(historyId, env);
var status = actionResult as IStatusCodeActionResult;
var result = actionResult as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success ?? false)
{
return Ok();
}

Response.StatusCode = 400;
Response.StatusCode = status.StatusCode.Value;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down
Loading

0 comments on commit f3f4d41

Please sign in to comment.