diff --git a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs index 2b89d555..95d8de26 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs @@ -11,6 +11,7 @@ using AgileConfig.Server.Apisite.Utilites; using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Event; +using AgileConfig.Server.Apisite.Models.Mapping; namespace AgileConfig.Server.Apisite.Controllers { @@ -23,8 +24,8 @@ public class AppController : Controller private readonly IUserService _userService; private readonly ITinyEventBus _tinyEventBus; - public AppController(IAppService appService, - IPremissionService premissionService, + public AppController(IAppService appService, + IPremissionService premissionService, IUserService userService, ITinyEventBus tinyEventBus) { @@ -168,18 +169,7 @@ public async Task Search(string name, string id, string group, st private async Task AppToListVM(App item, bool appendInheritancedInfo) { - var vm = new AppListVM - { - Id = item.Id, - Name = item.Name, - Group = item.Group, - Secret = item.Secret, - Inheritanced = item.Type == AppType.Inheritance, - Enabled = item.Enabled, - UpdateTime = item.UpdateTime, - CreateTime = item.CreateTime, - AppAdmin = item.AppAdmin, - }; + var vm = item.ToAppListVM(); if (appendInheritancedInfo) { @@ -219,10 +209,7 @@ private async Task AppendInheritancedInfo(List list) [HttpPost] public async Task Add([FromBody] AppVM model) { - if (model == null) - { - throw new ArgumentNullException("model"); - } + ArgumentNullException.ThrowIfNull(model); var oldApp = await _appService.GetAsync(model.Id); if (oldApp != null) @@ -235,16 +222,8 @@ public async Task Add([FromBody] AppVM model) }); } - var app = new App(); - app.Id = model.Id; - app.Name = model.Name; - app.Secret = model.Secret; - app.Enabled = model.Enabled; + var app = model.ToApp(); app.CreateTime = DateTime.Now; - app.UpdateTime = null; - app.Type = model.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; - app.AppAdmin = model.AppAdmin; - app.Group = model.Group; var inheritanceApps = new List(); if (!model.Inheritanced && model.inheritancedApps != null) @@ -280,10 +259,7 @@ public async Task Add([FromBody] AppVM model) [HttpPost] public async Task Edit([FromBody] AppVM model) { - if (model == null) - { - throw new ArgumentNullException("model"); - } + ArgumentNullException.ThrowIfNull(model); var app = await _appService.GetAsync(model.Id); if (app == null) @@ -304,13 +280,8 @@ public async Task Edit([FromBody] AppVM model) }); } - app.Name = model.Name; - app.Secret = model.Secret; - app.Enabled = model.Enabled; + app = model.ToApp(); app.UpdateTime = DateTime.Now; - app.Type = model.Inheritanced ? AppType.Inheritance : AppType.PRIVATE; - app.AppAdmin = model.AppAdmin; - app.Group = model.Group; var inheritanceApps = new List(); if (!model.Inheritanced && model.inheritancedApps != null) @@ -345,22 +316,12 @@ public async Task All() { var apps = await _appService.GetAllAppsAsync(); var vms = new List(); - foreach (var item in apps) + foreach (var app in apps) { - vms.Add(new AppListVM - { - Id = item.Id, - Name = item.Name, - Secret = item.Secret, - Inheritanced = item.Type == AppType.Inheritance, - Enabled = item.Enabled, - UpdateTime = item.UpdateTime, - CreateTime = item.CreateTime, - inheritancedApps = item.Type == AppType.Inheritance ? - new List() : - (await _appService.GetInheritancedAppsAsync(item.Id)).Select(ia => ia.Id).ToList(), - AppAdmin = item.AppAdmin - }); + var vm = app.ToAppListVM(); + vm.inheritancedAppNames = app.Type == AppType.Inheritance ? new List() : + (await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList(); + vms.Add(vm); } return Json(new @@ -373,22 +334,13 @@ public async Task All() [HttpGet] public async Task Get(string id) { - if (string.IsNullOrEmpty(id)) - { - throw new ArgumentNullException("id"); - } + ArgumentNullException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); + var vm = app.ToAppVM(); - var vm = new AppVM(); - if (app != null) + if (vm != null) { - vm.Id = app.Id; - vm.Name = app.Name; - vm.Secret = app.Secret; - vm.Inheritanced = app.Type == AppType.Inheritance; - vm.Enabled = app.Enabled; - vm.AppAdmin = app.AppAdmin; vm.inheritancedApps = (await _appService.GetInheritancedAppsAsync(id)).Select(x => x.Id).ToList(); } @@ -409,10 +361,7 @@ public async Task Get(string id) [HttpPost] public async Task DisableOrEanble(string id) { - if (string.IsNullOrEmpty(id)) - { - throw new ArgumentNullException("id"); - } + ArgumentNullException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); if (app == null) @@ -444,10 +393,7 @@ public async Task DisableOrEanble(string id) [HttpPost] public async Task Delete(string id) { - if (string.IsNullOrEmpty(id)) - { - throw new ArgumentNullException("id"); - } + ArgumentNullException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); if (app == null) @@ -513,10 +459,7 @@ public async Task InheritancedApps(string currentAppId) [HttpPost] public async Task SaveAppAuth([FromBody] AppAuthVM model) { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } + ArgumentNullException.ThrowIfNull(model); var result = await _appService.SaveUserAppAuth(model.AppId, model.EditConfigPermissionUsers, _premissionService.EditConfigPermissionKey); var result1 = await _appService.SaveUserAppAuth(model.AppId, model.PublishConfigPermissionUsers, _premissionService.PublishConfigPermissionKey); @@ -530,10 +473,7 @@ public async Task SaveAppAuth([FromBody] AppAuthVM model) [HttpGet] public async Task GetUserAppAuth(string appId) { - if (string.IsNullOrEmpty(appId)) - { - throw new ArgumentNullException(nameof(appId)); - } + ArgumentNullException.ThrowIfNullOrEmpty(appId); var result = new AppAuthVM { diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs new file mode 100644 index 00000000..eae7edc3 --- /dev/null +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -0,0 +1,75 @@ +using AgileConfig.Server.Data.Entity; +using System; + +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. + /// + public static class ModelMappingExtension + { + public static AppVM ToAppVM(this App app) + { + if (app == null) + { + return null; + } + + var appVM = new AppVM + { + Id = app.Id, + Name = app.Name, + Group = app.Group, + Secret = app.Secret, + Enabled = app.Enabled, + Inheritanced = app.Type == AppType.Inheritance, + AppAdmin = app.AppAdmin, + CreateTime = app.CreateTime, + }; + + return appVM; + } + + public static AppListVM ToAppListVM(this App app) + { + if (app == null) + { + return null; + } + + var vm = new AppListVM + { + Id = app.Id, + Name = app.Name, + Group = app.Group, + Secret = app.Secret, + Inheritanced = app.Type == AppType.Inheritance, + Enabled = app.Enabled, + UpdateTime = app.UpdateTime, + CreateTime = app.CreateTime, + AppAdmin = app.AppAdmin, + }; + + return vm; + } + + 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; + + return app; + } + } +}