diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs new file mode 100644 index 0000000..d74dad8 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs @@ -0,0 +1,25 @@ +using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Database.Interfaces; +using System; +using System.Linq; +using System.Reflection; + +namespace EasyMicroservices.Cores.Database.Helpers; +/// +/// +/// +internal static class DatabaseExtensions +{ + public static void SetIdToRecordId(IContext context, TEntity entity, TRecordEntity recordEntity) + { + var idProperty = typeof(TRecordEntity) + .GetProperty(nameof(IRecordIdSchema.RecordId), BindingFlags.Public | BindingFlags.Instance); + if (idProperty == null) + throw new Exception($"I cannot find RecordId in your {typeof(TRecordEntity).Name}, Did you inherit from IRecordIdSchema?"); + + var ids = context.GetPrimaryKeyValues(entity); + if (!ids.HasAny()) + throw new Exception($"I cannot find any primary key in your {typeof(TEntity).Name}!"); + idProperty.SetValue(recordEntity, ids.First()); + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs index bf9ede7..3cccec7 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs @@ -5,7 +5,8 @@ namespace EasyMicroservices.Cores.Database.Interfaces; /// /// /// -public interface IDatabaseWidget : IWidget +public interface IDatabaseWidget : IWidget + where TEntity : class { /// /// @@ -17,6 +18,7 @@ public interface IDatabaseWidget : IWidget /// /// /// + /// /// - Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract); + Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity); } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs index f411417..ed7d7db 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs @@ -1,5 +1,4 @@ using EasyMicroservices.Cores.Interfaces; -using EasyMicroservices.Database.Interfaces; using System.Threading.Tasks; namespace EasyMicroservices.Cores.Database.Interfaces; @@ -12,8 +11,11 @@ public interface IDatabaseWidgetManager : IWidgetManager /// /// /// + /// /// /// + /// /// - Task Add(IBaseUnitOfWork baseUnitOfWork, T contract); + Task Add(IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity) + where TEntity : class; } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs index 6fbe74d..0ec2e3c 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs @@ -1,6 +1,7 @@ using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.DataTypes; using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using System; using System.Collections.Generic; @@ -18,6 +19,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces /// public interface IReadableLogic { + /// + /// + /// + /// + IContext GetReadableContext(); /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs new file mode 100644 index 0000000..23aad09 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs @@ -0,0 +1,12 @@ +namespace EasyMicroservices.Cores.Database.Interfaces; +/// +/// +/// +/// +public interface IRecordIdSchema +{ + /// + /// + /// + public TId RecordId { get; set; } +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs index 262d549..41d56eb 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs @@ -1,4 +1,5 @@ using EasyMicroservices.Cores.Contracts.Requests; +using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using System; using System.Linq.Expressions; @@ -12,6 +13,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces /// public interface IWritableLogic { + /// + /// + /// + /// + IContext GetWritableContext(); /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs index 48c8ea9..d318fc1 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs @@ -257,5 +257,25 @@ public Task SaveChangesAsync(CancellationToken cancellationToken = default) { return _easyWriteableQueryable.SaveChangesAsync(cancellationToken); } + + /// + /// + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index 1690aa9..c6de48e 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -974,7 +974,7 @@ await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, tr } } var widgetManager = _baseUnitOfWork.GetDatabaseWidgetManager(); - await widgetManager.Add(_baseUnitOfWork, contract); + await widgetManager.Add(_baseUnitOfWork, contract, result.Entity); await ActivityChangeLogLogic.AddAsync(result.Entity, _baseUnitOfWork); return result.Entity; } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs index 1f7e7a2..c344833 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs @@ -365,5 +365,23 @@ public Task SoftDeleteBulkByIds(SoftDeleteBulkRequestContract + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs index 712828b..b4cd316 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs @@ -388,5 +388,23 @@ protected Func, IEasyReadableQueryableAsync return (q) => _easyReadableQueryable.ConvertToReadable(query(q)); return null; } + + /// + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs index 4527305..539d5bd 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs @@ -14,17 +14,20 @@ public class DatabaseWidgetManager : WidgetManager, IDatabaseWidgetManager /// /// /// + /// /// /// + /// /// - public async Task Add(IBaseUnitOfWork baseUnitOfWork, T contract) + public async Task Add(IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity) + where TEntity : class { var widgets = GetWidgetsByType(typeof(T)); foreach (var widget in widgets) { - if (widget is IDatabaseWidget databaseWidget && databaseWidget.CanProcess(baseUnitOfWork)) + if (widget is IDatabaseWidget databaseWidget && databaseWidget.CanProcess(baseUnitOfWork)) { - await databaseWidget.Process(this, baseUnitOfWork, contract); + await databaseWidget.Process(this, baseUnitOfWork, contract, entity); } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs index d218051..c6970ce 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs @@ -1,4 +1,5 @@ -using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Helpers; +using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.ServiceContracts; using System; @@ -11,8 +12,10 @@ namespace EasyMicroservices.Cores.Widgets; /// /// /// -public class SimpleReportingEntityWidget : IDatabaseWidget +/// +public class SimpleReportingEntityWidget : IDatabaseWidget where TReportEntity : class + where TEntity : class where TObjectContract : class { /// @@ -29,7 +32,7 @@ public void Build() public bool CanProcess(IBaseUnitOfWork baseUnitOfWork) { if (baseUnitOfWork.LogicOptions.HasValue) - return !baseUnitOfWork.LogicOptions.Value.DoStopReporting; + return !baseUnitOfWork.LogicOptions.Value.DoStopReporting; return true; } @@ -58,18 +61,20 @@ public Task Initialize(params TObjectContract[] parameters) /// /// /// + /// /// /// - public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract) + public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract, TEntity entity) { var reportEntity = await baseUnitOfWork .GetMapper() .MapAsync(contract); - - await baseUnitOfWork.GetLogic(new Models.LogicOptions() + var logic = baseUnitOfWork.GetLogic(new Models.LogicOptions() { DoStopReporting = true - }) + }); + DatabaseExtensions.SetIdToRecordId(logic.GetReadableContext(), entity, reportEntity); + await logic .Add(reportEntity) .AsCheckedResult(); }