From 1fa68ab4bc3e3d519c09affd50458b853ec5d3d7 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sun, 3 Dec 2023 14:32:53 +0330 Subject: [PATCH] Fix Zero uid bug --- .../EasyMicroservices.Cores.AspCoreApi.csproj | 2 +- ...ces.Cores.AspEntityFrameworkCoreApi.csproj | 2 +- .../EasyMicroservices.Cores.Clients.csproj | 2 +- .../EasyMicroservices.Cores.Contracts.csproj | 2 +- .../Logics/DatabaseLogicInfrastructure.cs | 27 ++++++++++++------ .../EasyMicroservices.Cores.Database.csproj | 2 +- ...oservices.Cores.EntityFrameworkCore.csproj | 2 +- ...ores.Relational.EntityFrameworkCore.csproj | 2 +- .../LongIdMappedDatabaseLogicBaseTest.cs | 28 +++++++++++++++++++ 9 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index 45a1fdd..89f4367 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index aeb2bdc..9912cf9 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index d9e798e..405e6dc 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index a9fac0f..9810b59 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index 9eefa99..da6d24f 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -37,11 +37,13 @@ public DatabaseLogicInfrastructure(IBaseUnitOfWork baseUnitOfWork) _baseUnitOfWork = baseUnitOfWork; MapperProvider = baseUnitOfWork.GetMapper(); } + async Task GetIUniqueIdentityManager() { await _baseUnitOfWork.InitializeWhiteLabel(); return _baseUnitOfWork.GetUniqueIdentityManager(); } + /// /// /// @@ -57,6 +59,8 @@ protected virtual void ValidateMappedResult(ref T value) private async Task> UniqueIdentityQueryMaker(IEasyReadableQueryableAsync easyReadableQueryable, string uniqueIdentity, GetUniqueIdentityType type) where TEntity : class { + if (!typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) + return easyReadableQueryable; var uniqueIdentityManager = await GetIUniqueIdentityManager(); IEasyReadableQueryableAsync queryable = easyReadableQueryable; if (!uniqueIdentityManager.IsUniqueIdentityForThisTable(easyReadableQueryable.Context, uniqueIdentity)) @@ -737,12 +741,16 @@ public async Task> Add(IEasyWritableQueryableA } } await easyWritableQueryable.SaveChangesAsync(); - var uniqueIdentityManager = await GetIUniqueIdentityManager(); - if (uniqueIdentityManager.UpdateUniqueIdentity(easyWritableQueryable.Context, result.Entity)) + if (typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) { - await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, cancellationToken); - await easyWritableQueryable.SaveChangesAsync(); + var uniqueIdentityManager = await GetIUniqueIdentityManager(); + if (uniqueIdentityManager.UpdateUniqueIdentity(easyWritableQueryable.Context, result.Entity)) + { + await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, cancellationToken); + await easyWritableQueryable.SaveChangesAsync(); + } } + return result.Entity; } @@ -769,12 +777,15 @@ public async Task AddBulk(IEasyWritableQueryableAsync< } await easyWritableQueryable.SaveChangesAsync(); bool anyUpdate = false; - var uniqueIdentityManager = await GetIUniqueIdentityManager(); - foreach (var item in result) + if (typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) { - if (uniqueIdentityManager.UpdateUniqueIdentity(easyWritableQueryable.Context, item.Entity)) + var uniqueIdentityManager = await GetIUniqueIdentityManager(); + foreach (var item in result) { - anyUpdate = true; + if (uniqueIdentityManager.UpdateUniqueIdentity(easyWritableQueryable.Context, item.Entity)) + { + anyUpdate = true; + } } } if (anyUpdate) diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index da83771..86817a4 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index cefb4ba..bfd1ae7 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index 3f5d29d..71b9372 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.59 + 0.0.0.60 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational diff --git a/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs b/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs index 84ddcfd..1a8f723 100644 --- a/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs +++ b/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs @@ -122,12 +122,14 @@ public async Task AddAsync(string userName) Assert.Equal(user.Result, foundUser.Result.Id); Assert.Equal(addUser.UserName, foundUser.Result.UserName); Assert.NotEmpty(foundUser.Result.UniqueIdentity); + CheckUniqueIdentity(foundUser.Result.UniqueIdentity); var allUsers = await logic.GetAll(); Assert.Contains(allUsers.Result, x => x.Id == user.Result); var allFilterUsers = await logic.Filter(new FilterRequestContract() { IsDeleted = false }); + 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); @@ -136,6 +138,21 @@ public async Task AddAsync(string userName) return foundUser.Result; } + void CheckUniqueIdentity(string uniqueIdentity) + { + Assert.False(uniqueIdentity.Contains("-0-")); + Assert.False(uniqueIdentity.EndsWith("-0")); + Assert.False(uniqueIdentity.StartsWith("0")); + } + + void CheckUniqueIdentity(IEnumerable uniqueIdentities) + { + foreach (var item in uniqueIdentities) + { + CheckUniqueIdentity(item); + } + } + [Fact] public async Task FilterAsync() { @@ -214,6 +231,7 @@ public async Task AddProfileAsync(long userId, string name) UniqueIdentity = user.Result.UniqueIdentity, UserId = userId }); + CheckUniqueIdentity(addedProfile.Result.UniqueIdentity); return addedProfile.Result; } @@ -239,6 +257,7 @@ public async Task UpdateAsync(string userName, string toUserName) { Id = added.Id }); + CheckUniqueIdentity(found.Result.UniqueIdentity); Assert.NotNull(found.Result.ModificationDateTime); Assert.Equal(found.Result.CreationDateTime, added.CreationDateTime); Assert.True(found.Result.CreationDateTime > DateTime.Now.AddMinutes(-5)); @@ -259,6 +278,7 @@ public async Task UpdateChangedValuesOnlyAsync(string userName) UniqueIdentity = added.UniqueIdentity, UserName = default }); + CheckUniqueIdentity(updateResult.Result.UniqueIdentity); Assert.NotNull(updateResult.Result.ModificationDateTime); Assert.Equal(updateResult.Result.CreationDateTime, added.CreationDateTime); Assert.True(updateResult.Result.CreationDateTime > DateTime.Now.AddMinutes(-5)); @@ -374,6 +394,7 @@ public async Task UniqueIdentityAsync(string userName) UniqueIdentity = added.UniqueIdentity }); Assert.Equal(found.Result.UserName, userName); + CheckUniqueIdentity(found.Result.UniqueIdentity); var foundAll = await logic.GetAllByUniqueIdentity(new GetUniqueIdentityRequestContract() { @@ -390,6 +411,7 @@ public async Task UniqueIdentityAsync(string userName) { UniqueIdentity = added.UniqueIdentity }); + CheckUniqueIdentity(foundAllProfiles.Result.Select(x=>x.UniqueIdentity)); Assert.Contains(foundAllProfiles.Result, x => x.FirstName.StartsWith("Ali")); Assert.Equal(foundAllProfiles.Result.Count, 10); @@ -398,6 +420,7 @@ public async Task UniqueIdentityAsync(string userName) UniqueIdentity = added.UniqueIdentity }, default, q => q.Where(x => x.FirstName == "Ali5")); Assert.Contains(foundAllProfiles.Result, x => x.FirstName == "Ali5"); + CheckUniqueIdentity(onlyUniqueIdentity.Result.UniqueIdentity); } [Theory] @@ -595,16 +618,19 @@ public async Task SelfChildTestAsync(string parentName, string[] chilren) Assert.True(allResult.HasItems); Assert.True(chilren.All(x => allResult.Result.Any(y => y.Name == x))); Assert.True(allResult.Result.Any(y => y.Name == parentName)); + CheckUniqueIdentity(allResult.Result.Select(x => x.UniqueIdentity)); var onlyChildrenResult = await logic.GetAllByUniqueIdentity(foundUser.Result, type: DataTypes.GetUniqueIdentityType.OnlyChilren); Assert.True(onlyChildrenResult.HasItems); Assert.True(chilren.All(x => onlyChildrenResult.Result.Any(y => y.Name == x))); Assert.True(!onlyChildrenResult.Result.Any(y => y.Name == parentName)); + CheckUniqueIdentity(onlyChildrenResult.Result.Select(x => x.UniqueIdentity)); var onlyParentResult = await logic.GetAllByUniqueIdentity(foundUser.Result, type: DataTypes.GetUniqueIdentityType.OnlyParent); Assert.True(onlyParentResult.HasItems); Assert.True(chilren.All(x => !onlyParentResult.Result.Any(y => y.Name == x))); Assert.True(onlyParentResult.Result.Any(y => y.Name == parentName)); + CheckUniqueIdentity(onlyParentResult.Result.Select(x => x.UniqueIdentity)); } [Theory] @@ -657,11 +683,13 @@ public async Task ChildTestAsync(string parentName, string[] chilren) Assert.True(onlyChildrenResult.HasItems); Assert.True(chilren.All(x => onlyChildrenResult.Result.Any(y => y.Name == "Subcject" + x))); Assert.True(!onlyChildrenResult.Result.Any(y => y.Name == "Subcject" + parentName)); + CheckUniqueIdentity(onlyChildrenResult.Result.Select(x => x.UniqueIdentity)); var onlyParentResult = await subjectlogic.GetAllByUniqueIdentity(foundUser.Result, type: DataTypes.GetUniqueIdentityType.OnlyParent); Assert.True(onlyParentResult.HasItems); Assert.True(chilren.All(x => !onlyParentResult.Result.Any(y => y.Name == "Subcject" + x))); Assert.True(onlyParentResult.Result.Any(y => y.Name == "Subcject" + parentName)); + CheckUniqueIdentity(onlyParentResult.Result.Select(x => x.UniqueIdentity)); } } }