Files
2025-09-22 19:37:14 +02:00

702 lines
24 KiB
C#

using MagMan.Core;
using MagMan.Core.Services;
using MagMan.Data.Admin.Controllers;
using MagMan.Data.Admin.DbModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using StackExchange.Redis;
using System.Diagnostics;
namespace MagMan.Data.Admin.Services
{
public class MTAdminService : BaseServ, IDisposable
{
#region Public Constructors
public MTAdminService(IConfiguration configuration, IConnectionMultiplexer redisConnMult, IEmailSender emailSender, UserManager<IdentityUser> userManager)
{
Log.Info("MTAdminService starting...");
_configuration = configuration;
_emailSender = emailSender;
_userManager = userManager;
// Conf cache
redisConn = redisConnMult;
redisDb = this.redisConn.GetDatabase();
// 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
};
// cod app
CodApp = _configuration.GetValue<string>("OptConf:CodApp");
// gestione parallelismo...
numPar = _configuration.GetValue<int>("OptConf:NumPar");
// DB
dbController = new MTAdminController(configuration);
// chiudo log
Log.Info("MTAdminService started!");
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Lista AuthKey
/// </summary>
/// <param name="CustomerId">0 = tutti</param>
/// <returns></returns>
public async Task<List<AuthKeyModel>> AuthKeyByCustId(int CustomerId)
{
string source = "DB";
List<AuthKeyModel>? dbResult = new List<AuthKeyModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Admin:AuthKeyList:{CustomerId}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2)
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthKeyModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthKeyModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthKeyByCustId(CustomerId);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (dbResult == null)
{
dbResult = new List<AuthKeyModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthKeyByCustId | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthKeyByCustId:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Delete record AuthKey + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> AuthKeyDelete(AuthKeyModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.AuthKeyDelete(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during AuthKeyDelete:{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Lista AuthKey da Rest Token
/// </summary>
/// <param name="RestToken">token di auth del cliente</param>
/// <returns></returns>
public async Task<List<AuthKeyModel>> AuthKeyGetByToken(string RestToken)
{
List<AuthKeyModel>? dbResult = new List<AuthKeyModel>();
// cerco in primis customer ID...
int CustomerId = await CustomerIdByToken(RestToken);
if (CustomerId >= 0)
{
dbResult = await AuthKeyByCustId(CustomerId);
}
return dbResult;
}
/// <summary>
/// Update record AuthKey + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> AuthKeyUpdate(AuthKeyModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.AuthKeyUpdate(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during AuthKeyUpdate:{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Delete record customer + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> CustomerDelete(CustomerModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.CustomerDelete(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during CustomerDelete:{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Lista Customers
/// </summary>
/// <returns></returns>
public async Task<List<CustomerModel>> CustomerGetAll()
{
string source = "DB";
List<CustomerModel>? dbResult = new List<CustomerModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Admin:CustomersList";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<CustomerModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<CustomerModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.CustomerGetAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (dbResult == null)
{
dbResult = new List<CustomerModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"CustomerGetAll | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during CustomerGetAll:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="RestToken"></param>
/// <returns></returns>
public async Task<int> CustomerIdByToken(string RestToken)
{
int answ = -1;
string currKey = $"{Const.rKeyConfig}:Admin:Dict:Token2CustID";
answ = RedisHashGetInt(currKey, RestToken);
if (answ <= 0)
{
// cerco nel DB
var custList = await CustomerGetAll();
var custRow = custList.FirstOrDefault(x => x.RestToken == RestToken);
// se trovato salvo
if (custRow != null)
{
answ = custRow.CustomerID;
RedisHashSet(currKey, RestToken, $"{answ}");
Log.Info($"Token2CustID: added {RestToken} --> {answ}");
}
}
return answ;
}
/// <summary>
/// Update record customer + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> CustomerUpdate(CustomerModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.CustomerUpdate(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during CustomerUpdate:{Environment.NewLine}{exc}");
}
return fatto;
}
public void Dispose()
{
dbController.Dispose();
}
/// <summary>
/// Delete record Machine + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> MachineDelete(MachineModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.MachineDelete(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during MachineDelete:{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Lista Macchine
/// </summary>
/// <param name="CustomerId">0 = tutti</param>
/// <returns></returns>
public async Task<List<MachineModel>> MachineGetByCustId(int CustomerId)
{
string source = "DB";
List<MachineModel>? dbResult = new List<MachineModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Admin:MachineList:{CustomerId}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<MachineModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<MachineModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.MachineGetByCustId(CustomerId);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<MachineModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<MachineModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"MachineGetByCustId | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during MachineGetByCustId:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Lista Macchine da Rest Token
/// </summary>
/// <param name="RestToken">token di auth del cliente</param>
/// <returns></returns>
public async Task<List<MachineModel>> MachineGetByToken(string RestToken)
{
List<MachineModel>? dbResult = new List<MachineModel>();
// cerco in primis customer ID...
int CustomerId = await CustomerIdByToken(RestToken);
if (CustomerId >= 0)
{
dbResult = await MachineGetByCustId(CustomerId);
}
return dbResult;
}
/// <summary>
/// Update record Machine + refresh cache
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> MachineUpdate(MachineModel currItem)
{
bool fatto = false;
try
{
fatto = dbController.MachineUpdate(currItem);
if (fatto)
{
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Error during MachineUpdate:{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="CustID"></param>
/// <returns></returns>
public async Task<int> MainKeyByCustomer(int CustID)
{
int answ = -1;
string currKey = $"{Const.rKeyConfig}:Admin:Dict:Cust2MKey";
answ = RedisHashGetInt(currKey, $"{CustID}");
if (answ <= 0)
{
// cerco nel DB
var custList = await CustomerGetAll();
var custRow = custList.FirstOrDefault(x => x.CustomerID == CustID);
// se trovato salvo
if (custRow != null)
{
answ = custRow.MainKey;
RedisHashSet(currKey, $"{CustID}", $"{answ}");
Log.Info($"Cust2MKey: added {CustID} --> {answ}");
}
}
return answ;
}
/// <summary>
/// Recupera MainKey dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="RestToken"></param>
/// <returns></returns>
public async Task<int> MainKeyByToken(string RestToken)
{
int answ = -1;
string currKey = $"{Const.rKeyConfig}:Admin:Dict:Token2MKey";
answ = RedisHashGetInt(currKey, RestToken);
if (answ <= 0)
{
// cerco nel DB
var custList = await CustomerGetAll();
var custRow = custList.FirstOrDefault(x => x.RestToken == RestToken);
// se trovato salvo
if (custRow != null)
{
answ = custRow.MainKey;
RedisHashSet(currKey, RestToken, $"{answ}");
Log.Info($"TokenMKey: added {RestToken} --> {answ}");
}
}
return answ;
}
/// <summary>
/// Reset dati cache redis
/// </summary>
/// <returns></returns>
public async Task ResetUserDataCache()
{
string currKey = $"{Const.rKeyConfig}:Admin:UserData:*";
await ExecFlushRedisPattern((RedisValue)currKey);
}
public async Task<List<UserData>> UserDataGetFilt(string searchVal)
{
// Collezione utenti
List<IdentityUser> RawList = new List<IdentityUser>();
List<UserData> dbResult = new List<UserData>();
string source = "DB";
try
{
string typeSearch = string.IsNullOrEmpty(searchVal) ? "ALL" : searchVal;
string currKey = $"{Const.rKeyConfig}:Admin:UserData:{typeSearch}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2)
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<UserData>>(rawData);
if (tempResult == null)
{
dbResult = new List<UserData>();
}
else
{
dbResult = tempResult;
}
}
else
{
// recupero utenti da obj _userManager
var allData = _userManager.Users.ToList();
if (!string.IsNullOrEmpty(searchVal))
{
RawList = allData.Where(x => x.NormalizedEmail.Contains(searchVal.ToUpper()) || x.NormalizedUserName.Contains(searchVal.ToUpper())).ToList();
}
else
{
RawList = allData;
}
var user = RawList.Select(x => new IdentityUser
{
Id = x.Id,
UserName = x.UserName,
Email = x.Email,
PhoneNumber = x.PhoneNumber,
PasswordHash = "*****",
EmailConfirmed = x.EmailConfirmed
}).ToList();
foreach (var item in user)
{
var UserRoles = await _userManager.GetRolesAsync(item);
var UserClaims = await _userManager.GetClaimsAsync(item);
var newItem = new UserData()
{
Identity = item,
Roles = UserRoles.ToList(),
Claims = UserClaims.ToList()
};
dbResult.Add(newItem);
}
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (dbResult == null)
{
dbResult = new List<UserData>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"UserDataGetFilt | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during UserDataGetFilt:{Environment.NewLine}{exc}");
}
return dbResult;
}
#endregion Public Methods
#region Protected Fields
protected static MTAdminController dbController = null!;
#endregion Protected Fields
#region Private Fields
private static JsonSerializerSettings? JSSettings;
private readonly IEmailSender _emailSender;
private readonly UserManager<IdentityUser> _userManager;
#endregion Private Fields
#region Private Methods
/// <summary>
/// Recupera UN SINGOLO VALORE dalla hash per un dato field
/// </summary>
/// <param name="hashKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
private string RedisHashGet(RedisKey hashKey, string hashField)
{
string answ = "";
var hasVal = redisDb.HashExists(hashKey, hashField);
if (hasVal)
{
answ = redisDb.HashGet(hashKey, hashField).ToString();
}
return answ;
}
/// <summary>
/// Recupero HashSet redis come Dictionary
/// </summary>
/// <param name="hashKey"></param>
private Dictionary<string, string> RedisHashGetDict(RedisKey hashKey)
{
Dictionary<string, string> answ = new Dictionary<string, string>();
try
{
answ = redisDb
.HashGetAll(hashKey)
.ToDictionary(x => $"{x.Name}", x => $"{x.Value}");
}
catch (Exception exc)
{
Log.Info($"Errore RedisHashGetDict | hashKey: {hashKey}{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Recupera UN SINGOLO VALORE dalla hash per un dato field come INT
/// </summary>
/// <param name="hashKey">Redis Key for Hashlist</param>
/// <param name="hashField">Requested key on list</param>
/// <returns>Value as Int</returns>
private int RedisHashGetInt(RedisKey hashKey, string hashField)
{
int result = 0;
var rawRes = RedisHashGet(hashKey, hashField);
if (!string.IsNullOrEmpty(rawRes))
{
int.TryParse($"{rawRes}", out result);
}
return result;
}
/// <summary>
/// Aggiunta KVP in HashSet Redis
/// </summary>
/// <param name="currKey"></param>
/// <param name="dict"></param>
private bool RedisHashSet(RedisKey currKey, string hashField, string value)
{
bool fatto = false;
try
{
// salvo!
redisDb.HashSet(currKey, hashField, value);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashSet | currKey: {currKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis
/// </summary>
/// <param name="hashKey"></param>
/// <param name="dict"></param>
private bool RedisHashSetDict(RedisKey hashKey, Dictionary<string, string> dict)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[dict.Count];
int i = 0;
foreach (KeyValuePair<string, string> kvp in dict)
{
data2ins[i] = new HashEntry(kvp.Key, kvp.Value);
i++;
}
// salvo!
redisDb.HashSet(hashKey, data2ins);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashSet | hashKey: {hashKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis
/// </summary>
/// <param name="hashKey"></param>
/// <param name="dict"></param>
/// <param name="ttl"></param>
private bool RedisHashSetDict(RedisKey hashKey, Dictionary<string, string> dict, TimeSpan ttl)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[dict.Count];
int i = 0;
foreach (KeyValuePair<string, string> kvp in dict)
{
data2ins[i] = new HashEntry(kvp.Key, kvp.Value);
i++;
}
// salvo!
redisDb.HashSet(hashKey, data2ins);
redisDb.KeyExpire(hashKey, ttl);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashSet | hashKey: {hashKey}{Environment.NewLine}{exc}");
}
return fatto;
}
#endregion Private Methods
}
}