using k8s.Models; using MagMan.Core; using MagMan.Core.DTO; 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 { #region Public Constructors public InventoryController(MTAdminService MTDataService, TenantService TDataService, MessageService messageService) { MTAdmService = MTDataService; TService = TDataService; MService = messageService; // 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"); } #endregion Public Constructors #region Public Methods /// /// Controllo status Alive /// GET: api/Inventory/alive /// /// [HttpGet("alive")] public string alive() { //Log.Debug("Chiamata alive"); return $"OK"; } // GET api/Inventory [HttpGet] public async Task> Get() { // se non ho chiave --> vuoto! List ListRecords = new List(); await Task.Delay(100); return ListRecords; } /// /// Elenco Materiali dato RestToken /// /// Rest Token cliente /// ID del materiale cercato /// // GET api/Inventory/00000000-0000-0000-0000-000000000000 [HttpGet("{id}")] public async Task> Get(string id, int MatCloudId) { // in primis recupero codice chiave da token... int nKey = await MTAdmService.MainKeyByToken(id); // ora recupero direttametne i materiali var ListRecords = await TService.MaterialDtoGetFilt(nKey, MatCloudId, true); return ListRecords; } /// /// 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 /// /// token comunicazione /// [HttpPost("upsert/{id}")] public async Task 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 MTAdmService.MainKeyByToken(id); if (nKey > 0) { // creo oggetti materiale da lista ricevuta List currList = rawList.ItemList.Select(jpl => TService.ItemFromDto(jpl, nKey)).ToList(); foreach (var item in currList) { try { // chiamo metodo SENZA aggiornamento forzato quantità (x upload da EgtBW) await TService.ItemUpdate(nKey, item, $"Key: {nKey}", false, true); fatto = true; } catch (Exception exc) { Log.Error($"InventoryController.upsert | Errore in fase salvataggio ItemDto{Environment.NewLine}{exc}"); fatto = false; } } // resetto cache redis await MTAdmService.FlushRedisCache(); // broadcast update via redis, indico nKey update puntuale MService.QueUpdInve.sendMessage($"{nKey}"); } } answ = fatto ? "OK" : "NO"; return answ; } #endregion Public Methods #region Private Fields private static JsonSerializerSettings? JSSettings; /// /// Classe per logging /// private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Private Properties private MessageService MService { get; set; } = null!; private MTAdminService MTAdmService { get; set; } = null!; private TenantService TService { get; set; } = null!; #endregion Private Properties } }