Skip to content

Commit

Permalink
Merge pull request #156 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Fix bug #155 Filter paging is not working when you delete list of items.
  • Loading branch information
Ali-YousefiTelori authored Feb 7, 2024
2 parents b37843b + 0ef301e commit fde7845
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,client,clients</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ protected async Task<ListMessageContract<TEntity>> Filter<TEntity>(FilterRequest
if (query != null)
queryable = query(queryable);
var countQueryable = queryable;

if (filterRequest.IsDeleted.HasValue && typeof(ISoftDeleteSchema).IsAssignableFrom(typeof(TEntity)))
{
queryable = queryable.ConvertToReadable(queryable.Where(x => (x as ISoftDeleteSchema).IsDeleted == filterRequest.IsDeleted));
countQueryable = countQueryable.ConvertToReadable(countQueryable.Where(x => (x as ISoftDeleteSchema).IsDeleted == filterRequest.IsDeleted));
}

if (filterRequest.Index.HasValue)
queryable = queryable.ConvertToReadable(queryable.Skip((int)filterRequest.Index.Value));
if (filterRequest.Length.HasValue)
Expand Down Expand Up @@ -438,11 +445,6 @@ protected async Task<ListMessageContract<TEntity>> Filter<TEntity>(FilterRequest
queryable = queryable.ConvertToReadable(queryable.Where(x => (x as ISoftDeleteSchema).DeletedDateTime <= filterRequest.ToDeletedDateTime));
countQueryable = countQueryable.ConvertToReadable(countQueryable.Where(x => (x as ISoftDeleteSchema).DeletedDateTime <= filterRequest.ToDeletedDateTime));
}
if (filterRequest.IsDeleted.HasValue && typeof(ISoftDeleteSchema).IsAssignableFrom(typeof(TEntity)))
{
queryable = queryable.ConvertToReadable(queryable.Where(x => (x as ISoftDeleteSchema).IsDeleted == filterRequest.IsDeleted));
countQueryable = countQueryable.ConvertToReadable(countQueryable.Where(x => (x as ISoftDeleteSchema).IsDeleted == filterRequest.IsDeleted));
}
ListMessageContract<TEntity> result = await queryable.ToListAsync(cancellationToken);
result.TotalCount = await countQueryable.CountAsync(cancellationToken);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.49</Version>
<Version>0.0.1.50</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public async Task<UserEntity> AddAsync(string userName)
var allFilterUsers = await logic.Filter(new FilterRequestContract()
{
IsDeleted = false
});
CheckUniqueIdentity(allFilterUsers.Result.Select(x=>x.UniqueIdentity));
}, q => q.Where(x => !x.UserName.StartsWith("Bug_")));
CheckUniqueIdentity(allFilterUsers.Result.Select(x => x.UniqueIdentity));
Assert.True(allFilterUsers.Result.All(x => allUsers.Result.Any(i => x.Id == i.Id)));
Assert.True(allFilterUsers.TotalCount > 0);
var ids = DefaultUniqueIdentityManager.DecodeUniqueIdentity(foundUser.Result.UniqueIdentity);
Expand Down Expand Up @@ -411,7 +411,7 @@ public async Task UniqueIdentityAsync(string userName)
{
UniqueIdentity = added.UniqueIdentity
});
CheckUniqueIdentity(foundAllProfiles.Result.Select(x=>x.UniqueIdentity));
CheckUniqueIdentity(foundAllProfiles.Result.Select(x => x.UniqueIdentity));
Assert.Contains(foundAllProfiles.Result, x => x.FirstName.StartsWith("Ali"));
Assert.Equal(foundAllProfiles.Result.Count, 10);

Expand All @@ -437,7 +437,7 @@ public async Task HardDeleteAsync(string userName)
Id = added.Id
});
Assert.Equal(found.Result.Id, added.Id);

