diff --git a/Projects/DLL/System.Configuration.ConfigurationManager.dll b/Projects/DLL/System.Configuration.ConfigurationManager.dll index 7d0b114..14f8ef6 100644 Binary files a/Projects/DLL/System.Configuration.ConfigurationManager.dll and b/Projects/DLL/System.Configuration.ConfigurationManager.dll differ diff --git a/Projects/DLL/SystemX.Core.dll b/Projects/DLL/SystemX.Core.dll index d8175ae..f540533 100644 Binary files a/Projects/DLL/SystemX.Core.dll and b/Projects/DLL/SystemX.Core.dll differ diff --git a/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac b/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac index f2df814..6243de0 100644 Binary files a/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac and b/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac differ diff --git a/Projects/HubX/HubX.Library.DB/HubX.Library.DB.csproj b/Projects/HubX/HubX.Library.DB/HubX.Library.DB.csproj index 7ff51e6..a15a195 100644 --- a/Projects/HubX/HubX.Library.DB/HubX.Library.DB.csproj +++ b/Projects/HubX/HubX.Library.DB/HubX.Library.DB.csproj @@ -11,7 +11,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Projects/HubX/HubX.Library/DataBase/DBManager.cs b/Projects/HubX/HubX.Library/DataBase/DBManager.cs deleted file mode 100644 index 6696e6f..0000000 --- a/Projects/HubX/HubX.Library/DataBase/DBManager.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Azure.Core; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HubX.Library.DataBase -{ - public class DBManager - { - public DBManager() - { - } - - public async Task Insert(T context) where T : DbContext - { - //using (var transaction = await context.CreateTransactionAsync()) - //{ - // if (context.TCertificates.ToList().Exists(x => x.CCuid == tbscsr.CCuid) == false) - // { - // await context.AddAsync(certificate); - // } - // transactionResult = await context.CloseTransactionAsync(transaction); - //} - } - } -} diff --git a/Projects/HubX/HubX.Library/Http/Packet/Packet.cs b/Projects/HubX/HubX.Library/Http/Packet/Packet.cs new file mode 100644 index 0000000..8d271e5 --- /dev/null +++ b/Projects/HubX/HubX.Library/Http/Packet/Packet.cs @@ -0,0 +1,25 @@ +using DB.HubXDB; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HubX.Library.Http.Packet +{ + public class Request_InsertUniqueKey + { + public string Identity { get; set; } + + public string? Data1 { get; set; } + public string? Data2 { get; set; } + public string? Data3 { get; set; } + public string? Data4 { get; set; } + public string? Data5 { get; set; } + } + + public class Response_InsertUniqueKy + { + + } +} diff --git a/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs b/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs index 2a41316..e9370cd 100644 --- a/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs +++ b/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs @@ -1,4 +1,6 @@ -using HubX.Library.Socket.Object; +using DB.HubXDB; +using HubX.Library.Http.Packet; +using HubX.Library.Socket.Object; using HubX.Library.Socket.Session; using System; using System.Collections.Generic; @@ -37,7 +39,16 @@ namespace HubX.Library.Socket.Packet //insert DB if(jsonObject != null) { - + Request_InsertUniqueKey request = new Request_InsertUniqueKey(); + request.Identity = jsonObject.Identity; + request.Data1 = jsonObject.Data1; + request.Data2 = jsonObject.Data2; + request.Data3 = jsonObject.Data3; + request.Data4 = jsonObject.Data4; + request.Data5 = jsonObject.Data5; + + SystemX.Core.Communication.Http http = new (); + var res = http.PostJsonAsync("https://127.0.0.1:9000/UniqueKey/InsertUniqueKey", request); } ClientSession clientSession = session as ClientSession; diff --git a/Projects/HubX/HubX.Server/Controllers/UniqueKeyController.cs b/Projects/HubX/HubX.Server/Controllers/UniqueKeyController.cs new file mode 100644 index 0000000..9cd0cec --- /dev/null +++ b/Projects/HubX/HubX.Server/Controllers/UniqueKeyController.cs @@ -0,0 +1,27 @@ +using HubX.Library.Http.Packet; +using HubX.Server.Services; +using Microsoft.AspNetCore.Mvc; + +namespace HubX.Server.Controllers +{ + [ApiController] + [Route("[controller]/[action]")] + + public class UniqueKeyController + { + private readonly UniqueKeyService _uniqueKeyService; + + public UniqueKeyController(UniqueKeyService uniqueKeyService) + { + _uniqueKeyService = uniqueKeyService; + } + + [HttpPost] + public async Task InsertUniqueKey(Request_InsertUniqueKey request) + { + Response_InsertUniqueKy res = await _uniqueKeyService.Request_InsertUniqueKey(request); + + return Results.Ok(res); + } + } +} diff --git a/Projects/HubX/HubX.Server/Controllers/WeatherForecastController.cs b/Projects/HubX/HubX.Server/Controllers/WeatherForecastController.cs deleted file mode 100644 index ca7fa53..0000000 --- a/Projects/HubX/HubX.Server/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace HubX.Server.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} diff --git a/Projects/HubX/HubX.Server/Program.cs b/Projects/HubX/HubX.Server/Program.cs index 0654f85..bd3bbe9 100644 --- a/Projects/HubX/HubX.Server/Program.cs +++ b/Projects/HubX/HubX.Server/Program.cs @@ -1,6 +1,7 @@ using HubX.Library.Config; using HubX.Library.Socket.Session; using HubX.Server; +using HubX.Server.Services; using HubX.Server.TaskManager; using System.Net; using System.Net.Sockets; @@ -27,10 +28,15 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddHttpContextAccessor(); //singleton builder.Services.AddSingleton>(); +//scoped +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + //config preload ConfigService preloadConfig = new ConfigService(); if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) @@ -84,8 +90,8 @@ if (app.Environment.IsDevelopment()) TaskSocket taskSocket = new TaskSocket(); taskSocket?.Run(socketPort); +app.UseAuthentication(); app.UseHttpsRedirection(); - app.UseAuthorization(); app.MapControllers(); diff --git a/Projects/HubX/HubX.Server/Services/EFCoreService.cs b/Projects/HubX/HubX.Server/Services/EFCoreService.cs new file mode 100644 index 0000000..2786753 --- /dev/null +++ b/Projects/HubX/HubX.Server/Services/EFCoreService.cs @@ -0,0 +1,62 @@ +using DB.HubXDB; +using HubX.Library.Config; +using Microsoft.EntityFrameworkCore; +using SystemX.Core.Config.Model; +using SystemX.Core.DB; +using SystemX.Core.Services; + +namespace HubX.Server.Services +{ + public class EFCoreService + { + private readonly Dictionary DicDbContext = new Dictionary(); + private readonly ConfigService _configService; + + public EFCoreService(ConfigService configService) + { + _configService = configService; + InitializeDB(); + } + + #region Initialize DBContext + private void InitializeDB() + { + var dbList = _configService.GetConfig()?.DataBase; + if (dbList is not null) + { + foreach (var db in dbList) + { + if (typeof(HubXContext).Name == db.DBContext) + { + CreateDBContext(db); + } + } + } + } + + private void CreateDBContext(DataBase? dbConfig) where TDBContext : DbContext, new() + { + var connectionString = dbConfig?.ConvertToConnectionString(); + + var dbContext = new TDBContext(); + dbContext.Database.SetConnectionString($"{connectionString}"); + + if (dbContext is not null) + DicDbContext.Add($"{dbConfig?.DBContext}_{dbConfig?.DBID}", dbContext); + } + #endregion + + public TDBContext? GetDBContext(int dbID = 1) where TDBContext : DbContext + { + TDBContext? dBContext = default; + + var dbContextType = typeof(TDBContext); + if (DicDbContext.TryGetValue($"{dbContextType.Name}_{dbID}", out var context) == true) + { + dBContext = context as TDBContext; + } + + return dBContext; + } + }//class end +} diff --git a/Projects/HubX/HubX.Server/Services/UniqueKeyService.cs b/Projects/HubX/HubX.Server/Services/UniqueKeyService.cs new file mode 100644 index 0000000..a7bddf5 --- /dev/null +++ b/Projects/HubX/HubX.Server/Services/UniqueKeyService.cs @@ -0,0 +1,55 @@ +using DB.HubXDB; +using HubX.Library.Http.Packet; +using SystemX.Core.DB; + +namespace HubX.Server.Services +{ + public class UniqueKeyService + { + private readonly EFCoreService _efCoreService; + + public UniqueKeyService(EFCoreService efCoreService) + { + _efCoreService = efCoreService; + } + + public async Task Request_InsertUniqueKey(Request_InsertUniqueKey request) + { + Response_InsertUniqueKy response = new Response_InsertUniqueKy(); + + var storage = new TStorage + { + CIdentity = request.Identity, + CData1 = request.Data1, + CData2 = request.Data2, + CData3 = request.Data3, + CData4 = request.Data4, + CData5 = request.Data5, + + CDateTime = DateTime.Now + }; + + + bool transactionResult = true; + bool isExist = false; + var context = _efCoreService.GetDBContext(); + + if (context != null) + { + using (var transaction = await context.CreateTransactionAsync()) + { + await context.AddAsync(storage); + transactionResult = await context.CloseTransactionAsync(transaction); + } + } + + //db error + if (transactionResult == false) + { + + } + + return response; + } + } +} diff --git a/Projects/HubX/HubX.Server/WeatherForecast.cs b/Projects/HubX/HubX.Server/WeatherForecast.cs deleted file mode 100644 index d5ec83b..0000000 --- a/Projects/HubX/HubX.Server/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace HubX.Server -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -}