Files
mapo-core/MP.Data/Services/Mtc/MtcSetupService.cs
T
Samuele Locatelli 9055eaf73c SPEC:
- aggiunta pagina operatori
- completato fix
2026-06-03 18:05:59 +02:00

78 lines
2.4 KiB
C#

using Microsoft.Extensions.Configuration;
using MP.Core.Objects;
using MP.Data.DbModels.Mtc;
using MP.Data.Repository.Mtc;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZiggyCreatures.Caching.Fusion;
namespace MP.Data.Services.Mtc
{
// A lightweight service that mirrors the old Mongo based archive
public class MtcSetupService : BaseServ, IMtcSetupService
{
private readonly string _className;
private readonly IMtcSetupRepository _repo;
public MtcSetupService(
IConfiguration config,
IConnectionMultiplexer redis,
IFusionCache cache,
IMtcSetupRepository repo) : base(config, cache, redis)
{
_className = "MtcSetup";
_repo = repo;
}
/// <inheritdoc />
public async Task<bool> ReplaceMachineDataAsync(string idxMacchina, List<MachDataItem> items)
{
return await TraceAsync($"{_className}.ReplaceMachineData", async (activity) =>
{
// cerso sul DB
var currRec = await _repo.GetByIdxAsync(idxMacchina);
var upsRec = new MtcSetupModel
{
IdxMacchina = idxMacchina,
MtcDataItems = items
};
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
success = await _repo.UpdateAsync(upsRec);
}
else
{
operation = "INSERT";
success = await _repo.InsertAsync(upsRec);
}
activity?.SetTag("db.operation", operation);
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
return success;
});
}
/// <inheritdoc />
public async Task<MtcSetupModel> GetMachineDataAsync(string idxMacchina)
{
return await TraceAsync($"{_className}.GetMachineData", async (activity) =>
{
string cacheKey = $"{_redisBaseKey}:{_className}:MachData:{idxMacchina}";
return await GetOrSetCacheAsync(
cacheKey,
async () => await _repo.GetByIdxAsync(idxMacchina),
LongCache
);
});
}
}
}