diff --git a/MP.Data/Repository/Anag/AnagRepository.cs b/MP.Data/Repository/Anag/AnagRepository.cs index 5b3f7eb5..33d884a5 100644 --- a/MP.Data/Repository/Anag/AnagRepository.cs +++ b/MP.Data/Repository/Anag/AnagRepository.cs @@ -322,7 +322,7 @@ namespace MP.Data.Repository.Anag } /// - public async Task ArticoliUpdateRecord(AnagArticoliModel editRec) + public async Task ArticoliUpsertAsync(AnagArticoliModel editRec) { await using var dbCtx = await CreateContextAsync(); var currRec = await dbCtx.DbSetArticoli.FirstOrDefaultAsync(x => x.CodArticolo == editRec.CodArticolo); @@ -333,9 +333,12 @@ namespace MP.Data.Repository.Anag currRec.Tipo = editRec.Tipo; currRec.Azienda = editRec.Azienda; dbCtx.Entry(currRec).State = EntityState.Modified; - return await dbCtx.SaveChangesAsync() > 0; } - return false; + else + { + dbCtx.DbSetArticoli.Add(editRec); + } + return await dbCtx.SaveChangesAsync() > 0; } /// diff --git a/MP.Data/Repository/Anag/IAnagRepository.cs b/MP.Data/Repository/Anag/IAnagRepository.cs index 728a4657..4028e102 100644 --- a/MP.Data/Repository/Anag/IAnagRepository.cs +++ b/MP.Data/Repository/Anag/IAnagRepository.cs @@ -140,11 +140,11 @@ namespace MP.Data.Repository.Anag Task> ArticoliInKitAsync(); /// - /// Update Record Articolo + /// Upsert (add/update) Record Articolo /// /// Record da aggiornare /// True se aggiornato - Task ArticoliUpdateRecord(AnagArticoliModel editRec); + Task ArticoliUpsertAsync(AnagArticoliModel editRec); /// /// Elenco Gruppi tipo REPARTOin formato DTO con conteggi del numero record trovati filtrati per operatore diff --git a/MP.SPEC/Data/MpDataService.cs b/MP.SPEC/Data/MpDataService.cs index c0ecbd5d..7380067b 100644 --- a/MP.SPEC/Data/MpDataService.cs +++ b/MP.SPEC/Data/MpDataService.cs @@ -364,15 +364,15 @@ namespace MP.SPEC.Data /// /// /// - public async Task ArticoliUpdateRecord(AnagArticoliModel currRec) + public async Task ArticoliUpsertRecord(AnagArticoliModel currRec) { - using var activity = ActivitySource.StartActivity("ArticoliUpdateRecord"); + using var activity = ActivitySource.StartActivity("ArticoliUpsertRecord"); string source = "DB"; - bool fatto = await _anagRepository.ArticoliUpdateRecord(currRec); + bool fatto = await _anagRepository.ArticoliUpsertAsync(currRec); await FlushFusionCacheArticoli(); activity?.SetTag("data.source", source); activity?.Stop(); - LogTrace($"ArticoliUpdateRecord | {source} | {activity?.Duration.TotalMilliseconds}ms"); + LogTrace($"ArticoliUpsertRecord | {source} | {activity?.Duration.TotalMilliseconds}ms"); return fatto; } diff --git a/MP.SPEC/Pages/Articoli.razor b/MP.SPEC/Pages/Articoli.razor index f50e61eb..52b21c07 100644 --- a/MP.SPEC/Pages/Articoli.razor +++ b/MP.SPEC/Pages/Articoli.razor @@ -158,7 +158,7 @@
- @if (ListRecords == null || isLoading) + @if (isLoading && false) { } @@ -176,7 +176,7 @@ @if (currRecord != null) { - + } Articolo diff --git a/MP.SPEC/Pages/Articoli.razor.cs b/MP.SPEC/Pages/Articoli.razor.cs index 2981dbed..601fff15 100644 --- a/MP.SPEC/Pages/Articoli.razor.cs +++ b/MP.SPEC/Pages/Articoli.razor.cs @@ -28,20 +28,88 @@ namespace MP.SPEC.Pages #endregion Public Methods - #region Protected Properties + #region Protected Methods + + protected override async Task OnInitializedAsync() + { + numRecord = 10; + await ReloadBaseData(); + } + + protected override async Task OnParametersSetAsync() + { + await ReloadDataAsync(); + } + + #endregion Protected Methods + + #region Private Fields + + private int _currPage = 1; + private int _numRecord = 10; + private string _selAzienda = "*"; + private int availRecord = 1000; + private SelectArticoliParams currFilter = new SelectArticoliParams(); + private AnagArticoliModel? currRecord = null; + private bool isLoading = false; + private bool isNewArt = false; + private List? ListAziende; + private List? ListRecords; + private List? ListTipoArt; + private int maxNumRecord = 1000; + private List? SearchRecords; + private string selTipoArt = "*"; + private int totalCount = 0; + + #endregion Private Fields + + #region Private Properties + + /// + /// Verifica cablata x add tutto tranne KIT + /// + private bool CanAdd => !selTipoArt.Equals("KIT"); + + private int currPage + { + get => _currPage; + set + { + if (_currPage != value) + { + _currPage = value; + UpdateTable(); + } + } + } [Inject] - protected IJSRuntime JSRuntime { get; set; } = null!; + private IJSRuntime JSRuntime { get; set; } = null!; [Inject] - protected MpDataService MDService { get; set; } = null!; + private MpDataService MDService { get; set; } = null!; [Inject] - protected NavigationManager NavManager { get; set; } = null!; + private NavigationManager NavManager { get; set; } = null!; + + private int numRecord + { + get => _numRecord; + set + { + if (_numRecord != value) + { + _numRecord = value; + UpdateTable(); + } + } + } private string searchCss => string.IsNullOrEmpty(searchVal) ? "btn-secondary" : "btn-primary"; - protected string SearchVal + private string searchVal { get; set; } = ""; + + private string SearchVal { get => searchVal; set @@ -54,17 +122,29 @@ namespace MP.SPEC.Pages } } - protected int totalCount = 0; + private string selAzienda + { + get => _selAzienda; + set + { + if (value != _selAzienda) + { + _selAzienda = value; + } + } + } - #endregion Protected Properties + private bool ShowCharts { get; set; } = false; - #region Protected Methods + #endregion Private Properties + + #region Private Methods /// /// Crea nuovo record e va in editing... /// /// - protected void AddNew() + private void AddNew() { isNewArt = true; currRecord = new AnagArticoliModel() @@ -79,11 +159,24 @@ namespace MP.SPEC.Pages }; } + /// + /// Verifica cancellabilità record + /// + /// + /// + private bool ArticoloDelEnabled(AnagArticoliModel currRec) + { + if (currRec.Tipo.Equals("KIT")) + return false; + + return MDService.ArticoloDelEnabled(currRec.CodArticolo); + } + /// /// Cloning record /// /// - protected void cloneRecord(AnagArticoliModel selRec) + private void cloneRecord(AnagArticoliModel selRec) { isNewArt = true; // creo record duplicato... @@ -105,7 +198,7 @@ namespace MP.SPEC.Pages /// /// /// - protected async Task deleteRecord(AnagArticoliModel selRec) + private async Task deleteRecord(AnagArticoliModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Eliminazione Articolo: sei sicuro di voler procedere?")) return; @@ -116,139 +209,16 @@ namespace MP.SPEC.Pages await Task.Delay(1); } - protected void ForceReload(int newNum) + private void ForceReload(int newNum) { numRecord = newNum; } - protected void ForceReloadPage(int newNum) + private void ForceReloadPage(int newNum) { currPage = newNum; } - protected override async Task OnInitializedAsync() - { - numRecord = 10; - await ReloadBaseData(); - } - - protected override async Task OnParametersSetAsync() - { - await ReloadDataAsync(); - } - - protected void ResetData() - { - isNewArt = false; - currRecord = null; - } - - protected async Task ResetSearch() - { - SearchVal = ""; - await ResetDataAsync(); - } - - protected async Task resetSel() - { - isNewArt = false; - currRecord = null; - await ResetDataAsync(); - } - - protected async Task selRecord(AnagArticoliModel selRec) - { - isNewArt = false; - currRecord = selRec; - await Task.Delay(1); - } - - protected async Task UpdateAsync(AnagArticoliModel selRec) - { - if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) - return; - - var done = await MDService.ArticoliUpdateRecord(selRec); - await ResetDataAsync(); - } - - protected async Task ResetDataAsync() - { - currPage = 1; - currRecord = null; - await MDService.FlushFusionCacheArticoli(); - await ReloadDataAsync(); - } - - #endregion Protected Methods - - #region Private Fields - - private string _selAzienda = "*"; - private SelectArticoliParams currFilter = new SelectArticoliParams(); - private AnagArticoliModel? currRecord = null; - private bool isNewArt = false; - private List? ListAziende; - private List? ListRecords; - private List? ListTipoArt; - private int maxNumRecord = 1000; - private int availRecord = 1000; - private int totRecord = 0; - private List? SearchRecords; - - #endregion Private Fields - - #region Private Properties - - private int _currPage = 1; - private int _numRecord = 10; - - private int currPage - { - get => _currPage; - set - { - if (_currPage != value) - { - _currPage = value; - UpdateTable(); - } - } - } - - private bool isLoading { get; set; } = false; - - private int numRecord - { - get => _numRecord; - set - { - if (_numRecord != value) - { - _numRecord = value; - UpdateTable(); - } - } - } - - private string searchVal { get; set; } = ""; - - private string selAzienda - { - get => _selAzienda; - set - { - if (value != _selAzienda) - { - _selAzienda = value; - } - } - } - /// - /// Tipo articolo selezionato - /// - private string selTipoArt = "*"; - private async Task ReloadAziendaAsync() { isLoading = true; @@ -264,28 +234,9 @@ namespace MP.SPEC.Pages await ResetDataAsync(); } - - private bool ShowCharts { get; set; } = false; - - #endregion Private Properties - - #region Private Methods - - /// - /// Verifica cancellabilità record - /// - /// - /// - private bool ArticoloDelEnabled(AnagArticoliModel currRec) - { - if (currRec.Tipo.Equals("KIT")) - return false; - - return MDService.ArticoloDelEnabled(currRec.CodArticolo); - } - private async Task ReloadBaseData() { + isLoading = true; await MDService.EnsureArtCacheLoadedAsync(true); selAzienda = await MDService.ConfigTryGetAsync("AZIENDA"); if (string.IsNullOrEmpty(selAzienda)) @@ -296,11 +247,6 @@ namespace MP.SPEC.Pages ListTipoArt = await MDService.AnagTipoArtLvAsync(); } - /// - /// Verifica cablata x add tutto tranne KIT - /// - private bool CanAdd => !selTipoArt.Equals("KIT"); - private async Task ReloadDataAsync() { isLoading = true; @@ -310,9 +256,55 @@ namespace MP.SPEC.Pages UpdateTable(); } + private void ResetData() + { + isNewArt = false; + currRecord = null; + } + + private async Task ResetDataAsync() + { + currPage = 1; + currRecord = null; + await MDService.FlushFusionCacheArticoli(); + await ReloadDataAsync(); + } + + private async Task ResetSearch() + { + SearchVal = ""; + await ResetDataAsync(); + } + + private async Task ResetSel() + { + isNewArt = false; + currRecord = null; + await ResetDataAsync(); + } + + private async Task selRecord(AnagArticoliModel selRec) + { + isNewArt = false; + currRecord = selRec; + await Task.Delay(1); + } + + private async Task UpdateAsync(AnagArticoliModel selRec) + { + if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) + return; + + var done = await MDService.ArticoliUpsertRecord(selRec); + await ResetDataAsync(); + } + private void UpdateTable() { - ListRecords = SearchRecords?.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList() ?? new(); + ListRecords = SearchRecords? + .Skip(numRecord * (currPage - 1)) + .Take(numRecord) + .ToList() ?? new(); isLoading = false; }