Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Widget, Reporting Widget only for add #157 #158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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.50</Version>
<Version>0.0.1.51</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.50</Version>
<Version>0.0.1.51</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 @@ -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 @@ -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.50</Version>
<Version>0.0.1.51</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.50</Version>
<Version>0.0.1.51</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 @@ -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,25 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Database.Interfaces;
using System;
using System.Linq;
using System.Reflection;

namespace EasyMicroservices.Cores.Database.Helpers;
/// <summary>
///
/// </summary>
internal static class DatabaseExtensions
{
public static void SetIdToRecordId<TEntity, TRecordEntity>(IContext context, TEntity entity, TRecordEntity recordEntity)
{
var idProperty = typeof(TRecordEntity)
.GetProperty(nameof(IRecordIdSchema<string>.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<T>?");

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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using EasyMicroservices.Cores.Interfaces;
using System.Threading.Tasks;

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

namespace EasyMicroservices.Cores.Database.Interfaces;
/// <summary>
///
/// </summary>
public interface IDatabaseWidgetManager : IWidgetManager
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TEntity"></typeparam>
/// <param name="baseUnitOfWork"></param>
/// <param name="contract"></param>
/// <param name="entity"></param>
/// <returns></returns>
Task Add<T, TEntity>(IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity)
where TEntity : class;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +19,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces
/// <typeparam name="TId"></typeparam>
public interface IReadableLogic<TResultSchema, TId>
{
/// <summary>
///
/// </summary>
/// <returns></returns>
IContext GetReadableContext();
/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EasyMicroservices.Cores.Database.Interfaces;
/// <summary>
///
/// </summary>
/// <typeparam name="TId"></typeparam>
public interface IRecordIdSchema<TId>
{
/// <summary>
///
/// </summary>
public TId RecordId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Contracts.Requests;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.ServiceContracts;
using System;
using System.Linq.Expressions;
Expand All @@ -12,6 +13,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces
/// </summary>
public interface IWritableLogic<TRequsetSchema, TResultSchema, TId>
{
/// <summary>
///
/// </summary>
/// <returns></returns>
IContext GetWritableContext();
/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -101,6 +107,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 @@ -257,5 +257,25 @@ public Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return _easyWriteableQueryable.SaveChangesAsync(cancellationToken);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public IContext GetReadableContext()
{
return _easyReadableQueryable.Context;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public IContext GetWritableContext()
{
return _easyWriteableQueryable.Context;
}
}
}
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, result.Entity);
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 Expand Up @@ -364,5 +365,23 @@ public Task<MessageContract> SoftDeleteBulkByIds(SoftDeleteBulkRequestContract<T
{
throw new Exception("SoftDeleteBulkByIds is not supported in DatabaseMappedLogicBase, you can use IdSchemaDatabaseMappedLogicBase or override this SoftDeleteBulkByIds method");
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public IContext GetReadableContext()
{
return _easyReadableQueryable.Context;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public IContext GetWritableContext()
{
return _easyWriteableQueryable.Context;
}
}
}
Loading
Loading