From 4818342bce37f6fec735f49108fd2c52e8856787 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Fri, 8 Sep 2023 12:39:46 +0330 Subject: [PATCH 1/6] Add IrregularVerbs --- .../RelationalCoreContext.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs index 7cbada1..c2bb3f0 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs @@ -149,6 +149,8 @@ Type[] GetAllBases(Type type) string[] GetSimplifyPropertyName(string name) { + if (IrregularVerbs.TryGetValue(name, out string value)) + return new string[] { value }; string entity = "es"; if (name.EndsWith(entity)) return GetEndOfCollectionNames(name[..^entity.Length]); @@ -157,6 +159,39 @@ string[] GetSimplifyPropertyName(string name) return new string[] { name }; } + static Dictionary IrregularVerbs = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "Man" , "Men" }, + { "Child" , "Children" }, + { "Parent" , "Children" }, + { "Tooth" , "Teeth" }, + { "Foot" , "Feet" }, + { "Person" , "People" }, + { "Leaf" , "Leaves" }, + { "Mouse" , "Mice" }, + { "Goose" , "Geese" }, + { "Half" , "Halves" }, + { "Knife" , "Knives" }, + { "Wife" , "Wives" }, + { "Life" , "Lives" }, + { "Elf" , "Elves" }, + { "Loaf" , "Loaves" }, + { "Potato" , "Potatoes" }, + { "Tomato" , "Tomatoes" }, + { "Cactus" , "Cacti" }, + { "Focus" , "Foci" }, + { "Fungus" , "Fungi" }, + { "Nucleus" , "Nuclei" }, + { "Syllabus" , "Syllabuses" }, + { "Analysis" , "Analyses" }, + { "Diagnosis" , "Diagnoses" }, + { "Oasis" , "Oases" }, + { "Thesis" , "Theses" }, + { "Crisis" , "Crises" }, + { "Phenomenon" , "Phenomena" }, + { "Criterion" , "Criteria" }, + { "Datum" , "Data" }, + }; string[] GetEndOfCollectionNames(string name) { List names = new List From fe678a322b58130f36e8426f4dc7a87d94ebbb68 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Fri, 8 Sep 2023 12:54:35 +0330 Subject: [PATCH 2/6] add readonly --- .../RelationalCoreContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs index c2bb3f0..5348a61 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs @@ -159,7 +159,7 @@ string[] GetSimplifyPropertyName(string name) return new string[] { name }; } - static Dictionary IrregularVerbs = new Dictionary(StringComparer.OrdinalIgnoreCase) + static readonly Dictionary IrregularVerbs = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Man" , "Men" }, { "Child" , "Children" }, From 160df9f66e8e4967bb3adeebe98175b87b767347 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sat, 9 Sep 2023 16:58:03 +0330 Subject: [PATCH 3/6] Add null check exception to better development --- .../UnitOfWork.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs index 7d06bf5..45f9c37 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs @@ -51,7 +51,10 @@ T AddDisposable(T data) /// public IDatabase GetDatabase() { - return AddDisposable(new EntityFrameworkCoreDatabaseProvider(_service.GetService())); + var context = _service.GetService(); + if (context == null) + throw new Exception("RelationalCoreContext is null, please add your context to RelationalCoreContext as Transit or Scope.\r\nExample : services.AddTransient(serviceProvider => serviceProvider.GetService());"); + return AddDisposable(new EntityFrameworkCoreDatabaseProvider(context)); } /// @@ -223,7 +226,11 @@ public virtual IEasyReadableQueryableAsync GetReadableQueryable() where TContext : RelationalCoreContext { - return AddDisposable(new EntityFrameworkCoreDatabaseProvider(_service.GetService())); + var context = _service.GetService(); + if (context == null) + throw new Exception("TContext is null, please add your context to Context as Transit or Scope.\r\nExample : services.AddTransient(serviceProvider => serviceProvider.GetService());"); + + return AddDisposable(new EntityFrameworkCoreDatabaseProvider(context)); } /// From 71f0b01ef77e75123ac098339664314436ec2f6c Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sat, 9 Sep 2023 20:39:57 +0330 Subject: [PATCH 4/6] Add come laboratory template tests for white label client --- .../BasicTests.cs | 29 +++- .../Startup.cs | 10 +- .../EasyMicroservices.Cores.Tests.csproj | 2 + .../Laboratories/WhiteLabelLaboratoryTest.cs | 148 ++++++++++++++++++ 4 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Cores.Tests/Laboratories/WhiteLabelLaboratoryTest.cs diff --git a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/BasicTests.cs b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/BasicTests.cs index 5431230..33831e2 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/BasicTests.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/BasicTests.cs @@ -1,27 +1,31 @@ -using EasyMicroservices.ServiceContracts; +using EasyMicroservices.Cores.Database.Managers; +using EasyMicroservices.Cores.Tests.Contracts.Common; +using EasyMicroservices.Cores.Tests.Laboratories; +using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.TestHost; using Newtonsoft.Json; namespace EasyMicroservices.Cores.AspCore.Tests { - public class BasicTests + public class BasicTests : WhiteLabelLaboratoryTest { protected TestServer _testServer; public BasicTests() { var webBuilder = new WebHostBuilder(); webBuilder.UseStartup(); - + base.OnInitialize().Wait(); _testServer = new TestServer(webBuilder); } [Fact] - public async Task Get_EndpointsReturnSuccessAndCorrectContentType() + public async Task Get_EndpointsReturnSuccessAndCorrectContentType() { var client = _testServer.CreateClient(); var data = await client.GetStringAsync($"api/user/getall"); var result = JsonConvert.DeserializeObject(data); Assert.True(result); + return data; } [Fact] @@ -42,5 +46,22 @@ public async Task InternalErrorTest() Assert.False(result); Assert.Contains(result.Error.StackTrace, x => x.Contains("UserController.cs")); } + + [Fact] + public async Task AddUser() + { + var client = _testServer.CreateClient(); + var data = await client.PostAsJsonAsync($"api/user/Add", new UpdateUserContract() + { + UserName = "Ali", + UniqueIdentity = "1-2" + }); + var result = JsonConvert.DeserializeObject(await data.Content.ReadAsStringAsync()); + Assert.True(result); + var getAllRespone = await Get_EndpointsReturnSuccessAndCorrectContentType(); + var users = JsonConvert.DeserializeObject>(getAllRespone); + Assert.True(users); + Assert.True(users.Result.All(x => DefaultUniqueIdentityManager.DecodeUniqueIdentity(x.UniqueIdentity).Length > 2)); + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Startup.cs b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Startup.cs index dacfc41..59e4217 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Startup.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Startup.cs @@ -22,14 +22,6 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped((serviceProvider) => new UnitOfWork(serviceProvider).GetContractLogic()); services.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService())); services.AddScoped(serviceProvider => new DatabaseBuilder()); - //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - // .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, - // options => - // { - // options.LoginPath = new PathString("/auth/login"); - // options.AccessDeniedPath = new PathString("/auth/denied"); - // }); - //services.AddMvc().AddApplicationPart(typeof(UserController).Assembly).AddControllersAsServices(); } public void Configure(IApplicationBuilder app) @@ -41,7 +33,7 @@ public void Configure(IApplicationBuilder app) { endpoints.MapControllers(); }); - app.Build(); + app.Build().Wait(); } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Tests/EasyMicroservices.Cores.Tests.csproj b/src/CSharp/EasyMicroservices.Cores.Tests/EasyMicroservices.Cores.Tests.csproj index 7bbc04c..b60afa1 100644 --- a/src/CSharp/EasyMicroservices.Cores.Tests/EasyMicroservices.Cores.Tests.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Tests/EasyMicroservices.Cores.Tests.csproj @@ -9,6 +9,7 @@ + @@ -24,6 +25,7 @@ + diff --git a/src/CSharp/EasyMicroservices.Cores.Tests/Laboratories/WhiteLabelLaboratoryTest.cs b/src/CSharp/EasyMicroservices.Cores.Tests/Laboratories/WhiteLabelLaboratoryTest.cs new file mode 100644 index 0000000..65fda0f --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Tests/Laboratories/WhiteLabelLaboratoryTest.cs @@ -0,0 +1,148 @@ +using EasyMicroservices.Laboratory.Engine; +using EasyMicroservices.Laboratory.Engine.Net.Http; +using System.Text; + +namespace EasyMicroservices.Cores.Tests.Laboratories +{ + public class WhiteLabelLaboratoryTest + { + const int Port = 6041; + string _routeAddress = ""; + public static HttpClient HttpClient { get; set; } = new HttpClient(); + public WhiteLabelLaboratoryTest() + { + _routeAddress = $"http://localhost:{Port}"; + } + + static bool _isInitialized = false; + static SemaphoreSlim Semaphore = new SemaphoreSlim(1); + protected async Task OnInitialize() + { + if (_isInitialized) + return; + try + { + await Semaphore.WaitAsync(); + _isInitialized = true; + + ResourceManager resourceManager = new ResourceManager(); + HttpHandler httpHandler = new HttpHandler(resourceManager); + await httpHandler.Start(Port); + resourceManager.Append(@$"GET /api/WhiteLabel/GetAll HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody*" +, +@"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{""isSuccess"":true,""result"":[{""id"":1,""name"":""ProjectName""},{""id"":2,""name"":""TenantName"",""parentId"":1}]}"); + + + resourceManager.Append(@$"GET /api/Microservice/GetAll HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody*" +, +@"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{""isSuccess"":true,""result"":[{""id"":1,""name"":""TextExample"",""description"":""Automatically added""}]}"); + + + resourceManager.Append(@$"POST /api/Microservice/Add HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody*" +, +@"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{""isSuccess"":true,""result"": 1}"); + + resourceManager.Append(@$"GET /api/MicroserviceContextTable/GetAll HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody*" +, +@"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{""isSuccess"":true,""result"":[{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""AddressEntity"",""contextTableId"":1},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""CompanyEntity"",""contextTableId"":2},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""ProfileEntity"",""contextTableId"":3},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""UserCompanyEntity"",""contextTableId"":4},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""UserEntity"",""contextTableId"":5}]}"); + +resourceManager.Append(@$"GET /api/ContextTable/GetAll HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody*" +, +@"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{""isSuccess"":true,""result"":[{""id"":1,""tableName"":""AddressEntity"",""contextName"":""MyTestContext""},{""id"":2,""tableName"":""CompanyEntity"",""contextName"":""MyTestContext""},{""id"":3,""tableName"":""ProfileEntity"",""contextName"":""MyTestContext""},{""id"":4,""tableName"":""UserCompanyEntity"",""contextName"":""MyTestContext""},{""id"":5,""tableName"":""UserEntity"",""contextName"":""MyTestContext""}]}"); + + } + finally + { + Semaphore.Release(); + } + } + + [Fact] + public async Task WhiteLabelGetAllTest() + { + await OnInitialize(); + var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(_routeAddress, HttpClient); + var all = await whiteLabelClient.GetAllAsync(); + Assert.True(all.IsSuccess); + Assert.True(all.Result.Count == 2); + Assert.True(all.Result.All(x => x.Name.HasValue())); + } + + [Fact] + public async Task MicroserviceGetAllTest() + { + await OnInitialize(); + var client = new WhiteLables.GeneratedServices.MicroserviceClient(_routeAddress, HttpClient); + var all = await client.GetAllAsync(); + Assert.True(all.IsSuccess); + Assert.True(all.Result.Count == 1); + Assert.True(all.Result.All(x => x.Name == "TextExample")); + } + + [Fact] + public async Task MicroserviceAddTest() + { + await OnInitialize(); + var client = new WhiteLables.GeneratedServices.MicroserviceClient(_routeAddress, HttpClient); + var added = await client.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContract() + { + Name = "TextExample", + Description = "Automatically added" + }); + Assert.True(added.IsSuccess); + Assert.True(added.Result == 1); + } + + + [Fact] + public async Task MicroserviceContextTableGetAllTest() + { + await OnInitialize(); + var client = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(_routeAddress, HttpClient); + var all = await client.GetAllAsync(); + Assert.True(all.IsSuccess); + Assert.True(all.Result.Count >= 5); + Assert.True(all.Result.All(x=>x.MicroserviceName == "TextExample")); + } + + [Fact] + public async Task ContextTableGetAllTest() + { + await OnInitialize(); + var client = new WhiteLables.GeneratedServices.ContextTableClient(_routeAddress, HttpClient); + var all = await client.GetAllAsync(); + Assert.True(all.IsSuccess); + Assert.True(all.Result.Count >= 5); + } + } +} From 7a270709867f7bdcbe0b4216d59283ec254b15c6 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sat, 9 Sep 2023 20:41:11 +0330 Subject: [PATCH 5/6] add versions --- .../EasyMicroservices.Cores.AspCoreApi.csproj | 2 +- .../EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj | 5 +++-- .../EasyMicroservices.Cores.Database.csproj | 2 +- .../EasyMicroservices.Cores.EntityFrameworkCore.csproj | 2 +- ...Microservices.Cores.Relational.EntityFrameworkCore.csproj | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index 7067076..75c207a 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.26 + 0.0.0.27 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index 91bbff8..d911541 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.7 + 0.0.0.8 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore @@ -22,8 +22,9 @@ + - + diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 8477e02..3cb6d8d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.28 + 0.0.0.29 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index 3d29e40..fea2a67 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.19 + 0.0.0.20 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index c9e89d9..4af111d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.16 + 0.0.0.17 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational From 15fd6d85acb7f6b2a7b54c2fad50510ffd15569d Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sat, 9 Sep 2023 20:41:22 +0330 Subject: [PATCH 6/6] Support for WhiteLabel Manager --- .../Managers/WhiteLabelManager.cs | 109 ++++++++++++++++++ .../StartUpExtensions.cs | 5 +- .../UnitOfWork.cs | 15 ++- 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs new file mode 100644 index 0000000..80486e5 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs @@ -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 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 addedInWhitLabels = new HashSet(); + 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); + } + } + } + } + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs index 3ff3e00..3bb94bf 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs @@ -15,6 +15,7 @@ using System.Net; using System.Net.Mime; using System.Text.Json; +using System.Threading.Tasks; namespace EasyMicroservices.Cores.AspEntityFrameworkCoreApi { @@ -90,7 +91,7 @@ public static void UseGlobalExceptionHandler(this IApplicationBuilder app) /// /// /// - public static void Build(this IApplicationBuilder app) + public static async Task Build(this IApplicationBuilder app) where TContext : RelationalCoreContext { app.UseDeveloperExceptionPage(); @@ -107,6 +108,8 @@ public static void Build(this IApplicationBuilder app) var dbbuilder = new DatabaseCreator(); using var context = scope.ServiceProvider.GetRequiredService(); dbbuilder.Initialize(context); + var uow = scope.ServiceProvider.GetRequiredService() as UnitOfWork; + await uow.Initialize("TextExample", "http://localhost:6041", typeof(TContext)).ConfigureAwait(false); } var build = app.Build(); app.Run(build); diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs index 45f9c37..cce8e8c 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs @@ -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; @@ -280,6 +281,18 @@ public IUniqueIdentityManager GetUniqueIdentityManager() return UniqueIdentityManager; } + /// + /// + /// + /// + /// + /// + /// + public Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes) + { + return new WhiteLabelManager(_service).Initialize(microserviceName, whiteLableRoute, dbContextTypes); + } + /// /// ///