Skip to content

Commit 6524422

Browse files
authored
Merge pull request #194 from dotnetcore/dev
Refactor app search
2 parents f3f4d41 + f761461 commit 6524422

File tree

3 files changed

+176
-134
lines changed

3 files changed

+176
-134
lines changed

src/AgileConfig.Server.Apisite/Controllers/AppController.cs

Lines changed: 44 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -35,156 +35,64 @@ public AppController(IAppService appService,
3535
_premissionService = premissionService;
3636
}
3737

38-
public async Task<IActionResult> Search(string name, string id, string group, string sortField, string ascOrDesc, bool tableGrouped, int current = 1, int pageSize = 20)
38+
public async Task<IActionResult> Search(string name, string id, string group, string sortField,
39+
string ascOrDesc, bool tableGrouped, int current = 1, int pageSize = 20)
3940
{
4041
if (current < 1)
4142
{
4243
throw new ArgumentException("current cant less then 1 .");
4344
}
45+
4446
if (pageSize < 1)
4547
{
4648
throw new ArgumentException("pageSize cant less then 1 .");
4749
}
4850

49-
var query = await _appService.GetAllAppsAsync();
50-
if (!string.IsNullOrWhiteSpace(name))
51-
{
52-
query = query.Where(x => x.Name.Contains(name)).ToList();
53-
}
54-
if (!string.IsNullOrWhiteSpace(id))
55-
{
56-
query = query.Where(x => x.Id.Contains(id)).ToList();
57-
}
58-
if (!string.IsNullOrWhiteSpace(group))
59-
{
60-
query = query.Where(x => x.Group == group).ToList();
61-
}
62-
63-
var appvms = new List<AppListVM>();
64-
foreach (var app in query)
51+
var appListVms = new List<AppListVM>();
52+
long count = 0;
53+
if (!tableGrouped)
6554
{
66-
appvms.Add(await AppToListVM(app, false));
67-
}
68-
if (tableGrouped)
69-
{
70-
var appGroups = appvms.GroupBy(x => x.Group);
71-
var appGroupList = new List<AppListVM>();
72-
foreach (var appGroup in appGroups)
55+
var searchResult =
56+
await _appService.SearchAsync(id, name, group, sortField, ascOrDesc, current, pageSize);
57+
foreach (var app in searchResult.Apps)
7358
{
74-
var first = appGroup.First();
75-
var children = new List<AppListVM>();
76-
if (appGroup.Count() > 1)
77-
{
78-
foreach (var item in appGroup)
79-
{
80-
if (first.Id != item.Id)
81-
{
82-
children.Add(item);
83-
}
84-
}
85-
}
86-
87-
if (children.Count > 0)
88-
{
89-
first.children = children;
90-
}
91-
appGroupList.Add(first);
59+
appListVms.Add(app.ToAppListVM());
9260
}
9361

94-
appvms = appGroupList;
95-
}
96-
97-
if (tableGrouped)
98-
{
99-
if (sortField == "group" && ascOrDesc.StartsWith("desc"))
100-
{
101-
appvms = appvms.OrderByDescending(x => x.Group).ToList();
102-
}
103-
else
104-
{
105-
appvms = appvms.OrderBy(x => x.Group).ToList();
106-
}
62+
count = searchResult.Count;
10763
}
10864
else
10965
{
110-
if (sortField == "createTime")
111-
{
112-
if (ascOrDesc.StartsWith("asc"))
113-
{
114-
appvms = appvms.OrderBy(x => x.CreateTime).ToList();
115-
}
116-
else
117-
{
118-
appvms = appvms.OrderByDescending(x => x.CreateTime).ToList();
119-
}
120-
}
121-
if (sortField == "id")
122-
{
123-
if (ascOrDesc.StartsWith("asc"))
124-
{
125-
appvms = appvms.OrderBy(x => x.Id).ToList();
126-
}
127-
else
128-
{
129-
appvms = appvms.OrderByDescending(x => x.Id).ToList();
130-
}
131-
}
132-
if (sortField == "name")
66+
var searchResult =
67+
await _appService.SearchGroupedAsync(id, name, group, sortField, ascOrDesc, current, pageSize);
68+
foreach (var groupedApp in searchResult.GroupedApps)
13369
{
134-
if (ascOrDesc.StartsWith("asc"))
70+
var app = groupedApp.App;
71+
var vm = app.ToAppListVM();
72+
vm.children = new List<AppListVM>();
73+
foreach (var child in groupedApp.Children ?? [])
13574
{
136-
appvms = appvms.OrderBy(x => x.Name).ToList();
137-
}
138-
else
139-
{
140-
appvms = appvms.OrderByDescending(x => x.Name).ToList();
141-
}
142-
}
143-
if (sortField == "group")
144-
{
145-
if (ascOrDesc.StartsWith("asc"))
146-
{
147-
appvms = appvms.OrderBy(x => x.Group).ToList();
148-
}
149-
else
150-
{
151-
appvms = appvms.OrderByDescending(x => x.Group).ToList();
75+
vm.children.Add(child.App.ToAppListVM());
15276
}
77+
78+
appListVms.Add(vm);
15379
}
80+
81+
count = searchResult.Count;
15482
}
15583

156-
var count = appvms.Count;
157-
var pageList = appvms.ToList().Skip((current - 1) * pageSize).Take(pageSize).ToList();
158-
await AppendInheritancedInfo(pageList);
84+
await AppendInheritancedInfo(appListVms);
85+
15986
return Json(new
16087
{
16188
current,
16289
pageSize,
16390
success = true,
16491
total = count,
165-
data = pageList
92+
data = appListVms
16693
});
16794
}
16895

