Skip to content

Commit

Permalink
Support for WhiteLabel Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Sep 9, 2023
1 parent 7a27070 commit 15fd6d8
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.Database.Managers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using WhiteLables.GeneratedServices;

namespace EasyMicroservices.Cores.AspCoreApi.Managers
{
internal class WhiteLabelManager
{
internal WhiteLabelManager(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

private readonly IServiceProvider _serviceProvider;
public static HttpClient HttpClient { get; set; } = new HttpClient();

string GetDefaultUniqueIdentity(ICollection<WhiteLabelContract> whiteLables, long? parentId)
{
var found = whiteLables.FirstOrDefault(x => x.ParentId == parentId);
if (found == null)
{
return "";
}
return $"{DefaultUniqueIdentityManager.GenerateUniqueIdentity(found.Id)}-{GetDefaultUniqueIdentity(whiteLables, found.Id)}".Trim('-');
}

public async Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes)
{
if (dbContextTypes.IsNullOrEmpty())
return;
var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, HttpClient);
var whiteLabels = await whiteLabelClient.GetAllAsync().ConfigureAwait(false);
UnitOfWork.DefaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels.Result, null);

var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, HttpClient);
var microservices = await microserviceClient.GetAllAsync().ConfigureAwait(false);
var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase));
if (foundMicroservice == null)
{
foundMicroservice = new WhiteLables.GeneratedServices.MicroserviceContract()
{
InstanceIndex = 1,
Name = microserviceName,
Description = "Automatically added"
};
var addMicroservice = await microserviceClient.AddAsync(foundMicroservice).ConfigureAwait(false);
foundMicroservice.Id = addMicroservice.Result;
}
UnitOfWork.MicroserviceId = foundMicroservice.Id;

var uniqueIdentityManager = new UnitOfWork(_serviceProvider).GetUniqueIdentityManager() as DefaultUniqueIdentityManager;

var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient);
var microserviceContextTables = await microserviceContextTableClient.GetAllAsync().ConfigureAwait(false);

HashSet<string> addedInWhitLabels = new HashSet<string>();
foreach (var contextTableContract in microserviceContextTables.Result)
{
uniqueIdentityManager.InitializeTables(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName, contextTableContract.ContextTableId);
addedInWhitLabels.Add(uniqueIdentityManager.GetContextTableName(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName));
}

foreach (var contextType in dbContextTypes)
{
var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, HttpClient);
var contextTables = await contextTableClient.GetAllAsync().ConfigureAwait(false);
using var insctanceOfContext = _serviceProvider.GetService(contextType) as DbContext;
string contextName = uniqueIdentityManager.GetContextName(contextType);
foreach (var entityType in insctanceOfContext.Model.GetEntityTypes())
{
string tableName = entityType.ServiceOnlyConstructorBinding.RuntimeType.Name;
var tableFullName = uniqueIdentityManager.GetContextTableName(foundMicroservice.Id, contextType.Name, tableName);
if (!addedInWhitLabels.Contains(tableFullName))
{
if (microserviceContextTables.Result.Any(x => x.ContextName == contextName && x.TableName == tableName && x.MicroserviceId == foundMicroservice.Id))
continue;
var contextTable = contextTables.Result.FirstOrDefault(x => x.ContextName == contextName && x.TableName == tableName);
if (contextTable == null)
{
contextTable = new WhiteLables.GeneratedServices.ContextTableContract()
{
ContextName = contextName,
TableName = tableName,
};
var contextTableResult = await contextTableClient.AddAsync(contextTable).ConfigureAwait(false);
contextTable.Id = contextTableResult.Result;
}
var addedMicroserviceContextTable = await microserviceContextTableClient.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContextTableContract()
{
ContextName = contextName,
TableName = tableName,
MicroserviceName = microserviceName,
MicroserviceId = foundMicroservice.Id,
ContextTableId = contextTable.Id
}).ConfigureAwait(false);
uniqueIdentityManager.InitializeTables(foundMicroservice.Id, contextName, tableName, contextTable.Id);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Net;
using System.Net.Mime;
using System.Text.Json;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.AspEntityFrameworkCoreApi
{
Expand Down Expand Up @@ -90,7 +91,7 @@ public static void UseGlobalExceptionHandler(this IApplicationBuilder app)
/// <typeparam name="TContext"></typeparam>
/// <param name="app"></param>
/// <returns></returns>
public static void Build<TContext>(this IApplicationBuilder app)
public static async Task Build<TContext>(this IApplicationBuilder app)
where TContext : RelationalCoreContext
{
app.UseDeveloperExceptionPage();
Expand All @@ -107,6 +108,8 @@ public static void Build<TContext>(this IApplicationBuilder app)
var dbbuilder = new DatabaseCreator();
using var context = scope.ServiceProvider.GetRequiredService<TContext>();
dbbuilder.Initialize(context);
var uow = scope.ServiceProvider.GetRequiredService<IUnitOfWork>() as UnitOfWork;
await uow.Initialize("TextExample", "http://localhost:6041", typeof(TContext)).ConfigureAwait(false);
}
var build = app.Build();
app.Run(build);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.AspCoreApi.Managers;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Logics;
using EasyMicroservices.Cores.Database.Managers;
Expand Down Expand Up @@ -280,6 +281,18 @@ public IUniqueIdentityManager GetUniqueIdentityManager()
return UniqueIdentityManager;
}

/// <summary>
///
/// </summary>
/// <param name="microserviceName"></param>
/// <param name="whiteLableRoute"></param>
/// <param name="dbContextTypes"></param>
/// <returns></returns>
public Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes)
{
return new WhiteLabelManager(_service).Initialize(microserviceName, whiteLableRoute, dbContextTypes);
}

/// <summary>
///
/// </summary>
Expand Down

0 comments on commit 15fd6d8

Please sign in to comment.