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

Add OrderBy top the first/firstordefault operator #6396

Merged
merged 3 commits into from
Dec 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public EfCoreAuditLogRepository(IDbContextProvider<IAuditLoggingDbContext> dbCon

return result.ToDictionary(element => element.Day.ClearTime(), element => element.avgExecutionTime);
}

public override IQueryable<AuditLog> WithDetails()
{
return GetQueryable().IncludeDetails();
Expand All @@ -147,8 +147,8 @@ public virtual async Task<EntityChange> GetEntityChange(Guid entityChangeId)
var entityChange = await DbContext.Set<EntityChange>()
.AsNoTracking()
.IncludeDetails()
.OrderBy(x => x.Id)
.Where(x => x.Id == entityChangeId)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync();

if (entityChange == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MongoAuditLogRepository(IMongoDbContextProvider<IAuditLoggingMongoDbConte

public virtual async Task<List<AuditLog>> GetListAsync(
string sorting = null,
int maxResultCount = 50,
int maxResultCount = 50,
int skipCount = 0,
DateTime? startTime = null,
DateTime? endTime = null,
Expand Down Expand Up @@ -145,9 +145,10 @@ public virtual async Task<EntityChange> GetEntityChange(Guid entityChangeId)
{
var entityChange = (await GetMongoQueryable()
.Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId))
.OrderBy(x => x.Id)
.FirstAsync()).EntityChanges.FirstOrDefault(x => x.Id == entityChangeId);


if (entityChange == null)
{
throw new EntityNotFoundException(typeof(EntityChange));
Expand All @@ -174,7 +175,7 @@ public virtual async Task<EntityChange> GetEntityChange(Guid entityChangeId)
var auditLogs = await query.As<IMongoQueryable<EntityChange>>()
.PageBy<EntityChange, IMongoQueryable<EntityChange>>(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));

return auditLogs.AsQueryable().OrderBy(sorting ?? "changeTime desc").ToList();
}

Expand All @@ -188,7 +189,7 @@ public virtual async Task<EntityChange> GetEntityChange(Guid entityChangeId)
CancellationToken cancellationToken = default)
{
var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName);

var count = await query.As<IMongoQueryable<EntityChange>>().LongCountAsync(GetCancellationToken(cancellationToken));

return count;
Expand Down Expand Up @@ -216,7 +217,7 @@ public virtual async Task<List<EntityChangeWithUsername>> GetEntityChangesWithUs
.ToListAsync();

var entityChanges = auditLogs.SelectMany(x => x.EntityChanges).ToList();

entityChanges.RemoveAll(x => x.EntityId != entityId || x.EntityTypeFullName != entityTypeFullName);

return entityChanges.Select(x => new EntityChangeWithUsername()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public EfCoreFeatureValueRepository(IDbContextProvider<IFeatureManagementDbConte
public virtual async Task<FeatureValue> FindAsync(string name, string providerName, string providerKey)
{
return await DbSet
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public MongoFeatureValueRepository(IMongoDbContextProvider<IFeatureManagementMon
public virtual async Task<FeatureValue> FindAsync(string name, string providerName, string providerKey)
{
return await GetMongoQueryable()
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public EFCoreIdentitySecurityLogRepository(IDbContextProvider<IIdentityDbContext

public async Task<IdentitySecurityLog> GetByUserIdAsync(Guid id, Guid userId, bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await DbSet.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, GetCancellationToken(cancellationToken));
return await DbSet.OrderBy(x => x.Id).FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, GetCancellationToken(cancellationToken));
}

protected virtual IQueryable<IdentitySecurityLog> GetListQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public EfCoreIdentityLinkUserRepository(IDbContextProvider<IIdentityDbContext> d

public async Task<IdentityLinkUser> FindAsync(IdentityLinkUserInfo sourceLinkUserInfo, IdentityLinkUserInfo targetLinkUserInfo, CancellationToken cancellationToken = default)
{
return await DbSet.FirstOrDefaultAsync(x =>
return await DbSet
.OrderBy(x => x.Id).FirstOrDefaultAsync(x =>
x.SourceUserId == sourceLinkUserInfo.UserId && x.SourceTenantId == sourceLinkUserInfo.TenantId &&
x.TargetUserId == targetLinkUserInfo.UserId && x.TargetTenantId == targetLinkUserInfo.TenantId ||
x.TargetUserId == sourceLinkUserInfo.UserId && x.TargetTenantId == sourceLinkUserInfo.TenantId &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public EfCoreIdentityRoleRepository(IDbContextProvider<IIdentityDbContext> dbCon
{
return await DbSet
.IncludeDetails(includeDetails)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, GetCancellationToken(cancellationToken));
}

Expand Down Expand Up @@ -76,4 +77,4 @@ public override IQueryable<IdentityRole> WithDetails()
return GetQueryable().IncludeDetails();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public EfCoreIdentityUserRepository(IDbContextProvider<IIdentityDbContext> dbCon
{
return await DbSet
.IncludeDetails(includeDetails)
.OrderBy(x => x.NormalizedUserName)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
GetCancellationToken(cancellationToken)
Expand Down Expand Up @@ -80,6 +80,7 @@ select ouRole.RoleId
return await DbSet
.IncludeDetails(includeDetails)
.Where(u => u.Logins.Any(login => login.LoginProvider == loginProvider && login.ProviderKey == providerKey))
.OrderBy(x=>x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
}

Expand All @@ -90,7 +91,7 @@ select ouRole.RoleId
{
return await DbSet
.IncludeDetails(includeDetails)
.OrderBy(x => x.NormalizedEmail)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, GetCancellationToken(cancellationToken));
}

Expand All @@ -112,6 +113,7 @@ select ouRole.RoleId
{
var role = await DbContext.Roles
.Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));

if (role == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class EfCoreOrganizationUnitRepository
{
return await DbSet
.IncludeDetails(includeDetails)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
ou => ou.DisplayName == displayName,
GetCancellationToken(cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public MongoIdentityLinkUserRepository(IMongoDbContextProvider<IAbpIdentityMongo

public async Task<IdentityLinkUser> FindAsync(IdentityLinkUserInfo sourceLinkUserInfo, IdentityLinkUserInfo targetLinkUserInfo, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(x =>
return await GetMongoQueryable()
.OrderBy(x => x.Id).FirstOrDefaultAsync(x =>
x.SourceUserId == sourceLinkUserInfo.UserId && x.SourceTenantId == sourceLinkUserInfo.TenantId &&
x.TargetUserId == targetLinkUserInfo.UserId && x.TargetTenantId == targetLinkUserInfo.TenantId ||
x.TargetUserId == sourceLinkUserInfo.UserId && x.TargetTenantId == sourceLinkUserInfo.TenantId &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public MongoIdentityRoleRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, GetCancellationToken(cancellationToken));
return await GetMongoQueryable()
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, GetCancellationToken(cancellationToken));
}

public async Task<List<IdentityRole>> GetListAsync(
Expand Down Expand Up @@ -72,4 +74,4 @@ public MongoIdentityRoleRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
.LongCountAsync(GetCancellationToken(cancellationToken));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public MongoIdentitySecurityLogRepository(IMongoDbContextProvider<IAbpIdentityMo
public async Task<IdentitySecurityLog> GetByUserIdAsync(Guid id, Guid userId, bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId,
return await GetMongoQueryable().OrderBy(x => x.Id).FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId,
GetCancellationToken(cancellationToken));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
GetCancellationToken(cancellationToken)
Expand Down Expand Up @@ -81,6 +82,7 @@ public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
{
return await GetMongoQueryable()
.Where(u => u.Logins.Any(login => login.LoginProvider == loginProvider && login.ProviderKey == providerKey))
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
}

Expand All @@ -89,7 +91,8 @@ public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, GetCancellationToken(cancellationToken));
return await GetMongoQueryable()
.OrderBy(x => x.Id).FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, GetCancellationToken(cancellationToken));
}

public virtual async Task<List<IdentityUser>> GetListByClaimAsync(
Expand All @@ -107,7 +110,10 @@ public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbCo
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var role = await DbContext.Roles.AsQueryable().Where(x => x.NormalizedName == normalizedRoleName).FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
var role = await DbContext.Roles.AsQueryable()
.Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));

if (role == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class MongoOrganizationUnitRepository
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
ou => ou.DisplayName == displayName,
GetCancellationToken(cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class ApiScopeRepository : EfCoreRepository<IIdentityServerDbContext, Api

public async Task<ApiScope> GetByNameAsync(string scopeName, bool includeDetails = true, CancellationToken cancellationToken = default)
{
return await DbSet.FirstOrDefaultAsync(x => x.Name == scopeName, GetCancellationToken(cancellationToken));
return await DbSet
.OrderBy(x=>x.Id)
.FirstOrDefaultAsync(x => x.Name == scopeName, GetCancellationToken(cancellationToken));
}

public async Task<List<ApiScope>> GetListByNameAsync(string[] scopeNames, bool includeDetails = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public EfCorePermissionGrantRepository(IDbContextProvider<IPermissionManagementD
CancellationToken cancellationToken = default)
{
return await DbSet
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(s =>
s.Name == name &&
s.ProviderName == providerName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public MongoPermissionGrantRepository(IMongoDbContextProvider<IPermissionManagem
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(s =>
s.Name == name &&
s.ProviderName == providerName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public EfCoreSettingRepository(IDbContextProvider<ISettingManagementDbContext> d
public virtual async Task<Setting> FindAsync(string name, string providerName, string providerKey)
{
return await DbSet
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public MongoSettingRepository(IMongoDbContextProvider<ISettingManagementMongoDbC

public virtual async Task<Setting> FindAsync(string name, string providerName, string providerKey)
{
return await GetMongoQueryable().FirstOrDefaultAsync(s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey);
return await GetMongoQueryable().OrderBy(x => x.Id).FirstOrDefaultAsync(s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey);
}

public virtual async Task<List<Setting>> GetListAsync(string providerName, string providerKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected EfCoreUserRepositoryBase(IDbContextProvider<TDbContext> dbContextProvi

public async Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default)
{
return await this.FirstOrDefaultAsync(u => u.UserName == userName, GetCancellationToken(cancellationToken));
return await this.OrderBy(x => x.Id).FirstOrDefaultAsync(u => u.UserName == userName, GetCancellationToken(cancellationToken));
}

public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
Expand All @@ -31,8 +31,8 @@ public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, Cance
}

public async Task<List<TUser>> SearchAsync(
string sorting = null,
int maxResultCount = int.MaxValue,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
CancellationToken cancellationToken = default)
Expand All @@ -52,7 +52,7 @@ public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, Cance
}

public async Task<long> GetCountAsync(
string filter = null,
string filter = null,
CancellationToken cancellationToken = default)
{
return await DbSet
Expand All @@ -67,4 +67,4 @@ public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, Cance
.LongCountAsync(GetCancellationToken(cancellationToken));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected MongoUserRepositoryBase(IMongoDbContextProvider<TDbContext> dbContextP

public virtual async Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(u => u.UserName == userName, GetCancellationToken(cancellationToken));
return await GetMongoQueryable().OrderBy(x => x.Id).FirstOrDefaultAsync(u => u.UserName == userName, GetCancellationToken(cancellationToken));
}

public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
Expand All @@ -32,7 +32,7 @@ public virtual async Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, Cance
}

public async Task<List<TUser>> SearchAsync(
string sorting = null,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
Expand Down Expand Up @@ -67,4 +67,4 @@ public async Task<long> GetCountAsync(string filter = null, CancellationToken ca
.LongCountAsync(GetCancellationToken(cancellationToken));
}
}
}
}