diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs index 9f5dcd48..9ddb5c61 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs @@ -1,10 +1,7 @@ using AgileConfig.Server.Apisite.Models; using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using AgileConfig.Server.Data.Entity; -using NuGet.Common; namespace AgileConfig.Server.Apisite.Controllers.api.Models { @@ -46,4 +43,25 @@ public class ApiAppVM : IAppModel public DateTime CreateTime { get; set; } } + + 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 + }; + } + } } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiNodeVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiNodeVM.cs index dca335c0..495d5b57 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiNodeVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiNodeVM.cs @@ -1,8 +1,6 @@ using AgileConfig.Server.Data.Entity; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using AgileConfig.Server.Apisite.Models; namespace AgileConfig.Server.Apisite.Controllers.api.Models { @@ -25,4 +23,25 @@ public class ApiNodeVM /// public DateTime? LastEchoTime { get; set; } } + + public static class ApiNodeVMExtension + { + public static ServerNodeVM ToServerNodeVM(this ApiNodeVM node) + { + if (node == null) + { + return null; + } + + var vm = new ServerNodeVM + { + Address = node.Address, + Remark = node.Remark, + LastEchoTime = node.LastEchoTime, + Status = node.Status + }; + + return vm; + } + } } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs index 67ff736b..ef81e202 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using AgileConfig.Server.Apisite.Models.Mapping; namespace AgileConfig.Server.Apisite.Controllers.api { @@ -43,13 +44,7 @@ public async Task>> GetAll() { var nodes = await _serverNodeService.GetAllNodesAsync(); - var vms = nodes.Select(x => new ApiNodeVM - { - Address = x.Id, - Remark = x.Remark, - LastEchoTime = x.LastEchoTime, - Status = x.Status - }); + var vms = nodes.Select(x => x.ToApiNodeVM()); return Json(vms); } @@ -77,16 +72,10 @@ public async Task Add([FromBody] ApiNodeVM model) var ctrl = new ServerNodeController(_serverNodeService, _sysLogService, _remoteServerNodeProxy, _tinyEventBus); ctrl.ControllerContext.HttpContext = HttpContext; - var result = (await ctrl.Add(new ServerNodeVM - { - Address = model.Address, - Remark = model.Remark, - LastEchoTime = model.LastEchoTime, - Status = model.Status - })) as JsonResult; + var result = (await ctrl.Add(model.ToServerNodeVM())) as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success == true) { return Created("", ""); } @@ -94,7 +83,7 @@ public async Task Add([FromBody] ApiNodeVM model) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -112,8 +101,8 @@ public async Task Delete([FromQuery] string address) ctrl.ControllerContext.HttpContext = HttpContext; var result = (await ctrl.Delete(new ServerNodeVM { Address = address })) as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success == true) { return NoContent(); } @@ -121,7 +110,7 @@ public async Task Delete([FromQuery] string address) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } diff --git a/src/AgileConfig.Server.Apisite/Models/AppVM.cs b/src/AgileConfig.Server.Apisite/Models/AppVM.cs index 5c0ac8b8..e721c18b 100644 --- a/src/AgileConfig.Server.Apisite/Models/AppVM.cs +++ b/src/AgileConfig.Server.Apisite/Models/AppVM.cs @@ -1,4 +1,5 @@ -using AgileConfig.Server.Data.Entity; +using AgileConfig.Server.Apisite.Controllers.api.Models; +using AgileConfig.Server.Data.Entity; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -46,4 +47,70 @@ public class AppListVM : AppVM public List children { get; set; } } + + public static class AppVMExtension + { + public static App ToApp(this AppVM vm) + { + if (vm == null) + { + return null; + } + + var app = new App(); + app.Id = vm.Id; + app.Name = vm.Name; + app.Secret = vm.Secret; + app.Enabled = vm.Enabled; + app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; + app.AppAdmin = vm.AppAdmin; + app.Group = vm.Group; + app.CreateTime = vm.CreateTime; + + return app; + } + + public static App ToApp(this AppVM vm, App app) + { + if (vm == null) + { + return null; + } + + app.Id = vm.Id; + app.Name = vm.Name; + app.Secret = vm.Secret; + app.Enabled = vm.Enabled; + app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; + app.AppAdmin = vm.AppAdmin; + app.Group = vm.Group; + if (vm.CreateTime > DateTime.MinValue) + { + app.CreateTime = vm.CreateTime; + } + + 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 + }; + } + } } \ No newline at end of file diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs index 26c8d0ba..3018ae9b 100644 --- a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -1,7 +1,5 @@ using AgileConfig.Server.Data.Entity; -using System; using AgileConfig.Server.Apisite.Controllers.api.Models; -using Google.Protobuf.WellKnownTypes; namespace AgileConfig.Server.Apisite.Models.Mapping { @@ -76,72 +74,6 @@ public static ApiAppVM ToApiAppVM(this App vm) } } - public static class AppVMExtension - { - public static App ToApp(this AppVM vm) - { - if (vm == null) - { - return null; - } - - var app = new App(); - app.Id = vm.Id; - app.Name = vm.Name; - app.Secret = vm.Secret; - app.Enabled = vm.Enabled; - app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; - app.AppAdmin = vm.AppAdmin; - app.Group = vm.Group; - app.CreateTime = vm.CreateTime; - - return app; - } - - public static App ToApp(this AppVM vm, App app) - { - if (vm == null) - { - return null; - } - - app.Id = vm.Id; - app.Name = vm.Name; - app.Secret = vm.Secret; - app.Enabled = vm.Enabled; - app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; - app.AppAdmin = vm.AppAdmin; - app.Group = vm.Group; - if (vm.CreateTime > DateTime.MinValue) - { - app.CreateTime = vm.CreateTime; - } - - 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) @@ -164,24 +96,23 @@ public static ApiPublishTimelineVM ToApiPublishTimelimeVM(this PublishTimeline t } } - public static class ApiAppVMExtension + public static class ServerNodeExtension { - public static AppVM ToAppVM(this ApiAppVM vm) + public static ApiNodeVM ToApiNodeVM(this ServerNode node) { - if (vm == null) + if (node == null) { return null; } - return new AppVM + return new ApiNodeVM { - Id = vm.Id, - Name = vm.Name, - Secret = vm.Secret, - AppAdmin = vm.AppAdmin, - Inheritanced = vm.Inheritanced, - Group = vm.Group + Address = node.Id, + Remark = node.Remark, + LastEchoTime = node.LastEchoTime, + Status = node.Status }; } } + } diff --git a/src/AgileConfig.Server.Apisite/Models/ServerNodeVM.cs b/src/AgileConfig.Server.Apisite/Models/ServerNodeVM.cs index ec859612..5fe98894 100644 --- a/src/AgileConfig.Server.Apisite/Models/ServerNodeVM.cs +++ b/src/AgileConfig.Server.Apisite/Models/ServerNodeVM.cs @@ -17,4 +17,5 @@ public class ServerNodeVM public NodeStatus Status { get; set; } public DateTime? LastEchoTime { get; set; } } + }