var deleted = await logic.HardDeleteById(new DeleteRequestContract<long>()
{
Id = found.Result.Id
Expand Down Expand Up @@ -470,7 +470,7 @@ public async Task HardDeleteBulkAsync(string name, string[] userNames)
{
Ids = ids
});
Assert.True(deleted,deleted.ToString());
Assert.True(deleted, deleted.ToString());
foreach (var item in ids)
{
var found = await logic.GetById(new GetByIdRequestContract<long>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.Contracts.Requests;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Logics;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Tests.Contracts.Common;
using EasyMicroservices.Cores.Tests.Database;
using EasyMicroservices.Cores.Tests.DatabaseLogics.Database.Contexts;
using EasyMicroservices.Cores.Tests.DatabaseLogics.Database.Entities;
using EasyMicroservices.Cores.Tests.Fixtures;
using EasyMicroservices.Database.EntityFrameworkCore.Providers;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.CompileTimeMapper.Providers;
using EasyMicroservices.Mapper.Interfaces;
using EasyMicroservices.Mapper.SerializerMapper.Providers;

namespace EasyMicroservices.Cores.Tests.Issues.BugReports._2024._02;
public class BugReport_2024_02_Test : IClassFixture<ServiceProviderFixture>, IClassFixture<WhiteLabelLaboratoryFixture>
{
private readonly IServiceProvider _serviceProvider;
public BugReport_2024_02_Test(ServiceProviderFixture fixture)
{
_serviceProvider = fixture.ServiceProvider;
}

UnitOfWork GetUnitOfWork()
{
return new UnitOfWork(_serviceProvider);
}

IContractLogic<UserEntity, UserEntity, UserEntity, UserEntity, long> GetContractLogic()
{
return new LongIdMappedDatabaseLogicBase<UserEntity, UserEntity, UserEntity, UserEntity>(GetDatabase().GetReadableOf<UserEntity>(), GetDatabase().GetWritableOf<UserEntity>(), GetUnitOfWork());
}

IContractLogic<UserEntity, UpdateUserContract, UpdateUserContract, UserEntity, long> GetUpdateContractLogic()
{
return new LongIdMappedDatabaseLogicBase<UserEntity, UpdateUserContract, UpdateUserContract, UserEntity>(GetDatabase().GetReadableOf<UserEntity>(), GetDatabase().GetWritableOf<UserEntity>(), GetUnitOfWork());
}

public virtual IDatabase GetDatabase()
{
return new EntityFrameworkCoreDatabaseProvider(new MyTestContext(new DatabaseBuilder()));
}

const long TableContextId = 9;
const long ProfileTableContextId = 151;
const long CategoryTableContextId = 152;
const long SubjectTableContextId = 153;
public virtual IUniqueIdentityManager GetUniqueIdentityManager()
{
var manager = new DefaultUniqueIdentityManager(new Models.WhiteLabelInfo()
{
StartUniqueIdentity = "1-1",
MicroserviceId = 5,
MicroserviceName = "TestExample"
});
manager.InitializeTables(5, manager.GetContextName(typeof(MyTestContext)), manager.GetTableName(typeof(UserEntity)), TableContextId);
manager.InitializeTables(5, manager.GetContextName(typeof(MyTestContext)), manager.GetTableName(typeof(ProfileEntity)), ProfileTableContextId);
manager.InitializeTables(5, manager.GetContextName(typeof(MyTestContext)), manager.GetTableName(typeof(CategoryEntity)), CategoryTableContextId);
manager.InitializeTables(5, manager.GetContextName(typeof(MyTestContext)), manager.GetTableName(typeof(SubjectEntity)), SubjectTableContextId);
return manager;
}

public virtual IMapperProvider GetMapper()
{
var mapper = new CompileTimeMapperProvider(new SerializerMapperProvider(new EasyMicroservices.Serialization.Newtonsoft.Json.Providers.NewtonsoftJsonProvider()));
return mapper;
}

/// <summary>
/// https://github.com/EasyMicroservices/Cores/issues/155
/// </summary>
/// <returns></returns>
[Fact]
public async Task Bug_FilterPagingDeletedItems_155()
{
await using var logic = GetContractLogic();
List<long> users = new List<long>();
string prefix = "Bug_155-";
for (int i = 0; i < 30; i++)
{
var addUser = new UserEntity()
{
UserName = prefix + Guid.NewGuid().ToString(),
};
var userResult = await logic.Add(addUser);
Assert.True(userResult);
if (i < 10)
users.Add(userResult);
}

var foundUser = await logic.Filter(new FilterRequestContract
{
Index = 0,
Length = 10
}, q => q.Where(x => x.UserName.StartsWith("Bug_155-")));

Assert.True(foundUser.IsSuccess);
Assert.True(foundUser.Result.Count == 10);

await using var deleteLogic = GetContractLogic();
var softDeleteResult = await deleteLogic.SoftDeleteBulkByIds(users);
Assert.True(softDeleteResult.IsSuccess);

var foundUser2 = await logic.Filter(new FilterRequestContract
{
Index = 0,
Length = 10
}, q => q.Where(x => x.UserName.StartsWith("Bug_155-")));

Assert.True(foundUser2.IsSuccess);
Assert.True(foundUser2.Result.Count == 10);
Assert.True(foundUser2.TotalCount == 20);

var foundUser3 = await logic.Filter(new FilterRequestContract
{
Index = 10,
Length = 10
}, q => q.Where(x => x.UserName.StartsWith("Bug_155-")));

Assert.True(foundUser3.IsSuccess);
Assert.True(foundUser3.Result.Count == 10);
Assert.True(foundUser3.TotalCount == 20);
Assert.True(foundUser2.Result.TrueForAll(x => foundUser3.Result.TrueForAll(y => y.Id != x.Id)));
}
}

0 comments on commit fde7845

Please sign in to comment.