Completato fix gestione inventario magazzino
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
using MagMan.Core;
|
||||
using MagMan.Core.Services;
|
||||
using MagMan.Data.Admin.DbModels;
|
||||
using MagMan.Data.Admin.Services;
|
||||
using MagMan.Data.Tenant.DbModels;
|
||||
using MagMan.Data.Tenant.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace MagMan.UI.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class InventoryController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Classe per logging
|
||||
/// </summary>
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private MTAdminService MTAdminService { get; set; } = null!;
|
||||
private static JsonSerializerSettings? JSSettings;
|
||||
private TenantService TService { get; set; } = null!;
|
||||
public InventoryController(MTAdminService MTDataService, TenantService TDataService)
|
||||
{
|
||||
MTAdminService = MTDataService;
|
||||
TService = TDataService;
|
||||
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
|
||||
JSSettings = new JsonSerializerSettings()
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||||
};
|
||||
Log.Info("Avviata classe InventoryController");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Controllo status Alive
|
||||
/// GET: api/Inventory/alive
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("alive")]
|
||||
public string alive()
|
||||
{
|
||||
//Log.Debug("Chiamata alive");
|
||||
return $"OK";
|
||||
}
|
||||
|
||||
// GET api/Inventory
|
||||
[HttpGet]
|
||||
public async Task<List<ItemModel>> Get()
|
||||
{
|
||||
// se non ho chiave --> vuoto!
|
||||
List<ItemModel> ListRecords = new List<ItemModel>();
|
||||
await Task.Delay(100);
|
||||
return ListRecords;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Elenco Materiali dato RestToken
|
||||
/// </summary>
|
||||
/// <param name="id">Rest Token cliente</param>
|
||||
/// <param name="MatId">ID del materiale cercato</param>
|
||||
/// <returns></returns>
|
||||
// GET api/Inventory/00000000-0000-0000-0000-000000000000
|
||||
[HttpGet("{id}")]
|
||||
public async Task<List<MaterialModel>> Get(string id, int MatId)
|
||||
{
|
||||
// in primis recupero codice chiave da token...
|
||||
int nKey = await MTAdminService.MainKeyByToken(id);
|
||||
// ora recupero direttametne i materiali
|
||||
var ListRecords = await TService.MaterialGetFilt(nKey, MatId, true);
|
||||
return ListRecords;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processa una chiamata POST per l'invio di un array Json di oggetti MaterialModel da salvare
|
||||
/// PUT: api/Inventory/upsert/00000000-0000-0000-0000-000000000000
|
||||
/// </summary>
|
||||
/// <param name="id">token comunicazione</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("upsert/{id}")]
|
||||
public async Task<string> upsert(string id, [FromBody] RestPayload.Items rawList)
|
||||
{
|
||||
string answ = "ND";
|
||||
bool fatto = false;
|
||||
// verifico ci sia valore
|
||||
if (!string.IsNullOrEmpty(id) && rawList != null && rawList.ItemList != null)
|
||||
{
|
||||
// in primis recupero codice chiave da token...
|
||||
int nKey = await MTAdminService.MainKeyByToken(id);
|
||||
if (nKey > 0)
|
||||
{
|
||||
// creo oggetti materiale da lista ricevuta
|
||||
List<ItemModel> matList = rawList.ItemList.Select(jpl => TService.ItemFromDto(jpl)).ToList();
|
||||
|
||||
foreach (var item in matList)
|
||||
{
|
||||
try
|
||||
{
|
||||
await TService.ItemUpdate(nKey, item);
|
||||
fatto = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"InventoryController.upsert | Errore in fase salvataggio ItemDto{Environment.NewLine}{exc}");
|
||||
fatto = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
answ = fatto ? "OK" : "NO";
|
||||
return answ;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user