Files
magman/MagMan.UI/Controllers/ResourcesController.cs
T
2024-01-25 19:44:28 +01:00

156 lines
6.3 KiB
C#

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;
namespace MagMan.UI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ResourcesController : ControllerBase
{
/// <summary>
/// Classe per logging
/// </summary>
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private MTAdminService MTAdmService { get; set; } = null!;
private static JsonSerializerSettings? JSSettings;
private TenantService TService { get; set; } = null!;
public ResourcesController(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 ResourcesController");
}
/// <summary>
/// Controllo status Alive
/// GET: api/Resources/alive
/// </summary>
/// <returns></returns>
[HttpGet("alive")]
public string alive()
{
//Log.Debug("Chiamata alive");
return $"OK";
}
// GET api/Resources/5
[HttpGet]
public async Task<List<ResourceModel>> Get()
{
// se non ho chaive --> vuoto!
List<ResourceModel> ListRecords = new List<ResourceModel>();
await Task.Delay(100);
return ListRecords;
}
/// <summary>
/// Elenco Macchine dato RestToken
/// </summary>
/// <param name="id">Rest Token cliente</param>
/// <param name="projDbId">ID progetto</param>
/// <param name="isEstim">true = ultima stima attiva / false = consumi effettivi</param>
/// <returns></returns>
// GET api/Resources/2cba60c7-7be4-40b1-aa0d-52e7c71fc1a7
[HttpGet("{id}")]
public async Task<List<ResourceModel>> Get(string id, int projDbId, bool isEstim)
{
List<ResourceModel> ListRecords = new List<ResourceModel>();
// verifico ci sia valore
if (!string.IsNullOrEmpty(id))
{
// in primis recupero codice chiave da token...
int nKey = await MTAdmService.MainKeyByToken(id);
ListRecords = await TService.ResourcesGetByProject(nKey, projDbId, isEstim, false);
}
return ListRecords;
}
/// <summary>
/// Processa una chiamata POST per l'invio di un oggetto di aggiornamento risorse progetto (RestPayload.Resources)
/// PUT: api/Resources/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.Resources projectData)
{
string answ = "ND";
bool fatto = false;
// verifico ci sia valore
if (!string.IsNullOrEmpty(id) && projectData != null)
{
// in primis recupero codice chiave da token...
int nKey = await MTAdmService.MainKeyByToken(id);
if (nKey > 0)
{
// recupero ID interno da id esterno...
int ProjDbId = 0;
ProjModel? projRec = null;
var allProj = await TService.ProjectGetByMachine(nKey, 0);
if (allProj != null)
{
projRec = allProj.Find(x => x.ProjExtDbId == projectData.ProjExtDbId);
if (projRec != null)
{
ProjDbId = projRec.ProjDbId;
}
}
if (ProjDbId > 0)
{
// in primis registro il record RequestPlan...
var recPlan = new RequestPlanModel()
{
DtRequest = DateTime.Now,
IsActive = true,
ReqState = projectData.ReqState,
ProjDbId = ProjDbId
};
int reqId = await TService.ReqPlanUpdate(nKey, recPlan);
// per ogni riga risorsa registro le info relative...
List<ResourceModel> listRes = new List<ResourceModel>();
if (projectData.ResourceList != null)
{
listRes = projectData.ResourceList.Select(x => TService.ResourceFromDto(x, reqId)).ToList();
try
{
string kDesc = $"K{nKey}";
string prDesc = kDesc;
if (projRec != null)
{
prDesc = $"P: {projRec.ProjExtId}.{projRec.ProjExtDbId} | {projRec.ProjDescription} ({projRec.Machine})";
}
await TService.ResourceUpdate(nKey, listRes, projectData.ReqState, $"{prDesc} | {kDesc}");
fatto = true;
}
catch (Exception exc)
{
Log.Error($"ResourceController.upsert | Errore in fase salvataggio ResourceDto{Environment.NewLine}{exc}");
fatto = false;
}
}
// resetto cache redis
await MTAdmService.FlushRedisCache();
}
}
}
answ = fatto ? "OK" : "NO";
return answ;
}
}
}