Files
lux/EgwCoreLib.Lux.Data/Services/Utils/GenClassService.cs
T
Samuele E. Locatelli (W11-AI) 912af1e60e Aggiunta commenti, part I
2026-03-18 19:27:19 +01:00

115 lines
3.3 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Utils;
using EgwCoreLib.Lux.Data.Repository.Utils;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
namespace EgwCoreLib.Lux.Data.Services.Utils
{
public class GenClassService : BaseServ, IGenClassService
{
#region Public Constructors
public GenClassService(
IConfiguration config,
IConnectionMultiplexer redis,
IGenClassRepository repo) : base(config, redis)
{
_className = "GenClass";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="rec2del"></param>
/// <returns></returns>
public async Task<bool> DeleteAsync(GenClassModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByCodeAsync(rec2del.ClassCod);
if (dbResult == null) return false;
var numChild = await _repo.CountChildrenAsync(rec2del.ClassCod);
if (numChild > 0)
{
activity?.SetTag("delete.status", "rejected_has_children");
return false;
}
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
}
return success;
});
}
/// <summary>
/// Elenco completo GenClass da DB
/// </summary>
/// <returns></returns>
public async Task<List<GenClassModel>> GetAllAsync()
{
return await TraceAsync($"{_className}.GetAll", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ALL",
async () => await _repo.GetAllAsync(),
UltraLongCache
);
});
}
/// <summary>
/// Upsert record GenClass (aggiorna o inserisce)
/// </summary>
/// <param name="upsRec"></param>
/// <returns></returns>
public async Task<bool> UpsertAsync(GenClassModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
var currRec = await _repo.GetByCodeAsync(upsRec.ClassCod);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
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 IGenClassRepository _repo;
#endregion Private Fields
}
}