Files
lux/EgwCoreLib.Lux.Data/Services/Config/ConfGlassService.cs
T
2026-03-25 10:03:13 +01:00

95 lines
2.9 KiB
C#

namespace EgwCoreLib.Lux.Data.Services.Config
{
public class ConfGlassService : BaseServ, IConfGlassService
{
#region Public Constructors
public ConfGlassService(
IConfiguration config,
IConnectionMultiplexer redis,
IConfGlassRepository repo) : base(config, redis)
{
_className = "ConfGlass";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> DeleteAsync(GlassModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByIdAsync(rec2del.GlassID);
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;
});
}
/// <inheritdoc />
public async Task<List<GlassModel>> 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
);
});
}
/// <inheritdoc />
public async Task<bool> UpsertAsync(GlassModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(upsRec.GlassID);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
upsRec.Code = string.IsNullOrEmpty(upsRec.Code) ? $"{upsRec.GlassID: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 IConfGlassRepository _repo;
#endregion Private Fields
}
}