Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor app search #194

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 44 additions & 127 deletions src/AgileConfig.Server.Apisite/Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,156 +35,64 @@ public AppController(IAppService appService,
_premissionService = premissionService;
}

public async Task<IActionResult> Search(string name, string id, string group, string sortField, string ascOrDesc, bool tableGrouped, int current = 1, int pageSize = 20)
public async Task<IActionResult> Search(string name, string id, string group, string sortField,
string ascOrDesc, bool tableGrouped, int current = 1, int pageSize = 20)
{
if (current < 1)
{
throw new ArgumentException("current cant less then 1 .");
}

if (pageSize < 1)
{
throw new ArgumentException("pageSize cant less then 1 .");
}

var query = await _appService.GetAllAppsAsync();
if (!string.IsNullOrWhiteSpace(name))
{
query = query.Where(x => x.Name.Contains(name)).ToList();
}
if (!string.IsNullOrWhiteSpace(id))
{
query = query.Where(x => x.Id.Contains(id)).ToList();
}
if (!string.IsNullOrWhiteSpace(group))
{
query = query.Where(x => x.Group == group).ToList();
}

var appvms = new List<AppListVM>();
foreach (var app in query)
var appListVms = new List<AppListVM>();
long count = 0;
if (!tableGrouped)
{
appvms.Add(await AppToListVM(app, false));
}
if (tableGrouped)
{
var appGroups = appvms.GroupBy(x => x.Group);
var appGroupList = new List<AppListVM>();
foreach (var appGroup in appGroups)
var searchResult =
await _appService.SearchAsync(id, name, group, sortField, ascOrDesc, current, pageSize);
foreach (var app in searchResult.Apps)
{
var first = appGroup.First();
var children = new List<AppListVM>();
if (appGroup.Count() > 1)
{
foreach (var item in appGroup)
{
if (first.Id != item.Id)
{
children.Add(item);
}
}
}

if (children.Count > 0)
{
first.children = children;
}
appGroupList.Add(first);
appListVms.Add(app.ToAppListVM());
}

appvms = appGroupList;
}

if (tableGrouped)
{
if (sortField == "group" && ascOrDesc.StartsWith("desc"))
{
appvms = appvms.OrderByDescending(x => x.Group).ToList();
}
else
{
appvms = appvms.OrderBy(x => x.Group).ToList();
}
count = searchResult.Count;
}
else
{
if (sortField == "createTime")
{
if (ascOrDesc.StartsWith("asc"))
{
appvms = appvms.OrderBy(x => x.CreateTime).ToList();
}
else
{
appvms = appvms.OrderByDescending(x => x.CreateTime).ToList();
}
}
if (sortField == "id")
{
if (ascOrDesc.StartsWith("asc"))
{
appvms = appvms.OrderBy(x => x.Id).ToList();
}
else
{
appvms = appvms.OrderByDescending(x => x.Id).ToList();
}
}
if (sortField == "name")
var searchResult =
await _appService.SearchGroupedAsync(id, name, group, sortField, ascOrDesc, current, pageSize);
foreach (var groupedApp in searchResult.GroupedApps)
{
if (ascOrDesc.StartsWith("asc"))
var app = groupedApp.App;
var vm = app.ToAppListVM();
vm.children = new List<AppListVM>();
foreach (var child in groupedApp.Children ?? [])
{
appvms = appvms.OrderBy(x => x.Name).ToList();
}
else
{
appvms = appvms.OrderByDescending(x => x.Name).ToList();
}
}
if (sortField == "group")
{
if (ascOrDesc.StartsWith("asc"))
{
appvms = appvms.OrderBy(x => x.Group).ToList();
}
else
{
appvms = appvms.OrderByDescending(x => x.Group).ToList();
vm.children.Add(child.App.ToAppListVM());
}

appListVms.Add(vm);
}

count = searchResult.Count;
}

var count = appvms.Count;
var pageList = appvms.ToList().Skip((current - 1) * pageSize).Take(pageSize).ToList();
await AppendInheritancedInfo(pageList);
await AppendInheritancedInfo(appListVms);

return Json(new
{
current,
pageSize,
success = true,
total = count,
data = pageList
data = appListVms
});
}

private async Task<AppListVM> AppToListVM(App item, bool appendInheritancedInfo)
{
var vm = item.ToAppListVM();

if (appendInheritancedInfo)
{
var inheritancedApps = await _appService.GetInheritancedAppsAsync(item.Id);
vm.inheritancedApps = item.Type == AppType.Inheritance
? new List<string>()
: (inheritancedApps).Select(ia => ia.Id).ToList();
vm.inheritancedAppNames = item.Type == AppType.Inheritance
? new List<string>()
: (inheritancedApps).Select(ia => ia.Name).ToList();
vm.AppAdminName = (await _userService.GetUserAsync(item.AppAdmin))?.UserName;
}

return vm;
}

private async Task AppendInheritancedInfo(List<AppListVM> list)
{
foreach (var appListVm in list)
Expand Down Expand Up @@ -213,7 +121,6 @@ public async Task<IActionResult> Add([FromBody] AppVM model)
var oldApp = await _appService.GetAsync(model.Id);
if (oldApp != null)
{

return Json(new
{
success = false,
Expand Down Expand Up @@ -303,6 +210,7 @@ public async Task<IActionResult> Edit([FromBody] AppVM model)
{
_tinyEventBus.Fire(new EditAppSuccessful(app, this.GetCurrentUserName()));
}

return Json(new
{
success = result,
Expand All @@ -318,8 +226,9 @@ public async Task<IActionResult> All()
foreach (var app in apps)
{
var vm = app.ToAppListVM();
vm.inheritancedAppNames = app.Type == AppType.Inheritance ? new List<string>() :
(await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList();
vm.inheritancedAppNames = app.Type == AppType.Inheritance
? new List<string>()
: (await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList();
vms.Add(vm);
}

Expand Down Expand Up @@ -363,7 +272,8 @@ public async Task<IActionResult> Get(string id)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[TypeFilter(typeof(PremissionCheckAttribute), Arguments = new object[] { "App.DisableOrEanble", Functions.App_Edit })]
[TypeFilter(typeof(PremissionCheckAttribute),
Arguments = new object[] { "App.DisableOrEanble", Functions.App_Edit })]
[HttpPost]
public async Task<IActionResult> DisableOrEanble(string id)
{
Expand Down Expand Up @@ -440,6 +350,7 @@ public async Task<IActionResult> InheritancedApps(string currentAppId)
//过滤本身
apps.Remove(self);
}

var vms = apps.Select(x =>
{
return new
Expand Down Expand Up @@ -467,8 +378,10 @@ public async Task<IActionResult> SaveAppAuth([FromBody] AppAuthVM 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);
var result = await _appService.SaveUserAppAuth(model.AppId, model.EditConfigPermissionUsers,
_premissionService.EditConfigPermissionKey);
var result1 = await _appService.SaveUserAppAuth(model.AppId, model.PublishConfigPermissionUsers,
_premissionService.PublishConfigPermissionKey);

return Json(new
{
Expand All @@ -485,8 +398,12 @@ public async Task<IActionResult> GetUserAppAuth(string appId)
{
AppId = appId
};
result.EditConfigPermissionUsers = (await _appService.GetUserAppAuth(appId, _premissionService.EditConfigPermissionKey)).Select(x => x.Id).ToList();
result.PublishConfigPermissionUsers = (await _appService.GetUserAppAuth(appId, _premissionService.PublishConfigPermissionKey)).Select(x => x.Id).ToList();
result.EditConfigPermissionUsers =
(await _appService.GetUserAppAuth(appId, _premissionService.EditConfigPermissionKey)).Select(x => x.Id)
.ToList();
result.PublishConfigPermissionUsers =
(await _appService.GetUserAppAuth(appId, _premissionService.PublishConfigPermissionKey))
.Select(x => x.Id).ToList();

return Json(new
{
Expand All @@ -506,4 +423,4 @@ public async Task<IActionResult> GetAppGroups()
});
}
}
}
}
11 changes: 11 additions & 0 deletions src/AgileConfig.Server.IService/IAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

namespace AgileConfig.Server.IService
{
public class GroupedApp
{
public App App { get; set; }
public List<GroupedApp> Children { get; set; }
}
public interface IAppService : IDisposable
{
Task<App> GetAsync(string id);
Expand All @@ -20,6 +25,12 @@ public interface IAppService : IDisposable

Task<List<App>> GetAllAppsAsync();

Task<(List<App> Apps, long Count)> SearchAsync(string id, string name, string group,string sortField, string ascOrDesc,
int current, int pageSize);

Task<(List<GroupedApp> GroupedApps, long Count)> SearchGroupedAsync(string id, string name, string group,string sortField, string ascOrDesc,
int current, int pageSize);

Task<List<App>> GetAllInheritancedAppsAsync();

Task<int> CountEnabledAppsAsync();
Expand Down
Loading
Loading