Update add articoli (upsert)

This commit is contained in:
Samuele Locatelli
2026-06-11 12:02:27 +02:00
parent d447b5501f
commit 06baf0167c
5 changed files with 168 additions and 173 deletions
+6 -3
View File
@@ -322,7 +322,7 @@ namespace MP.Data.Repository.Anag
}
/// <inheritdoc />
public async Task<bool> ArticoliUpdateRecord(AnagArticoliModel editRec)
public async Task<bool> 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;
}
/// <inheritdoc />
+2 -2
View File
@@ -140,11 +140,11 @@ namespace MP.Data.Repository.Anag
Task<List<AnagArticoliModel>> ArticoliInKitAsync();
/// <summary>
/// Update Record Articolo
/// Upsert (add/update) Record Articolo
/// </summary>
/// <param name="editRec">Record da aggiornare</param>
/// <returns>True se aggiornato</returns>
Task<bool> ArticoliUpdateRecord(AnagArticoliModel editRec);
Task<bool> ArticoliUpsertAsync(AnagArticoliModel editRec);
/// <summary>
/// Elenco Gruppi tipo REPARTOin formato DTO con conteggi del numero record trovati filtrati per operatore
+4 -4
View File
@@ -364,15 +364,15 @@ namespace MP.SPEC.Data
/// </summary>
/// <param name="currRec"></param>
/// <returns></returns>
public async Task<bool> ArticoliUpdateRecord(AnagArticoliModel currRec)
public async Task<bool> 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;
}
+2 -2
View File
@@ -158,7 +158,7 @@
</div>
<div class="card-body">
@if (ListRecords == null || isLoading)
@if (isLoading && false)
{
<LoadingData></LoadingData>
}
@@ -176,7 +176,7 @@
<th>
@if (currRecord != null)
{
<button @onclick="() => resetSel()" class="btn btn-primary btn-sm"><i class="bi bi-arrow-counterclockwise"></i></button>
<button @onclick="ResetSel" class="btn btn-primary btn-sm"><i class="bi bi-arrow-counterclockwise"></i></button>
}
</th>
<th><i class="fa-solid fa-file"></i> Articolo</th>
+154 -162
View File
@@ -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<AnagGruppiModel>? ListAziende;
private List<AnagArticoliModel>? ListRecords;
private List<ListValuesModel>? ListTipoArt;
private int maxNumRecord = 1000;
private List<AnagArticoliModel>? SearchRecords;
private string selTipoArt = "*";
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Verifica cablata x add tutto tranne KIT
/// </summary>
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
/// <summary>
/// Crea nuovo record e va in editing...
/// </summary>
/// <returns></returns>
protected void AddNew()
private void AddNew()
{
isNewArt = true;
currRecord = new AnagArticoliModel()
@@ -79,11 +159,24 @@ namespace MP.SPEC.Pages
};
}
/// <summary>
/// Verifica cancellabilità record
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
private bool ArticoloDelEnabled(AnagArticoliModel currRec)
{
if (currRec.Tipo.Equals("KIT"))
return false;
return MDService.ArticoloDelEnabled(currRec.CodArticolo);
}
/// <summary>
/// Cloning record
/// </summary>
/// <param name="selRec"></param>
protected void cloneRecord(AnagArticoliModel selRec)
private void cloneRecord(AnagArticoliModel selRec)
{
isNewArt = true;
// creo record duplicato...
@@ -105,7 +198,7 @@ namespace MP.SPEC.Pages
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
protected async Task deleteRecord(AnagArticoliModel selRec)
private async Task deleteRecord(AnagArticoliModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("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<bool>("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<AnagGruppiModel>? ListAziende;
private List<AnagArticoliModel>? ListRecords;
private List<ListValuesModel>? ListTipoArt;
private int maxNumRecord = 1000;
private int availRecord = 1000;
private int totRecord = 0;
private List<AnagArticoliModel>? 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;
}
}
}
/// <summary>
/// Tipo articolo selezionato
/// </summary>
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
/// <summary>
/// Verifica cancellabilità record
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
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();
}
/// <summary>
/// Verifica cablata x add tutto tranne KIT
/// </summary>
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<bool>("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;
}