From 870cf85e3f25bf10c50e271dfd9934941ef9dbb1 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 17 Mar 2026 15:23:05 +0100 Subject: [PATCH] Migrazione gestione risorse in obj separati (Repository/Service) --- .../Controllers/LuxController.cs | 99 ------------------- .../Repository/Cost/IResourceRepository.cs | 21 ++++ .../Repository/Cost/ResourceRepository.cs | 68 +++++++++++++ .../Services/Cost/IResourceService.cs | 17 ++++ .../Services/Cost/ResourceService.cs | 94 ++++++++++++++++++ .../Services/DataLayerServices.cs | 64 ------------ Lux.API/Lux.API.csproj | 2 +- Lux.API/Program.cs | 4 + .../Compo/JobTask/ResourcesMan.razor.cs | 14 +-- Lux.UI/Components/Pages/JobRoute.razor.cs | 16 +-- Lux.UI/Lux.UI.csproj | 2 +- Lux.UI/Program.cs | 4 + Resources/ChangeLog.html | 2 +- Resources/VersNum.txt | 2 +- Resources/manifest.xml | 2 +- 15 files changed, 231 insertions(+), 180 deletions(-) create mode 100644 EgwCoreLib.Lux.Data/Repository/Cost/IResourceRepository.cs create mode 100644 EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs create mode 100644 EgwCoreLib.Lux.Data/Services/Cost/IResourceService.cs create mode 100644 EgwCoreLib.Lux.Data/Services/Cost/ResourceService.cs diff --git a/EgwCoreLib.Lux.Data/Controllers/LuxController.cs b/EgwCoreLib.Lux.Data/Controllers/LuxController.cs index f7b45bb..373a451 100644 --- a/EgwCoreLib.Lux.Data/Controllers/LuxController.cs +++ b/EgwCoreLib.Lux.Data/Controllers/LuxController.cs @@ -2674,105 +2674,6 @@ namespace EgwCoreLib.Lux.Data.Controllers return dbRestults; } - /// - /// Eliminazione record - /// - /// - /// - internal async Task ResourcesDeleteAsync(ResourceModel rec2del) - { - bool answ = false; - //using (DataLayerContext dbCtx = new DataLayerContext(_config)) - using (DataLayerContext dbCtx = new DataLayerContext()) - { - try - { - var currRec = dbCtx - .DbSetResource - .Where(x => rec2del.ResourceID > 0 && x.ResourceID == rec2del.ResourceID) - .FirstOrDefault(); - // se trovato --> elimino - if (currRec != null) - { - dbCtx.DbSetResource.Remove(rec2del); - } - // salvo... - int numAct = await dbCtx.SaveChangesAsync(); - answ = numAct > 0; - } - catch (Exception exc) - { - Log.Error($"Eccezione durante ResourcesDeleteAsync{Environment.NewLine}{exc}"); - } - } - return answ; - } - - /// - /// Elenco record risorse da DB - /// - /// - internal async Task> ResourcesGetAllAsync() - { - List dbResult = new List(); - //using (DataLayerContext dbCtx = new DataLayerContext(_config)) - using (DataLayerContext dbCtx = new DataLayerContext()) - { - try - { - dbResult = await dbCtx - .DbSetResource - .Include(d => d.DriverNav) - .Include(j => j.JobStepNav) - .ToListAsync(); - } - catch (Exception exc) - { - Log.Error($"Eccezione durante ResourcesGetAllAsync{Environment.NewLine}{exc}"); - } - } - return dbResult; - } - - /// - /// Add record - /// - /// - /// - internal async Task ResourcesUpsertAsync(ResourceModel upsRec) - { - bool answ = false; - //using (DataLayerContext dbCtx = new DataLayerContext(_config)) - using (DataLayerContext dbCtx = new DataLayerContext()) - { - try - { - var currRec = dbCtx - .DbSetResource - .Where(x => upsRec.ResourceID > 0 && x.ResourceID == upsRec.ResourceID) - .FirstOrDefault(); - // se trovato --> aggiorno - if (currRec != null) - { - dbCtx.Entry(currRec).CurrentValues.SetValues(upsRec); - } - // se mancasse --> aggiungo - else - { - dbCtx.DbSetResource.Add(upsRec); - } - // salvo... - int numAct = await dbCtx.SaveChangesAsync(); - answ = numAct > 0; - } - catch (Exception exc) - { - Log.Error($"Eccezione durante ResourcesUpsertAsync{Environment.NewLine}{exc}"); - } - } - return answ; - } - /// /// Esegue merge dei dati nella tab profili del DB con le info accessorie... /// diff --git a/EgwCoreLib.Lux.Data/Repository/Cost/IResourceRepository.cs b/EgwCoreLib.Lux.Data/Repository/Cost/IResourceRepository.cs new file mode 100644 index 0000000..f8321cf --- /dev/null +++ b/EgwCoreLib.Lux.Data/Repository/Cost/IResourceRepository.cs @@ -0,0 +1,21 @@ +using EgwCoreLib.Lux.Data.DbModel.Cost; + +namespace EgwCoreLib.Lux.Data.Repository.Cost +{ + public interface IResourceRepository : IBaseRepository + { + #region Public Methods + + Task AddAsync(ResourceModel entity); + + Task DeleteAsync(ResourceModel entity); + + Task> GetAllAsync(); + + Task GetByIdAsync(int recId); + + Task UpdateAsync(ResourceModel entity); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs b/EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs new file mode 100644 index 0000000..04a01b6 --- /dev/null +++ b/EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs @@ -0,0 +1,68 @@ +using EgwCoreLib.Lux.Data.DbModel.Cost; +using Microsoft.EntityFrameworkCore; + +namespace EgwCoreLib.Lux.Data.Repository.Cost +{ + public class ResourceRepository : BaseRepository, IResourceRepository + { + #region Public Constructors + + public ResourceRepository(IDbContextFactory ctxFactory) : base(ctxFactory) + { + } + + #endregion Public Constructors + + #region Public Methods + + public async Task AddAsync(ResourceModel entity) + { + await using var dbCtx = await CreateContextAsync(); + await dbCtx.DbSetResource.AddAsync(entity); + return await dbCtx.SaveChangesAsync() > 0; + } + + public async Task DeleteAsync(ResourceModel entity) + { + await using var dbCtx = await CreateContextAsync(); + dbCtx.DbSetResource.Remove(entity); + return await dbCtx.SaveChangesAsync() > 0; + } + + public async Task> GetAllAsync() + { + await using var dbCtx = await CreateContextAsync(); + return await dbCtx.DbSetResource + .Include(d => d.DriverNav) + .Include(j => j.JobStepNav) + .AsNoTracking() + .ToListAsync(); + } + + public async Task GetByIdAsync(int recId) + { + await using var dbCtx = await CreateContextAsync(); + return await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == recId); + } + + public async Task UpdateAsync(ResourceModel entity) + { + await using var dbCtx = await CreateContextAsync(); + // Recuperiamo l'entità tracciata dal context + var trackedEntity = dbCtx.DbSetResource.Local.FirstOrDefault(x => x.ResourceID == entity.ResourceID); + + if (trackedEntity != null) + { + // Aggiorna i valori dell'entità tracciata con quelli della nuova + dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity); + } + else + { + dbCtx.DbSetResource.Update(entity); + } + return await dbCtx.SaveChangesAsync() > 0; + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/EgwCoreLib.Lux.Data/Services/Cost/IResourceService.cs b/EgwCoreLib.Lux.Data/Services/Cost/IResourceService.cs new file mode 100644 index 0000000..7b8bc2f --- /dev/null +++ b/EgwCoreLib.Lux.Data/Services/Cost/IResourceService.cs @@ -0,0 +1,17 @@ +using EgwCoreLib.Lux.Data.DbModel.Cost; + +namespace EgwCoreLib.Lux.Data.Services.Cost +{ + public interface IResourceService + { + #region Public Methods + + Task DeleteAsync(ResourceModel entity); + + Task> GetAllAsync(); + + Task UpsertAsync(ResourceModel upsRec); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/EgwCoreLib.Lux.Data/Services/Cost/ResourceService.cs b/EgwCoreLib.Lux.Data/Services/Cost/ResourceService.cs new file mode 100644 index 0000000..d1e90b8 --- /dev/null +++ b/EgwCoreLib.Lux.Data/Services/Cost/ResourceService.cs @@ -0,0 +1,94 @@ +using EgwCoreLib.Lux.Data.DbModel.Cost; +using EgwCoreLib.Lux.Data.Repository.Cost; +using Microsoft.Extensions.Configuration; +using StackExchange.Redis; + +namespace EgwCoreLib.Lux.Data.Services.Cost +{ + public class ResourceService : BaseServ, IResourceService + { + #region Public Constructors + + public ResourceService( + IConfiguration config, + IConnectionMultiplexer redis, + IResourceRepository repo) : base(config, redis) + { + _className = "Resource"; + _repo = repo; + } + + #endregion Public Constructors + + #region Public Methods + + public async Task DeleteAsync(ResourceModel rec2del) + { + return await TraceAsync($"{_className}.Delete", async (activity) => + { + var dbResult = await _repo.GetByIdAsync(rec2del.ResourceID); + if (dbResult == null) return false; + + bool success = await _repo.DeleteAsync(dbResult); + + if (success) + { + await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); + } + + return success; + }); + } + + public async Task> GetAllAsync() + { + return await TraceAsync($"{_className}.GetAll", async (activity) => + { + return await GetOrSetCacheAsync( + $"{_redisBaseKey}:{_className}:ALL", + async () => await _repo.GetAllAsync(), + UltraLongCache + ); + }); + } + + + public async Task UpsertAsync(ResourceModel upsRec) + { + return await TraceAsync($"{_className}.Upsert", async (activity) => + { + var currRec = await _repo.GetByIdAsync(upsRec.ResourceID); + + string operation = "UPDATE"; + bool success = false; + if (currRec != null) + { + success = await _repo.UpdateAsync(upsRec); + } + else + { + operation = "INSERT"; + success = await _repo.AddAsync(upsRec); + } + + activity?.SetTag("db.operation", operation); + + if (success) + { + await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); + } + + return success; + }); + } + + #endregion Public Methods + + #region Private Fields + + private readonly string _className; + private readonly IResourceRepository _repo; + + #endregion Private Fields + } +} diff --git a/EgwCoreLib.Lux.Data/Services/DataLayerServices.cs b/EgwCoreLib.Lux.Data/Services/DataLayerServices.cs index 99f222d..af9a531 100644 --- a/EgwCoreLib.Lux.Data/Services/DataLayerServices.cs +++ b/EgwCoreLib.Lux.Data/Services/DataLayerServices.cs @@ -1615,70 +1615,6 @@ namespace EgwCoreLib.Lux.Data.Services return dbResult; } - /// Esegue Delete del record ricevuto - /// - /// - /// - public async Task ResourcesDeleteAsync(ResourceModel upsRec) - { - using var activity = StartActivity(); - string source = "DB+REDIS"; - bool result = await dbController.ResourcesDeleteAsync(upsRec); - await ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:Resources:*"); - activity?.SetTag("data.source", source); - LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms"); - return result; - } - - /// - /// Elenco completo Risorse - /// - /// - public async Task> ResourcesGetAllAsync() - { - using var activity = StartActivity(); - string source = "DB"; - List? result = new List(); - // cerco in redis... - string currKey = $"{redisBaseKey}:Resources:ALL"; - RedisValue rawData = await _redisDb.StringGetAsync(currKey); - if (rawData.HasValue) - { - result = JsonConvert.DeserializeObject>($"{rawData}"); - source = "REDIS"; - } - else - { - result = await dbController.ResourcesGetAllAsync(); - // serializzo e salvo con config x evitare loop... - rawData = JsonConvert.SerializeObject(result, JSSettings); - await _redisDb.StringSetAsync(currKey, rawData, LongCache); - } - if (result == null) - { - result = new List(); - } - activity?.SetTag("data.source", source); - LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms"); - return result; - } - - /// - /// Esegue Add del record ricevuto - /// - /// - /// - public async Task ResourcesUpsertAsync(ResourceModel upsRec) - { - using var activity = StartActivity(); - string source = "DB+REDIS"; - bool result = await dbController.ResourcesUpsertAsync(upsRec); - await ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:Resources:*"); - activity?.SetTag("data.source", source); - LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms"); - return result; - } - /// /// Esegue salvataggio BOM sul DB /// diff --git a/Lux.API/Lux.API.csproj b/Lux.API/Lux.API.csproj index 2f14e28..0a3d51e 100644 --- a/Lux.API/Lux.API.csproj +++ b/Lux.API/Lux.API.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 1.1.2603.1714 + 1.1.2603.1715 diff --git a/Lux.API/Program.cs b/Lux.API/Program.cs index 7d6e282..805d418 100644 --- a/Lux.API/Program.cs +++ b/Lux.API/Program.cs @@ -1,10 +1,12 @@ using EgwCoreLib.Lux.Data; using EgwCoreLib.Lux.Data.Repository.Config; +using EgwCoreLib.Lux.Data.Repository.Cost; using EgwCoreLib.Lux.Data.Repository.Items; using EgwCoreLib.Lux.Data.Repository.Job; using EgwCoreLib.Lux.Data.Repository.Utils; using EgwCoreLib.Lux.Data.Services; using EgwCoreLib.Lux.Data.Services.Config; +using EgwCoreLib.Lux.Data.Services.Cost; using EgwCoreLib.Lux.Data.Services.Items; using EgwCoreLib.Lux.Data.Services.Job; using EgwCoreLib.Lux.Data.Services.Utils; @@ -178,6 +180,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -192,6 +195,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Lux.UI/Components/Compo/JobTask/ResourcesMan.razor.cs b/Lux.UI/Components/Compo/JobTask/ResourcesMan.razor.cs index a44c3ce..cdcbdb6 100644 --- a/Lux.UI/Components/Compo/JobTask/ResourcesMan.razor.cs +++ b/Lux.UI/Components/Compo/JobTask/ResourcesMan.razor.cs @@ -1,8 +1,7 @@ using EgwCoreLib.Lux.Data.DbModel.Cost; -using EgwCoreLib.Lux.Data.DbModel.Job; using EgwCoreLib.Lux.Data.Services; +using EgwCoreLib.Lux.Data.Services.Cost; using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.DataProtection; using Microsoft.JSInterop; namespace Lux.UI.Components.Compo.JobTask @@ -30,6 +29,9 @@ namespace Lux.UI.Components.Compo.JobTask [Inject] protected DataLayerServices DLService { get; set; } = null!; + [Inject] + protected IResourceService ResService { get; set; } = null!; + #endregion Protected Properties #region Protected Methods @@ -63,7 +65,7 @@ namespace Lux.UI.Components.Compo.JobTask isLoading = true; // elimino e ricarico... - await DLService.ResourcesDeleteAsync(rec2del); + await ResService.DeleteAsync(rec2del); await ReloadBaseData(); ReloadData(); } @@ -92,7 +94,7 @@ namespace Lux.UI.Components.Compo.JobTask CostDriverID = 1 }; - await DLService.ResourcesUpsertAsync(newRecord); + await ResService.UpsertAsync(newRecord); AllRecords = new List(); await Task.Delay(100); await ReloadBaseData(); @@ -156,7 +158,7 @@ namespace Lux.UI.Components.Compo.JobTask // se non nullo salvo! if (updRec != null) { - await DLService.ResourcesUpsertAsync(updRec); + await ResService.UpsertAsync(updRec); } await ReloadBaseData(); ReloadData(); @@ -165,7 +167,7 @@ namespace Lux.UI.Components.Compo.JobTask private async Task ReloadBaseData() { - AllRecords = await DLService.ResourcesGetAllAsync(); + AllRecords = await ResService.GetAllAsync(); ListCostDriver = await DLService.CostDriverGetAllAsync(); } diff --git a/Lux.UI/Components/Pages/JobRoute.razor.cs b/Lux.UI/Components/Pages/JobRoute.razor.cs index edf6f2f..414ac42 100644 --- a/Lux.UI/Components/Pages/JobRoute.razor.cs +++ b/Lux.UI/Components/Pages/JobRoute.razor.cs @@ -1,6 +1,7 @@ using EgwCoreLib.Lux.Data.DbModel.Cost; using EgwCoreLib.Lux.Data.DbModel.Job; using EgwCoreLib.Lux.Data.Services; +using EgwCoreLib.Lux.Data.Services.Cost; using EgwCoreLib.Lux.Data.Services.Job; using Microsoft.AspNetCore.Components; @@ -14,10 +15,13 @@ namespace Lux.UI.Components.Pages protected DataLayerServices DLService { get; set; } = null!; [Inject] - protected IJobStepService JSService { get; set; } = null!; + protected IJobStepService JStService { get; set; } = null!; [Inject] - protected IJobTaskService JTService { get; set; } = null!; + protected IJobTaskService JTaService { get; set; } = null!; + + [Inject] + protected IResourceService ResService { get; set; } = null!; #endregion Protected Properties @@ -74,8 +78,8 @@ namespace Lux.UI.Components.Pages ListTagsAvailable = rawTags.Select(x => x.CodTag).ToList(); ListCostDrivers = await DLService.CostDriverGetAllAsync(); ListPhases = await DLService.PhasesGetAllAsync(); - ListResources = await DLService.ResourcesGetAllAsync(); - ListJobTask = await JTService.GetAllAsync(); + ListResources = await ResService.GetAllAsync(); + ListJobTask = await JTaService.GetAllAsync(); ListStep = null; isLoading = false; } @@ -89,7 +93,7 @@ namespace Lux.UI.Components.Pages { if (selRecord != null) { - ListStep = await JSService.GetByParentAsync(selRecord.JobID); + ListStep = await JStService.GetByParentAsync(selRecord.JobID); } else { @@ -107,7 +111,7 @@ namespace Lux.UI.Components.Pages // ricarico dal DB if (selRec != null) { - ListStep = await JSService.GetByParentAsync(selRec.JobID); + ListStep = await JStService.GetByParentAsync(selRec.JobID); } else { diff --git a/Lux.UI/Lux.UI.csproj b/Lux.UI/Lux.UI.csproj index 8b7f1ce..82971e4 100644 --- a/Lux.UI/Lux.UI.csproj +++ b/Lux.UI/Lux.UI.csproj @@ -5,7 +5,7 @@ enable enable aspnet-Lux.UI-a758c101-a2f4-4e38-977d-1c4887dbbd50 - 1.1.2603.1714 + 1.1.2603.1715 diff --git a/Lux.UI/Program.cs b/Lux.UI/Program.cs index 0bed67b..cd5cfff 100644 --- a/Lux.UI/Program.cs +++ b/Lux.UI/Program.cs @@ -1,10 +1,12 @@ using EgwCoreLib.Lux.Data; using EgwCoreLib.Lux.Data.Repository.Config; +using EgwCoreLib.Lux.Data.Repository.Cost; using EgwCoreLib.Lux.Data.Repository.Items; using EgwCoreLib.Lux.Data.Repository.Job; using EgwCoreLib.Lux.Data.Repository.Utils; using EgwCoreLib.Lux.Data.Services; using EgwCoreLib.Lux.Data.Services.Config; +using EgwCoreLib.Lux.Data.Services.Cost; using EgwCoreLib.Lux.Data.Services.Items; using EgwCoreLib.Lux.Data.Services.Job; using EgwCoreLib.Lux.Data.Services.Utils; @@ -216,6 +218,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -230,6 +233,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Resources/ChangeLog.html b/Resources/ChangeLog.html index 048c181..b8581f9 100644 --- a/Resources/ChangeLog.html +++ b/Resources/ChangeLog.html @@ -1,6 +1,6 @@ LUX - Web Windows MES -

Versione: 1.1.2603.1714

+

Versione: 1.1.2603.1715


Note di rilascio:
  • diff --git a/Resources/VersNum.txt b/Resources/VersNum.txt index f6a1306..9222caa 100644 --- a/Resources/VersNum.txt +++ b/Resources/VersNum.txt @@ -1 +1 @@ -1.1.2603.1714 +1.1.2603.1715 diff --git a/Resources/manifest.xml b/Resources/manifest.xml index f2ca5d8..23f5bd5 100644 --- a/Resources/manifest.xml +++ b/Resources/manifest.xml @@ -1,6 +1,6 @@ - 1.1.2603.1714 + 1.1.2603.1715 http://nexus.steamware.net/repository/SWS/GPW/stable/GPW.UI.zip http://nexus.steamware.net/repository/SWS/GPW/stable/ChangeLog.html false