Skip to content

Commit

Permalink
Refactor AppController
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Sep 22, 2024
1 parent e308d8d commit 8549841
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 80 deletions.
100 changes: 20 additions & 80 deletions src/AgileConfig.Server.Apisite/Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -168,18 +169,7 @@ public async Task<IActionResult> Search(string name, string id, string group, st
private async Task<AppListVM> 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)
{
Expand Down Expand Up @@ -219,10 +209,7 @@ private async Task AppendInheritancedInfo(List<AppListVM> list)
[HttpPost]
public async Task<IActionResult> Add([FromBody] AppVM model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
ArgumentNullException.ThrowIfNull(model);

var oldApp = await _appService.GetAsync(model.Id);
if (oldApp != null)
Expand All @@ -235,16 +222,8 @@ public async Task<IActionResult> 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<AppInheritanced>();
if (!model.Inheritanced && model.inheritancedApps != null)
Expand Down Expand Up @@ -280,10 +259,7 @@ public async Task<IActionResult> Add([FromBody] AppVM model)
[HttpPost]
public async Task<IActionResult> Edit([FromBody] AppVM model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
ArgumentNullException.ThrowIfNull(model);

var app = await _appService.GetAsync(model.Id);
if (app == null)
Expand All @@ -304,13 +280,8 @@ public async Task<IActionResult> 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<AppInheritanced>();
if (!model.Inheritanced && model.inheritancedApps != null)
Expand Down Expand Up @@ -345,22 +316,12 @@ public async Task<IActionResult> All()
{
var apps = await _appService.GetAllAppsAsync();
var vms = new List<AppListVM>();
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<string>() :
(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<string>() :
(await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList();
vms.Add(vm);
}

return Json(new
Expand All @@ -373,22 +334,13 @@ public async Task<IActionResult> All()
[HttpGet]
public async Task<IActionResult> 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();
}

Expand All @@ -409,10 +361,7 @@ public async Task<IActionResult> Get(string id)
[HttpPost]
public async Task<IActionResult> DisableOrEanble(string id)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException("id");
}
ArgumentNullException.ThrowIfNullOrEmpty(id);

var app = await _appService.GetAsync(id);
if (app == null)
Expand Down Expand Up @@ -444,10 +393,7 @@ public async Task<IActionResult> DisableOrEanble(string id)
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException("id");
}
ArgumentNullException.ThrowIfNullOrEmpty(id);

var app = await _appService.GetAsync(id);
if (app == null)
Expand Down Expand Up @@ -513,10 +459,7 @@ public async Task<IActionResult> InheritancedApps(string currentAppId)
[HttpPost]
public async Task<IActionResult> 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);
Expand All @@ -530,10 +473,7 @@ public async Task<IActionResult> SaveAppAuth([FromBody] AppAuthVM model)
[HttpGet]
public async Task<IActionResult> GetUserAppAuth(string appId)
{
if (string.IsNullOrEmpty(appId))
{
throw new ArgumentNullException(nameof(appId));
}
ArgumentNullException.ThrowIfNullOrEmpty(appId);

var result = new AppAuthVM
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using AgileConfig.Server.Data.Entity;
using System;

namespace AgileConfig.Server.Apisite.Models.Mapping
{
/// <summary>
/// Do not ask me why not use AutoMapper, I don't know. Just like mannual mapping, it's simple and clear.
/// </summary>
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;
}
}
}

0 comments on commit 8549841

Please sign in to comment.