169-
private async Task<AppListVM> AppToListVM(App item, bool appendInheritancedInfo)
170-
{
171-
var vm = item.ToAppListVM();
172-
173-
if (appendInheritancedInfo)
174-
{
175-
var inheritancedApps = await _appService.GetInheritancedAppsAsync(item.Id);
176-
vm.inheritancedApps = item.Type == AppType.Inheritance
177-
? new List<string>()
178-
: (inheritancedApps).Select(ia => ia.Id).ToList();
179-
vm.inheritancedAppNames = item.Type == AppType.Inheritance
180-
? new List<string>()
181-
: (inheritancedApps).Select(ia => ia.Name).ToList();
182-
vm.AppAdminName = (await _userService.GetUserAsync(item.AppAdmin))?.UserName;
183-
}
184-
185-
return vm;
186-
}
187-
18896
private async Task AppendInheritancedInfo(List<AppListVM> list)
18997
{
19098
foreach (var appListVm in list)
@@ -213,7 +121,6 @@ public async Task<IActionResult> Add([FromBody] AppVM model)
213121
var oldApp = await _appService.GetAsync(model.Id);
214122
if (oldApp != null)
215123
{
216-
217124
return Json(new
218125
{
219126
success = false,
@@ -303,6 +210,7 @@ public async Task<IActionResult> Edit([FromBody] AppVM model)
303210
{
304211
_tinyEventBus.Fire(new EditAppSuccessful(app, this.GetCurrentUserName()));
305212
}
213+
306214
return Json(new
307215
{
308216
success = result,
@@ -318,8 +226,9 @@ public async Task<IActionResult> All()
318226
foreach (var app in apps)
319227
{
320228
var vm = app.ToAppListVM();
321-
vm.inheritancedAppNames = app.Type == AppType.Inheritance ? new List<string>() :
322-
(await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList();
229+
vm.inheritancedAppNames = app.Type == AppType.Inheritance
230+
? new List<string>()
231+
: (await _appService.GetInheritancedAppsAsync(app.Id)).Select(ia => ia.Id).ToList();
323232
vms.Add(vm);
324233
}
325234

@@ -363,7 +272,8 @@ public async Task<IActionResult> Get(string id)
363272
/// </summary>
364273
/// <param name="id"></param>
365274
/// <returns></returns>
366-
[TypeFilter(typeof(PremissionCheckAttribute), Arguments = new object[] { "App.DisableOrEanble", Functions.App_Edit })]
275+
[TypeFilter(typeof(PremissionCheckAttribute),
276+
Arguments = new object[] { "App.DisableOrEanble", Functions.App_Edit })]
367277
[HttpPost]
368278
public async Task<IActionResult> DisableOrEanble(string id)
369279
{
@@ -440,6 +350,7 @@ public async Task<IActionResult> InheritancedApps(string currentAppId)
440350
//过滤本身
441351
apps.Remove(self);
442352
}
353+
443354
var vms = apps.Select(x =>
444355
{
445356
return new
@@ -467,8 +378,10 @@ public async Task<IActionResult> SaveAppAuth([FromBody] AppAuthVM model)
467378
{
468379
ArgumentNullException.ThrowIfNull(model);
469380

470-
var result = await _appService.SaveUserAppAuth(model.AppId, model.EditConfigPermissionUsers, _premissionService.EditConfigPermissionKey);
471-
var result1 = await _appService.SaveUserAppAuth(model.AppId, model.PublishConfigPermissionUsers, _premissionService.PublishConfigPermissionKey);
381+
var result = await _appService.SaveUserAppAuth(model.AppId, model.EditConfigPermissionUsers,
382+
_premissionService.EditConfigPermissionKey);
383+
var result1 = await _appService.SaveUserAppAuth(model.AppId, model.PublishConfigPermissionUsers,
384+
_premissionService.PublishConfigPermissionKey);
472385

473386
return Json(new
474387
{
@@ -485,8 +398,12 @@ public async Task<IActionResult> GetUserAppAuth(string appId)
485398
{
486399
AppId = appId
487400
};
488-
result.EditConfigPermissionUsers = (await _appService.GetUserAppAuth(appId, _premissionService.EditConfigPermissionKey)).Select(x => x.Id).ToList();
489-
result.PublishConfigPermissionUsers = (await _appService.GetUserAppAuth(appId, _premissionService.PublishConfigPermissionKey)).Select(x => x.Id).ToList();
401+
result.EditConfigPermissionUsers =
402+
(await _appService.GetUserAppAuth(appId, _premissionService.EditConfigPermissionKey)).Select(x => x.Id)
403+
.ToList();
404+
result.PublishConfigPermissionUsers =
405+
(await _appService.GetUserAppAuth(appId, _premissionService.PublishConfigPermissionKey))
406+
.Select(x => x.Id).ToList();
490407

491408
return Json(new
492409
{
@@ -506,4 +423,4 @@ public async Task<IActionResult> GetAppGroups()
506423
});
507424
}
508425
}
509-
}
426+
}

src/AgileConfig.Server.IService/IAppService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
namespace AgileConfig.Server.IService
77
{
8+
public class GroupedApp
9+
{
10+
public App App { get; set; }
11+
public List<GroupedApp> Children { get; set; }
12+
}
813
public interface IAppService : IDisposable
914
{
1015
Task<App> GetAsync(string id);
@@ -20,6 +25,12 @@ public interface IAppService : IDisposable
2025

2126
Task<List<App>> GetAllAppsAsync();
2227

28+
Task<(List<App> Apps, long Count)> SearchAsync(string id, string name, string group,string sortField, string ascOrDesc,
29+
int current, int pageSize);
30+
31+
Task<(List<GroupedApp> GroupedApps, long Count)> SearchGroupedAsync(string id, string name, string group,string sortField, string ascOrDesc,
32+
int current, int pageSize);
33+
2334
Task<List<App>> GetAllInheritancedAppsAsync();
2435

2536
Task<int> CountEnabledAppsAsync();

0 commit comments

Comments
 (0)