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

142 lines
4.5 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Job;
using EgwCoreLib.Lux.Data.Repository.Job;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
namespace EgwCoreLib.Lux.Data.Services.Job
{
public class JobStepService : BaseServ, IJobStepService
{
#region Public Constructors
public JobStepService(
IConfiguration config,
IConnectionMultiplexer redis,
IJobStepRepository repo) : base(config, redis)
{
_className = "JobStep";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="rec2del"></param>
/// <returns></returns>
public async Task<bool> DeleteAsync(JobStepModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByIdAsync(rec2del.JobStepID);
if (dbResult == null) return false;
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobTaskList:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
/// <summary>
/// Elenco JobStep per parent (jobID)
/// </summary>
/// <param name="jobID"></param>
/// <returns></returns>
public async Task<List<JobStepModel>> GetByParentAsync(int jobID)
{
return await TraceAsync($"{_className}.GetByParent", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:{jobID}",
async () => await _repo.GetByParentAsync(jobID),
UltraLongCache
);
});
}
/// <summary>
/// Sposta record JobStep (up/down)
/// </summary>
/// <param name="selRec"></param>
/// <param name="moveUp"></param>
/// <returns></returns>
public async Task<bool> MoveAsync(JobStepModel selRec, bool moveUp)
{
return await TraceAsync($"{_className}.Move", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(selRec.JobStepID);
if (currRec == null) return false;
string operation = "Move";
bool success = await _repo.MoveAsync(selRec, moveUp);
if (currRec != null)
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobTaskList:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
/// <summary>
/// Upsert record JobStep (aggiorna o inserisce)
/// </summary>
/// <param name="upsRec"></param>
/// <returns></returns>
public async Task<bool> UpsertAsync(JobStepModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(upsRec.JobStepID);
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}*");
await ClearCacheAsync($"{_redisBaseKey}:JobTaskList:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IJobStepRepository _repo;
#endregion Private Fields
}
}