86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
using Lux.Report.Data.DbModel;
|
|
using Lux.Report.Data.Repository;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Lux.Report.Data.Services
|
|
{
|
|
public class ReportService : BaseServ, IReportService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ReportService(
|
|
IConfiguration config,
|
|
IConnectionMultiplexer redis,
|
|
IReportRepository repo) : base(config, redis)
|
|
{
|
|
_className = "Report";
|
|
_repo = repo;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<ReportModel>> 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()
|
|
);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpsertAsync(ReportModel upsRec)
|
|
{
|
|
return await TraceAsync($"{_className}.Upsert", async (activity) =>
|
|
{
|
|
var currRec = await _repo.GetByIdAsync(upsRec.ReportID);
|
|
|
|
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 IReportRepository _repo;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
}
|