ffca896c5d
- alias - materiali
145 lines
5.1 KiB
C#
145 lines
5.1 KiB
C#
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;
|
|
using static MagMan.Core.RestPayload;
|
|
|
|
namespace MagMan.UI.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class AliasController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
public AliasController(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 AliasController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Controllo status Alive
|
|
/// GET: api/Alias/alive
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("alive")]
|
|
public string alive()
|
|
{
|
|
//Log.Debug("Chiamata alive");
|
|
return $"OK";
|
|
}
|
|
|
|
// GET api/Alias
|
|
[HttpGet]
|
|
public async Task<List<AliasModel>> Get()
|
|
{
|
|
// se non ho chiave --> vuoto!
|
|
List<AliasModel> ListRecords = new List<AliasModel>();
|
|
await Task.Delay(100);
|
|
return ListRecords;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Materiali dato RestToken
|
|
/// </summary>
|
|
/// <param name="id">Rest Token cliente</param>
|
|
/// <returns></returns>
|
|
// GET api/Alias/00000000-0000-0000-0000-000000000000
|
|
[HttpGet("{id}")]
|
|
public async Task<List<AliasDTO>> Get(string id)
|
|
{
|
|
List<AliasDTO> ListDto = new List<AliasDTO>();
|
|
// in primis recupero codice chiave da token...
|
|
int nKey = await MTAdmService.MainKeyByToken(id);
|
|
// ora recupero direttamente elenco Alias x MatCode, cablato
|
|
var rawData = await TService.AliasGetFilt(nKey, "MatCode");
|
|
if (rawData != null && rawData.Count > 0)
|
|
{
|
|
ListDto = rawData.Select(x => new AliasDTO() { ValOrig = x.ValueOriginal, ValAlias = x.ValueAlias, IsActive = x.IsActive }).ToList();
|
|
}
|
|
return ListDto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processa una chiamata POST per l'invio di un array Json di oggetti AliasDTO da salvare
|
|
/// PUT: api/Materials/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.Alias rawList)
|
|
{
|
|
string answ = "ND";
|
|
bool fatto = false;
|
|
// verifico ci sia valore
|
|
if (!string.IsNullOrEmpty(id) && rawList != null && rawList.AliasList != null)
|
|
{
|
|
// in primis recupero codice chiave da token...
|
|
int nKey = await MTAdmService.MainKeyByToken(id);
|
|
if (nKey > 0)
|
|
{
|
|
// creo oggetti materiale da lista ricevuta
|
|
List<AliasModel> recList = rawList.AliasList.Select(jpl => TService.AliasFromDto(jpl, "MatCode")).ToList();
|
|
|
|
try
|
|
{
|
|
fatto = await TService.AliasUpsert(nKey, recList);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"AliasController.upsert | Errore in fase salvataggio AliasDto{Environment.NewLine}{exc}");
|
|
fatto = false;
|
|
}
|
|
// resetto cache redis
|
|
await MTAdmService.FlushRedisCache();
|
|
// broadcast update via redis, indico nKey x update puntuale
|
|
MService.QueUpdAliasMat.sendMessage($"{nKey}");
|
|
}
|
|
}
|
|
answ = fatto ? "OK" : "NO";
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
/// <summary>
|
|
/// Classe per logging
|
|
/// </summary>
|
|
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
|
|
}
|
|
} |