using k8s.Models; using MagMan.Core; using MagMan.Core.DTO; 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; using System.Diagnostics; namespace MagMan.UI.Controllers { [Route("api/[controller]")] [ApiController] public class ProjectsController : ControllerBase { /// /// Classe per logging /// private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); private MTAdminService MTAdmService { get; set; } = null!; private static JsonSerializerSettings? JSSettings; private TenantService TService { get; set; } = null!; public ProjectsController(MTAdminService MTDataService, TenantService TDataService) { MTAdmService = 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 ProjectsController"); } /// /// Controllo status Alive /// GET: api/Machines/alive /// /// [HttpGet("alive")] public string alive() { //Log.Debug("Chiamata alive"); return $"OK"; } // GET api/Machines/5 [HttpGet] public async Task> Get() { // se non ho chaive --> vuoto! List ListRecords = new List(); await Task.Delay(100); return ListRecords; } /// /// Elenco Progetti dato RestToken /// /// Rest Token cliente /// Chiave associata ai progetti /// // GET api/Machines/2cba60c7-7be4-40b1-aa0d-52e7c71fc1a7 [HttpGet("{id}")] public async Task> Get(string id, int KeyNum) { List ListRecords = new List(); if (!string.IsNullOrEmpty(id)) { // in primis recupero codice chiave da token... int nKey = await MTAdmService.MainKeyByToken(id); ListRecords = await TService.ProjectGetAll(nKey); } return ListRecords; } /// /// Processa una chiamata POST per l'invio di un oggetto di aggiornamento progetto /// PUT: api/Inventory/upsert/00000000-0000-0000-0000-000000000000 /// /// token comunicazione /// [HttpPost("upsert/{id}")] public async Task upsert(string id, [FromBody] RestPayload.Projects rawData) { string answ = "ND"; bool fatto = false; // verifico ci sia valore if (!string.IsNullOrEmpty(id) && rawData != null && rawData.Project != null) { // in primis recupero codice chiave da token... int nKey = await MTAdmService.MainKeyByToken(id); if (nKey > 0) { // converto ProjDto --> DB var currRec = TService.ProjectFromDto(rawData.Project); try { await TService.ProjectUpdate(nKey, currRec); fatto = true; } catch (Exception exc) { Log.Error($"ProjectsController.upsert | Errore in fase salvataggio ProjectDTO{Environment.NewLine}{exc}"); fatto = false; } // resetto cache redis await MTAdmService.FlushRedisCache(); } } answ = fatto ? "OK" : "NO"; return answ; } } }