Browse Source

Audit Log repository GetListAsync refactoring

pull/395/head
Yunus Emre Kalkan 8 years ago
parent
commit
75143e9d2f
  1. 73
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs
  2. 11
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs
  3. 46
      modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs
  4. 34
      modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs

73
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs

@ -1,73 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Volo.Abp.Paging
{
public class PagedResult<T> : ListResultDto<T>, IPagedResult<T>
{
/// <inheritdoc />
public long TotalCount { get; set; }
/// <summary>
/// Creates a new <see cref="PagedResult{T}"/> object.
/// </summary>
public PagedResult()
{
}
/// <summary>
/// Creates a new <see cref="PagedResult{T}"/> object.
/// </summary>
/// <param name="totalCount">Total count of Items</param>
/// <param name="items">List of items in current page</param>
public PagedResult(long totalCount, IReadOnlyList<T> items)
: base(items)
{
TotalCount = totalCount;
}
}
public class ListResultDto<T> : IListResult<T>
{
/// <inheritdoc />
public IReadOnlyList<T> Items
{
get { return _items ?? (_items = new List<T>()); }
set { _items = value; }
}
private IReadOnlyList<T> _items;
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
public ListResultDto()
{
}
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
/// <param name="items">List of items</param>
public ListResultDto(IReadOnlyList<T> items)
{
Items = items;
}
}
public interface IListResult<T>
{
IReadOnlyList<T> Items { get; set; }
}
public interface IPagedResult<T> : IListResult<T>, IHasTotalCount
{
}
public interface IHasTotalCount
{
long TotalCount { get; set; }
}
}

11
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
@ -7,15 +8,21 @@ namespace Volo.Abp.AuditLogging
{ {
public interface IAuditLogRepository : IBasicRepository<AuditLog, Guid> public interface IAuditLogRepository : IBasicRepository<AuditLog, Guid>
{ {
Task<Paging.PagedResult<AuditLog>> GetListAsync( Task<List<AuditLog>> GetListAsync(
string sorting = null, string sorting = null,
int maxResultCount = 50, int maxResultCount = 50,
int skipCount = 0, int skipCount = 0,
string filter = null,
string httpMethod = null, string httpMethod = null,
string url = null, string url = null,
string userName = null, string userName = null,
HttpStatusCode? httpStatusCode = null, HttpStatusCode? httpStatusCode = null,
bool includeDetails = false); bool includeDetails = false);
Task<long> GetCountAsync(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true);
} }
} }

46
modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using System.Net; using System.Net;
@ -17,31 +18,52 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore
} }
public async Task<Paging.PagedResult<AuditLog>> GetListAsync( public async Task<List<AuditLog>> GetListAsync(
string sorting = null, string sorting = null,
int maxResultCount = 50, int maxResultCount = 50,
int skipCount = 0, int skipCount = 0,
string filter = null,
string httpMethod = null, string httpMethod = null,
string url = null, string url = null,
string userName = null, string userName = null,
HttpStatusCode? httpStatusCode = null, HttpStatusCode? httpStatusCode = null,
bool includeDetails = false) bool includeDetails = true)
{ {
var query = DbSet.AsNoTracking() var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
.IncludeDetails(includeDetails)
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
var totalCount = await query.LongCountAsync();
var auditLogs = await query.OrderBy(sorting) var auditLogs = await query.OrderBy(sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToListAsync(); .ToListAsync();
return new Paging.PagedResult<AuditLog>(totalCount, auditLogs); return auditLogs;
}
public async Task<long> GetCountAsync(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
var totalCount = await query.LongCountAsync();
return totalCount;
}
private IQueryable<AuditLog> GetListQuery(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
return DbSet.AsNoTracking()
.IncludeDetails(includeDetails)
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
} }
public override IQueryable<AuditLog> WithDetails() public override IQueryable<AuditLog> WithDetails()

34
modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using System.Net; using System.Net;
@ -18,22 +19,35 @@ namespace Volo.Abp.AuditLogging.MongoDB
} }
public async Task<Paging.PagedResult<AuditLog>> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, string filter = null, public async Task<List<AuditLog>> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0,
string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false)
{ {
var query = GetMongoQueryable() var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
var totalCount = await query.As<IMongoQueryable<AuditLog>>().LongCountAsync(); return await query.OrderBy(sorting).As<IMongoQueryable<AuditLog>>()
var questions = await query.OrderBy(sorting).As<IMongoQueryable<AuditLog>>()
.PageBy<AuditLog, IMongoQueryable<AuditLog>>(skipCount, maxResultCount) .PageBy<AuditLog, IMongoQueryable<AuditLog>>(skipCount, maxResultCount)
.ToListAsync(); .ToListAsync();
}
public async Task<long> GetCountAsync(string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false)
{
var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
return new Paging.PagedResult<AuditLog>(totalCount, questions); return await query.As<IMongoQueryable<AuditLog>>().LongCountAsync();
}
private IQueryable<AuditLog> GetListQuery(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
return GetMongoQueryable()
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
} }
} }
} }

Loading…
Cancel
Save