Files
lux/EgwCoreLib.Lux.Data/Services/Config/ConfProfileService.cs
T
2026-03-17 11:35:38 +01:00

97 lines
3.0 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Config;
using EgwCoreLib.Lux.Data.Repository.Config;
using Microsoft.Extensions.Configuration;
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
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;
});
}
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}",
async () => await _repo.GetAllAsync(),
UltraLongCache
);
});
}
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
}
}