From 755229f2476ad03e4184e744731ab22f27a55799 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sun, 29 Sep 2024 01:15:03 +0800 Subject: [PATCH] Fix issue --- .../Controllers/AppController.cs | 25 ++++--- .../Controllers/api/AppController.cs | 70 +++++++++++-------- .../Controllers/api/Models/ApiAppVM.cs | 3 +- .../AppBasicAuthenticationAttribute.cs | 7 +- src/AgileConfig.Server.Apisite/Startup.cs | 2 +- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs index a4a1a881..3c297a34 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs @@ -333,7 +333,7 @@ public async Task All() [HttpGet] public async Task Get(string id) { - ArgumentNullException.ThrowIfNullOrEmpty(id); + ArgumentException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); var vm = app.ToAppVM(); @@ -342,12 +342,19 @@ public async Task 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 }); } @@ -360,12 +367,12 @@ public async Task Get(string id) [HttpPost] public async Task 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 = "未找到对应的应用程序。" @@ -392,12 +399,12 @@ public async Task DisableOrEanble(string id) [HttpPost] public async Task 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 = "未找到对应的应用程序。" @@ -472,7 +479,7 @@ public async Task SaveAppAuth([FromBody] AppAuthVM model) [HttpGet] public async Task GetUserAppAuth(string appId) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); + ArgumentException.ThrowIfNullOrEmpty(appId); var result = new AppAuthVM { diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs index 7f4de83f..f86023a0 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs @@ -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 { @@ -57,19 +58,22 @@ public async Task>> GetAll() [HttpGet("{id}")] public async Task> 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 }); } @@ -98,9 +102,9 @@ public async Task 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, ""); } @@ -108,7 +112,7 @@ public async Task Add([FromBody] ApiAppVM model) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -137,18 +141,20 @@ public async Task 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 }); } @@ -164,18 +170,20 @@ public async Task 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 }); } @@ -192,21 +200,23 @@ public async Task 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 }); } @@ -244,18 +254,20 @@ public async Task 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 }); } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs index 9ddb5c61..0226bf01 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs @@ -60,7 +60,8 @@ public static AppVM ToAppVM(this ApiAppVM vm) Secret = vm.Secret, AppAdmin = vm.AppAdmin, Inheritanced = vm.Inheritanced, - Group = vm.Group + Group = vm.Group, + Enabled = vm.Enabled.GetValueOrDefault() }; } } diff --git a/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs b/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs index e1e8e8e6..9639d0d7 100644 --- a/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs +++ b/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; diff --git a/src/AgileConfig.Server.Apisite/Startup.cs b/src/AgileConfig.Server.Apisite/Startup.cs index 72691d0d..1b504f05 100644 --- a/src/AgileConfig.Server.Apisite/Startup.cs +++ b/src/AgileConfig.Server.Apisite/Startup.cs @@ -67,7 +67,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMemoryCache(); services.AddCors(); - services.AddMvc().AddRazorRuntimeCompilation(); + services.AddMvc().AddRazorRuntimeCompilation().AddControllersAsServices(); if (Appsettings.IsPreviewMode) {