207 lines
6.9 KiB
C#
207 lines
6.9 KiB
C#
using EgwCoreLib.Lux.Data.DbModel.Config;
|
|
using EgwCoreLib.Lux.Data.Repository.Config;
|
|
using EgwMultiEngineManager.Data;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
namespace EgwCoreLib.Lux.Data.Services.Config
|
|
{
|
|
public class ConfProfileService : BaseServ, IConfProfileService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ConfProfileService(
|
|
IConfiguration config,
|
|
IConnectionMultiplexer redis,
|
|
IConfProfileRepository repo) : base(config, redis)
|
|
{
|
|
_className = "ConfProfile";
|
|
_repo = repo;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Eliminazione record
|
|
/// </summary>
|
|
/// <param name="rec2del"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteAsync(ProfileModel rec2del)
|
|
{
|
|
return await TraceAsync($"{_className}.Delete", async (activity) =>
|
|
{
|
|
var dbResult = await _repo.GetByIdAsync(rec2del.ProfileID);
|
|
if (dbResult == null) return false;
|
|
|
|
// 3. Eseguo la cancellazione
|
|
bool success = await _repo.DeleteAsync(dbResult);
|
|
|
|
// 4. Se ha avuto successo, pulisco la cache
|
|
if (success)
|
|
{
|
|
await ClearCacheAsync($"{_redisBaseKey}:{_className}");
|
|
}
|
|
|
|
return success;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo Profile da DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ProfileModel>> GetAllAsync()
|
|
{
|
|
// Uso helper TraceAsync che gestisce automaticamente StartActivity, Log e Exception tracking
|
|
return await TraceAsync($"{_className}.GetAll", async (activity) =>
|
|
{
|
|
return await GetOrSetCacheAsync(
|
|
$"{_redisBaseKey}:{_className}:ALL",
|
|
async () => await _repo.GetAllAsync(),
|
|
UltraLongCache
|
|
);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue merge dei dati nella tab profili del DB con le info accessorie...
|
|
/// </summary>
|
|
/// <param name="uID"></param>
|
|
/// <param name="execEnvironment"></param>
|
|
/// <param name="rawContent"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SaveProfileListAsync(string uID, Constants.EXECENVIRONMENTS execEnvironment, string rawContent)
|
|
{
|
|
return await TraceAsync($"{_className}.SaveProfileList", async (activity) =>
|
|
{
|
|
string operation = "SaveProfileList";
|
|
bool success = false;
|
|
|
|
if (string.IsNullOrWhiteSpace(rawContent))
|
|
return false;
|
|
|
|
|
|
// in primis recupero profili attuali
|
|
var dbList = await _repo.GetAllAsync();
|
|
|
|
// ciclo sul contenuto ricevuto, se mancasse aggiungo!
|
|
List<string> list2check = JsonConvert.DeserializeObject<List<string>>(rawContent) ?? new List<string>();
|
|
List<ProfileModel> rec2ins = new List<ProfileModel>();
|
|
foreach (var item in list2check)
|
|
{
|
|
if (!dbList.Any(x => x.Code == item))
|
|
{
|
|
rec2ins.Add(new ProfileModel() { Code = item, Description = $"{item} - NEW" });
|
|
}
|
|
}
|
|
|
|
// se ho dati li inserisco...
|
|
if (rec2ins.Count > 0)
|
|
{
|
|
success = await _repo.AddRangeAsync(rec2ins);
|
|
}
|
|
|
|
activity?.SetTag("db.operation", operation);
|
|
if (success)
|
|
{
|
|
await ClearCacheAsync($"{_redisBaseKey}:{_className}");
|
|
}
|
|
|
|
return success;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salvataggio info serializzate x soglie e dati opzionali sul DB
|
|
/// </summary>
|
|
/// <param name="uID"></param>
|
|
/// <param name="execEnvironment"></param>
|
|
/// <param name="rawThreshold"></param>
|
|
/// <param name="rawData"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SaveProfileThreshAsync(string uID, Constants.EXECENVIRONMENTS execEnvironment, string rawThreshold, string rawData)
|
|
{
|
|
return await TraceAsync($"{_className}.SaveProfileThresh", async (activity) =>
|
|
{
|
|
var currRec = await _repo.GetByUidAsync(uID);
|
|
|
|
string operation = "UPDATE";
|
|
bool success = false;
|
|
if (currRec != null)
|
|
{
|
|
currRec.ProfDataRaw = rawData;
|
|
currRec.ThreshDataRaw = rawThreshold;
|
|
success = await _repo.UpdateAsync(currRec);
|
|
}
|
|
else
|
|
{
|
|
operation = "INSERT";
|
|
var upsRec = new ProfileModel()
|
|
{
|
|
Code = uID,
|
|
Description = $"{uID} - NEW PROFILE",
|
|
ProfDataRaw = rawData,
|
|
ThreshDataRaw = rawThreshold
|
|
};
|
|
success = await _repo.AddAsync(upsRec);
|
|
}
|
|
|
|
activity?.SetTag("db.operation", operation);
|
|
|
|
if (success)
|
|
{
|
|
await ClearCacheAsync($"{_redisBaseKey}:{_className}");
|
|
}
|
|
|
|
return success;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert record Profile (aggiorna o inserisce)
|
|
/// </summary>
|
|
/// <param name="upsRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpsertAsync(ProfileModel upsRec)
|
|
{
|
|
return await TraceAsync($"{_className}.Upsert", async (activity) =>
|
|
{
|
|
var currRec = await _repo.GetByIdAsync(upsRec.ProfileID);
|
|
|
|
string operation = "UPDATE";
|
|
bool success = false;
|
|
if (currRec != null)
|
|
{
|
|
upsRec.Code = string.IsNullOrEmpty(upsRec.Code) ? $"{upsRec.ProfileID:0000}" : upsRec.Code;
|
|
success = await _repo.UpdateAsync(upsRec);
|
|
}
|
|
else
|
|
{
|
|
operation = "INSERT";
|
|
success = await _repo.AddAsync(upsRec);
|
|
}
|
|
|
|
activity?.SetTag("db.operation", operation);
|
|
|
|
if (success)
|
|
{
|
|
await ClearCacheAsync($"{_redisBaseKey}:{_className}");
|
|
}
|
|
|
|
return success;
|
|
});
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private readonly string _className;
|
|
private readonly IConfProfileRepository _repo;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |