Skip to content

Commit

Permalink
Merge pull request #135 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Fix Update  and add
  • Loading branch information
Ali-YousefiTelori authored Jan 12, 2024
2 parents bf94011 + b610755 commit c5517dd
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 19 deletions.
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.0.98</Version>
<Version>0.0.0.99</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.0.98</Version>
<Version>0.0.0.99</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.0.98</Version>
<Version>0.0.0.99</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.0.98</Version>
<Version>0.0.0.99</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 @@ -11,12 +11,11 @@ public interface IUniqueIdentityManager
/// <summary>
/// update unique identity
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="currentUserUniqueIdentity"></param>
/// <param name="context"></param>
/// <param name="entity"></param>
/// <returns>is need update database</returns>
bool UpdateUniqueIdentity<TEntity>(string currentUserUniqueIdentity, IContext context, TEntity entity);
bool UpdateUniqueIdentity(string currentUserUniqueIdentity, IContext context, object entity);
/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ private async Task<ListMessageContract<TEntity>> InternalUpdateBulk<TEntity>(IEa
return (FailedReasonType.AccessDenied, $"Some items you want to update not found!");

var result = await easyWritableQueryable.UpdateBulkAsync(entities, cancellationToken);
var uniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
var currentUserUniqueIdentity = await _baseUnitOfWork.GetCurrentUserUniqueIdentity(_logicOptions);
var uniqueIdentityManager = await GetIUniqueIdentityManager();

foreach (var entityEntry in easyWritableQueryable.Context.GetTrackerEntities().ToArray())
{
Expand All @@ -593,7 +594,10 @@ private async Task<ListMessageContract<TEntity>> InternalUpdateBulk<TEntity>(IEa
}
if (entityEntry.Entity is IUniqueIdentitySchema uidschema)
{
uidschema.UniqueIdentity = uniqueIdentity;
if (uniqueIdentityManager.UpdateUniqueIdentity(currentUserUniqueIdentity, easyWritableQueryable.Context, entityEntry.Entity))
{
FixUniqueIdentity(easyWritableQueryable.Context,typeof(TEntity), [entityEntry]);
}
}
}
continue;
Expand Down Expand Up @@ -910,9 +914,9 @@ await ActivityChangeLogLogic.ChangeLogAsync<TEntity, TId>(getResult.Result.Selec

#region Add

void FixUniqueIdentity<TEntity>(IContext context, IEntityEntry[] entityEntries)
void FixUniqueIdentity(IContext context, Type entityType, IEntityEntry[] entityEntries)
{
if (!typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity)))
if (!typeof(IUniqueIdentitySchema).IsAssignableFrom(entityType))
return;
var find = entityEntries.Select(x => x.Entity).Where(x => x is IUniqueIdentitySchema).Cast<IUniqueIdentitySchema>().FirstOrDefault(x => x.UniqueIdentity.HasValue());
if (find == null)
Expand All @@ -926,7 +930,6 @@ void FixUniqueIdentity<TEntity>(IContext context, IEntityEntry[] entityEntries)
uniqueIdentity.UniqueIdentity += "-" + DefaultUniqueIdentityManager.GenerateUniqueIdentity((long)primaryKeys.First());
}
}
return;
}

/// <summary>
Expand Down Expand Up @@ -960,7 +963,7 @@ public async Task<MessageContract<TEntity>> Add<TEntity>(IEasyWritableQueryableA
var uniqueIdentityManager = await GetIUniqueIdentityManager();
if (uniqueIdentityManager.UpdateUniqueIdentity(currentUserUniqueIdentity, easyWritableQueryable.Context, result.Entity))
{
FixUniqueIdentity<TEntity>(easyWritableQueryable.Context, allItems);
FixUniqueIdentity(easyWritableQueryable.Context, typeof(TEntity), allItems);
await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, true, cancellationToken)
.AsCheckedResult();
await easyWritableQueryable.SaveChangesAsync();
Expand Down Expand Up @@ -1007,7 +1010,7 @@ public async Task<ListMessageContract<TEntity>> AddBulk<TEntity>(IEasyWritableQu
anyUpdate = true;
}
}
FixUniqueIdentity<TEntity>(easyWritableQueryable.Context, allItems);
FixUniqueIdentity(easyWritableQueryable.Context, typeof(TEntity), allItems);
}
if (anyUpdate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ public DefaultUniqueIdentityManager(WhiteLabelInfo whiteLabelInfo)
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="currentUserUniqueIdentity"></param>
/// <param name="context"></param>
/// <param name="entity"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool UpdateUniqueIdentity<TEntity>(string currentUserUniqueIdentity, IContext context, TEntity entity)
public bool UpdateUniqueIdentity(string currentUserUniqueIdentity, IContext context, object entity)
{
if (entity is IUniqueIdentitySchema uniqueIdentitySchema)
{
Expand All @@ -43,7 +42,7 @@ public bool UpdateUniqueIdentity<TEntity>(string currentUserUniqueIdentity, ICon
if (uniqueIdentitySchema.UniqueIdentity.IsNullOrEmpty())
uniqueIdentitySchema.UniqueIdentity = _whiteLabelInfo.StartUniqueIdentity;
var ids = uniqueIdentitySchema.UniqueIdentity.IsNullOrEmpty() ? null : DecodeUniqueIdentity(uniqueIdentitySchema.UniqueIdentity);
if (TableIds.TryGetValue(GetContextTableName<TEntity>(context.ContextType, _whiteLabelInfo.MicroserviceId), out long tableId))
if (TableIds.TryGetValue(GetContextTableName(context.ContextType, entity.GetType(), _whiteLabelInfo.MicroserviceId), out long tableId))
{
if (entity is IIdSchema<long> longIdSchema)
{
Expand Down Expand Up @@ -170,6 +169,15 @@ public string GetContextTableName<TEntity>(Type contextType, long microserviceId
return GetContextTableName(microserviceId, GetContextName(contextType), GetTableName(typeof(TEntity)));
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetContextTableName(Type contextType, Type tableType, long microserviceId)
{
return GetContextTableName(microserviceId, GetContextName(contextType), GetTableName(tableType));
}

/// <summary>
///
/// </summary>
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.0.98</Version>
<Version>0.0.0.99</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>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.98</Version>
<Version>0.0.0.99</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>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.98</Version>
<Version>0.0.0.99</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down

0 comments on commit c5517dd

Please sign in to comment.