From c1031f426178f096cbf70d940d2b2a365e162051 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Tue, 24 Sep 2024 23:48:17 +0800 Subject: [PATCH 1/2] Refactor api.AppController --- .../Controllers/api/AppController.cs | 80 +++------------- .../Models/Mapping/ModelMappingExtension.cs | 93 ++++++++++++++++++- 2 files changed, 104 insertions(+), 69 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs index fc498ed6..7f4de83f 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs @@ -1,14 +1,13 @@ using AgileConfig.Server.Apisite.Controllers.api.Models; using AgileConfig.Server.Apisite.Filters; using AgileConfig.Server.Apisite.Models; -using AgileConfig.Server.Common.EventBus; -using AgileConfig.Server.Data.Entity; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using AgileConfig.Server.Apisite.Models.Mapping; namespace AgileConfig.Server.Apisite.Controllers.api { @@ -20,31 +19,21 @@ namespace AgileConfig.Server.Apisite.Controllers.api public class AppController : Controller { private readonly IConfigService _configService; - private readonly ITinyEventBus _tinyEventBus; private readonly IAppService _appService; - private readonly IPremissionService _premissionService; - private readonly IUserService _userService; private readonly Controllers.AppController _appController; private readonly Controllers.ConfigController _configController; public AppController(IAppService appService, - IPremissionService premissionService, - IUserService userService, IConfigService configService, - ITinyEventBus tinyEventBus, - - Controllers.AppController _appController, - Controllers.ConfigController _configController) + Controllers.AppController appController, + Controllers.ConfigController configController) { _appService = appService; - _premissionService = premissionService; - _userService = userService; _configService = configService; - _tinyEventBus = tinyEventBus; - this._appController = _appController; - this._configController = _configController; + _appController = appController; + _configController = configController; } /// @@ -55,17 +44,7 @@ public AppController(IAppService appService, public async Task>> GetAll() { var apps = await _appService.GetAllAppsAsync(); - var vms = apps.Select(x => - { - return new ApiAppVM - { - Id = x.Id, - Name = x.Name, - Secret = x.Secret, - Inheritanced = x.Type == AppType.Inheritance, - Enabled = x.Enabled, - }; - }); + var vms = apps.Select(x => x.ToApiAppVM()); return Json(vms); } @@ -84,16 +63,7 @@ public async Task> GetById(string id) if (obj.success) { AppVM appVM = obj.data; - return Json(new ApiAppVM - { - Id = appVM.Id, - Name = appVM.Name, - Secret = appVM.Secret, - Inheritanced = appVM.Inheritanced, - Enabled = appVM.Enabled, - InheritancedApps = appVM.inheritancedApps, - AppAdmin = appVM.AppAdmin - }); + return Json(appVM.ToApiAppVM()); } Response.StatusCode = 400; @@ -126,14 +96,7 @@ public async Task Add([FromBody] ApiAppVM model) _appController.ControllerContext.HttpContext = HttpContext; - var result = (await _appController.Add(new AppVM - { - Id = model.Id, - Name = model.Name, - Secret = model.Secret, - AppAdmin = model.AppAdmin, - Inheritanced = model.Inheritanced - })) as JsonResult; + var result = (await _appController.Add(model.ToAppVM())) as JsonResult; dynamic obj = result.Value; @@ -174,14 +137,7 @@ public async Task Edit(string id, [FromBody] ApiAppVM model) _appController.ControllerContext.HttpContext = HttpContext; model.Id = id; - var result = (await _appController.Edit(new AppVM - { - Id = model.Id, - Name = model.Name, - Secret = model.Secret, - AppAdmin = model.AppAdmin, - Inheritanced = model.Inheritanced - })) as JsonResult; + var result = (await _appController.Edit(model.ToAppVM())) as JsonResult; dynamic obj = result.Value; if (obj.success == true) @@ -262,25 +218,15 @@ public async Task Publish(string appId, EnvString env) /// [TypeFilter(typeof(AdmBasicAuthenticationAttribute))] [HttpGet("Publish_History")] - public async Task>> PublishHistory(string appId, string env) + public async Task>> PublishHistory(string appId, EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(appId); - var history = await _configService.GetPublishTimelineHistoryAsync(appId, env); + var history = await _configService.GetPublishTimelineHistoryAsync(appId, env.Value); history = history.OrderByDescending(x => x.Version).ToList(); - var vms = history.Select(x => new ApiPublishTimelineVM - { - Id = x.Id, - Version = x.Version, - Log = x.Log, - PublishTime = x.PublishTime, - PublishUserId = x.PublishUserId, - Env = x.Env - }); + var vms = history.Select(x => x.ToApiPublishTimelimeVM()); return Json(vms); } diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs index 0a770721..26c8d0ba 100644 --- a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -1,12 +1,14 @@ using AgileConfig.Server.Data.Entity; using System; +using AgileConfig.Server.Apisite.Controllers.api.Models; +using Google.Protobuf.WellKnownTypes; namespace AgileConfig.Server.Apisite.Models.Mapping { /// - /// Do not ask me why not use AutoMapper, I don't know. Just like mannual mapping, it's simple and clear. + /// Do not ask me why not use AutoMapper, I don't know. Just like manual mapping, it's simple and clear. /// - public static class ModelMappingExtension + public static class AppExtension { public static AppVM ToAppVM(this App app) { @@ -53,6 +55,29 @@ public static AppListVM ToAppListVM(this App app) return vm; } + public static ApiAppVM ToApiAppVM(this App vm) + { + if (vm == null) + { + return null; + } + + return new ApiAppVM + { + Id = vm.Id, + Name = vm.Name, + Secret = vm.Secret, + Inheritanced = vm.Type == AppType.Inheritance, + Enabled = vm.Enabled, + AppAdmin = vm.AppAdmin, + Group = vm.Group, + CreateTime = vm.CreateTime + }; + } + } + + public static class AppVMExtension + { public static App ToApp(this AppVM vm) { if (vm == null) @@ -94,5 +119,69 @@ public static App ToApp(this AppVM vm, App app) return app; } + + public static ApiAppVM ToApiAppVM(this AppVM vm) + { + if (vm == null) + { + return null; + } + + return new ApiAppVM + { + Id = vm.Id, + Name = vm.Name, + Secret = vm.Secret, + Inheritanced = vm.Inheritanced, + Enabled = vm.Enabled, + InheritancedApps = vm.inheritancedApps, + AppAdmin = vm.AppAdmin, + Group = vm.Group, + CreateTime = vm.CreateTime + }; + } + } + + public static class PublishTimelineExtension + { + public static ApiPublishTimelineVM ToApiPublishTimelimeVM(this PublishTimeline timeline) + { + if (timeline == null) + { + return null; + } + + return new ApiPublishTimelineVM + { + Id = timeline.Id, + Version = timeline.Version, + AppId = timeline.AppId, + Log = timeline.Log, + PublishTime = timeline.PublishTime, + PublishUserId = timeline.PublishUserId, + Env = timeline.Env + }; + } + } + + public static class ApiAppVMExtension + { + public static AppVM ToAppVM(this ApiAppVM vm) + { + if (vm == null) + { + return null; + } + + return new AppVM + { + Id = vm.Id, + Name = vm.Name, + Secret = vm.Secret, + AppAdmin = vm.AppAdmin, + Inheritanced = vm.Inheritanced, + Group = vm.Group + }; + } } } From d1c2171f440b02fad50f862cffd24ca48399017d Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Tue, 24 Sep 2024 23:57:55 +0800 Subject: [PATCH 2/2] Remove unuse ns --- .../Controllers/api/NodeController.cs | 1 - .../Controllers/api/RegisterCenterController.cs | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs index a0034652..67ff736b 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs @@ -4,7 +4,6 @@ using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Mvc; -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs index 62ec695b..faa424a9 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs @@ -4,10 +4,8 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; -using System.Dynamic; using System.Threading.Tasks; using Agile.Config.Protocol; -using AgileConfig.Server.Common; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using AgileConfig.Server.Common.EventBus;