重构代码:程序异常使用MongoDB的Find与Sort

This commit is contained in:
Argo Zhang 2019-03-17 16:18:09 +08:00
parent 45adc853e2
commit 8846519bba
1 changed files with 17 additions and 15 deletions

View File

@ -66,31 +66,33 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override Page<DataAccess.Exceptions> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) public override Page<DataAccess.Exceptions> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime)
{ {
var exceps = DbManager.Exceptions.Find(FilterDefinition<DataAccess.Exceptions>.Empty).ToList(); // filter
var filterBuilder = Builders<DataAccess.Exceptions>.Filter;
var filter = filterBuilder.Empty;
if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddDays(-7));
// sort // sort
var orderProxy = po.Order == "asc" ? var sortBuilder = Builders<DataAccess.Exceptions>.Sort;
new Func<Func<DataAccess.Exceptions, string>, List<DataAccess.Exceptions>>(p => exceps.OrderBy(p).ToList()) : SortDefinition<DataAccess.Exceptions> sort = null;
new Func<Func<DataAccess.Exceptions, string>, List<DataAccess.Exceptions>>(p => exceps.OrderByDescending(p).ToList());
var logTimeProxy = po.Order == "asc" ?
new Func<Func<DataAccess.Exceptions, DateTime>, List<DataAccess.Exceptions>>(p => exceps.OrderBy(p).ToList()) :
new Func<Func<DataAccess.Exceptions, DateTime>, List<DataAccess.Exceptions>>(p => exceps.OrderByDescending(p).ToList());
switch (po.Sort) switch (po.Sort)
{ {
case "LogTime":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime);
break;
case "ErrorPage": case "ErrorPage":
exceps = orderProxy(ex => ex.ErrorPage); sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.ErrorPage) : sortBuilder.Descending(t => t.ErrorPage);
break; break;
case "UserId": case "UserId":
exceps = orderProxy(ex => ex.UserId); sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserId) : sortBuilder.Descending(t => t.UserId);
break; break;
case "UserIp": case "UserIp":
exceps = orderProxy(ex => ex.UserIp); sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserIp) : sortBuilder.Descending(t => t.UserIp);
break;
case "LogTime":
exceps = logTimeProxy(ex => ex.LogTime);
break; break;
} }
var exceps = DbManager.Exceptions.Find(filter).Sort(sort).ToList();
return new Page<DataAccess.Exceptions>() return new Page<DataAccess.Exceptions>()
{ {
Context = exceps, Context = exceps,