Skip to content

Commit

Permalink
Add support for Widget, Reporting Widget only for add #157
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Feb 13, 2024
1 parent 29853e5 commit 81e297e
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EasyMicroservices.Cores.AspCoreApi.Managers;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Logics;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces;
Expand All @@ -20,6 +21,7 @@ public static async Task<IServiceProvider> Init(int port, int? whiteLabelPort, A
app.Services.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService<IEntityFrameworkCoreDatabaseBuilder>()));
app.Services.AddTransient<IEntityFrameworkCoreDatabaseBuilder, DatabaseBuilder>();
app.Services.AddSingleton(service => new WhiteLabelManager(service));
app.Services.AddSingleton<IDatabaseWidgetManager, DatabaseWidgetManager>();
app.Services.AddSingleton<IUniqueIdentityManager, DefaultUniqueIdentityManager>((provider) =>
{
return new DefaultUniqueIdentityManager(provider.GetService<WhiteLabelManager>().CurrentWhiteLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Logics;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Database.Widgets;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Builders;
using EasyMicroservices.Cores.Widgets;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
Expand Down Expand Up @@ -77,6 +79,8 @@ public static IServiceCollection Builder<TContext>(this IServiceCollection servi
services.AddHttpContextAccessor();
services.AddScoped<IUnitOfWork>(service => new UnitOfWork(service));
services.AddScoped<IBaseUnitOfWork, UnitOfWork>();
services.AddSingleton<IDatabaseWidgetManager, DatabaseWidgetManager>();
services.AddSingleton<IWidgetManager, WidgetManager>();
services.AddSingleton(service => new WhiteLabelManager(service));
services.AddSingleton<IUniqueIdentityManager, DefaultUniqueIdentityManager>((provider) =>
{
Expand Down Expand Up @@ -369,6 +373,9 @@ public static async Task<WebApplication> Use<TContext>(this WebApplication webAp

using (var scope = webApplication.Services.CreateAsyncScope())
{
var init = scope.ServiceProvider.GetService<ReportingBuilderWidget>();
if (init != null)
init.Build();
var dbbuilder = new DatabaseCreator();
using var context = scope.ServiceProvider.GetRequiredService<TContext>();
dbbuilder.Initialize(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,21 @@ public virtual IDatabase GetDatabase<TContext>()
}

#region ContractLogic

LogicOptions? _defaultLogicOptions;
/// <summary>
///
/// </summary>
public virtual void SetDefaultLogicOptions(LogicOptions logicOptions)
public LogicOptions? LogicOptions
{
_defaultLogicOptions = logicOptions;
get
{
return _defaultLogicOptions;
}
set
{
_defaultLogicOptions = value;
}
}

LogicOptions GetLogicOptions(LogicOptions logicOptions)
Expand Down Expand Up @@ -596,5 +604,14 @@ public virtual ServiceAddressInfo GetServiceAddress(string name)
?.Where(x => x.Name.HasValue())
.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public IDatabaseWidgetManager GetDatabaseWidgetManager()
{
return GetService<IDatabaseWidgetManager>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public LogicOptions()
/// <summary>
///
/// </summary>
public bool DoStopReporting { get; set; }
/// <summary>
///
/// </summary>
public UniqueIdentityStrategy UniqueIdentityStrategy
{
get
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using EasyMicroservices.Cores.Interfaces;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.Database.Interfaces;
/// <summary>
///
/// </summary>
public interface IDatabaseWidget<T> : IWidget<T>
{
/// <summary>
///
/// </summary>
bool CanProcess(IBaseUnitOfWork baseUnitOfWork);
/// <summary>
///
/// </summary>
/// <param name="databaseWidgetManager"></param>
/// <param name="baseUnitOfWork"></param>
/// <param name="contract"></param>
/// <returns></returns>
Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.Database.Interfaces;
/// <summary>
///
/// </summary>
public interface IDatabaseWidgetManager : IWidgetManager
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="baseUnitOfWork"></param>
/// <param name="contract"></param>
/// <returns></returns>
Task Add<T>(IBaseUnitOfWork baseUnitOfWork, T contract);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public interface IContractWritableLogic<TEntity, TCreateContract, TContract, TUp
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[Obsolete]
Task<MessageContract<TEntity>> AddEntity(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ private async Task<ListMessageContract<TEntity>> InternalUpdateBulk<TEntity>(IEa
{
if (uniqueIdentityManager.UpdateUniqueIdentity(currentUserUniqueIdentity, easyWritableQueryable.Context, entityEntry.Entity))
{
FixUniqueIdentity(easyWritableQueryable.Context,typeof(TEntity), [entityEntry]);
FixUniqueIdentity(easyWritableQueryable.Context, typeof(TEntity), [entityEntry]);
}
}
}
Expand Down Expand Up @@ -938,11 +938,13 @@ void FixUniqueIdentity(IContext context, Type entityType, IEntityEntry[] entityE
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TContract"></typeparam>
/// <param name="easyWritableQueryable"></param>
/// <param name="entity"></param>
/// <param name="contract"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<MessageContract<TEntity>> Add<TEntity>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, TEntity entity, CancellationToken cancellationToken = default)
internal async Task<MessageContract<TEntity>> Add<TEntity, TContract>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, TEntity entity, TContract contract, CancellationToken cancellationToken = default)
where TEntity : class
{
var result = await easyWritableQueryable.AddAsync(entity, cancellationToken);
Expand Down Expand Up @@ -971,6 +973,8 @@ await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, tr
await easyWritableQueryable.SaveChangesAsync();
}
}
var widgetManager = _baseUnitOfWork.GetDatabaseWidgetManager();
await widgetManager.Add(_baseUnitOfWork, contract);
await ActivityChangeLogLogic.AddAsync(result.Entity, _baseUnitOfWork);
return result.Entity;
}
Expand Down Expand Up @@ -1038,7 +1042,7 @@ public async Task<MessageContract<TEntity>> Add<TEntity, TContract>(IEasyWritabl
where TEntity : class
{
var entity = await MapAsync<TEntity, TContract>(contract);
var result = await Add<TEntity>(easyWritableQueryable, entity, cancellationToken);
var result = await Add<TEntity, TContract>(easyWritableQueryable, entity, contract, cancellationToken);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ public async Task<ListMessageContract<TResponseContract>> AddBulk(CreateBulkRequ
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
[Obsolete]
public Task<MessageContract<TEntity>> AddEntity(TEntity entity, CancellationToken cancellationToken = default)
{
return Add<TEntity>(_easyWriteableQueryable, entity, cancellationToken);
return Add<TEntity, TEntity>(_easyWriteableQueryable, entity, entity, cancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ public async Task<ListMessageContract<TId>> AddBulk(CreateBulkRequestContract<TC
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
[Obsolete]
public Task<MessageContract<TEntity>> AddEntity(TEntity entity, CancellationToken cancellationToken = default)
{
return Add<TEntity>(_easyWriteableQueryable, entity, cancellationToken);
return Add<TEntity, TEntity>(_easyWriteableQueryable, entity, entity, cancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Cores.Widgets;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.Database.Managers;

/// <summary>
///
/// </summary>
public class DatabaseWidgetManager : WidgetManager, IDatabaseWidgetManager
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="baseUnitOfWork"></param>
/// <param name="contract"></param>
/// <returns></returns>
public async Task Add<T>(IBaseUnitOfWork baseUnitOfWork, T contract)
{
var widgets = GetWidgetsByType(typeof(T));
foreach (var widget in widgets)
{
if (widget is IDatabaseWidget<T> databaseWidget && databaseWidget.CanProcess(baseUnitOfWork))
{
await databaseWidget.Process(this, baseUnitOfWork, contract);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using EasyMicroservices.Cores.Interfaces;
using System;
using System.Reflection.Emit;

namespace EasyMicroservices.Cores.Database.Widgets;
/// <summary>
///
/// </summary>
public abstract class DatabaseBuilderWidget<TModelBuilder> : IWidget
{
/// <summary>
///
/// </summary>
public void Build()
{

}

/// <summary>
///
/// </summary>
/// <param name="modelBuilder"></param>
/// <param name="suffix"></param>
/// <param name="prefix"></param>
public abstract void OnModelCreating(TModelBuilder modelBuilder, string suffix = "", string prefix = "");
/// <summary>
///
/// </summary>
/// <returns></returns>
public Type GetObjectType()
{
return typeof(DatabaseBuilderWidget<TModelBuilder>);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using EasyMicroservices.Cores.Interfaces;
using System;

namespace EasyMicroservices.Cores.Database.Widgets;
/// <summary>
///
/// </summary>
public abstract class ReportingBuilderWidget : IWidget
{
/// <summary>
///
/// </summary>
public abstract void Build();
/// <summary>
///
/// </summary>
/// <returns></returns>
public Type GetObjectType()
{
return typeof(ReportingBuilderWidget);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.ServiceContracts;
using System;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.Widgets;

/// <summary>
///
/// </summary>
/// <typeparam name="TReportEntity"></typeparam>
/// <typeparam name="TObjectContract"></typeparam>
public class SimpleReportingEntityWidget<TReportEntity, TObjectContract> : IDatabaseWidget<TObjectContract>
where TReportEntity : class
where TObjectContract : class
{
/// <summary>
///
/// </summary>
public void Build()
{

}

/// <summary>
///
/// </summary>
public bool CanProcess(IBaseUnitOfWork baseUnitOfWork)
{
if (baseUnitOfWork.LogicOptions.HasValue)
return !baseUnitOfWork.LogicOptions.Value.DoStopReporting;
return true;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public Type GetObjectType()
{
return typeof(TObjectContract);
}

/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public Task Initialize(params TObjectContract[] parameters)
{
return TaskHelper.GetCompletedTask();
}

/// <summary>
///
/// </summary>
/// <param name="databaseWidgetManager"></param>
/// <param name="baseUnitOfWork"></param>
/// <param name="contract"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract)
{
var reportEntity = await baseUnitOfWork
.GetMapper()
.MapAsync<TReportEntity>(contract);

await baseUnitOfWork.GetLogic<TReportEntity>(new Models.LogicOptions()
{
DoStopReporting = true
})
.Add(reportEntity)
.AsCheckedResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<ProjectReference Include="..\EasyMicroservices.Cores.Contracts\EasyMicroservices.Cores.Contracts.csproj" />
<ProjectReference Include="..\EasyMicroservices.Cores.Infrastructure\EasyMicroservices.Cores.Infrastructure.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 81e297e

Please sign in to comment.