Files

149 lines
4.9 KiB
C#

namespace EgwCoreLib.Lux.Data.Services.Items
{
public class SellingItemService : BaseServ, ISellingItemService
{
#region Public Constructors
public SellingItemService(
IConfiguration config,
IConnectionMultiplexer redis,
ISellingItemRepository repo) : base(config, redis)
{
_className = "SellingItem";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> DeleteAsync(SellingItemModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByIdAsync(rec2del.SellingItemID);
if (dbResult == null) return false;
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<List<SellingItemModel>> GetByEnvirAsync(Constants.EXECENVIRONMENTS envir)
{
// Uso helper TraceAsync che gestisce automaticamente StartActivity, Log e Exception tracking
return await TraceAsync($"{_className}.GetByEnvir", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:{envir}",
async () => await _repo.GetByEnvirAsync(envir),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<List<SellingItemModel>> GetFiltAsync(Constants.EXECENVIRONMENTS envir, ItemSourceType sourceType)
{
// Uso helper TraceAsync che gestisce automaticamente StartActivity, Log e Exception tracking
return await TraceAsync($"{_className}.GetFilt", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:{envir}:{sourceType}",
async () => await _repo.GetFiltAsync(envir, sourceType),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<bool> UpdateFileDataAsync(SellingItemModel updRec)
{
return await TraceAsync($"{_className}.UpdateFileData", async (activity) =>
{
// 1. Cerco se esiste già
var currRec = await _repo.GetByIdAsync(updRec.SellingItemID);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
currRec.FileName = updRec.FileName;
currRec.ImgType = updRec.ImgType;
currRec.SourceType = updRec.SourceType;
// check/fix tipo file..
if (currRec.ImgType == ImageType.ND)
{
if (currRec.SourceType == ItemSourceType.Jwd || updRec.SourceType == ItemSourceType.FileBTL)
{
currRec.ImgType = ImageType.Calculated;
}
else
{
currRec.ImgType = ImageType.Fixed;
}
}
success = await _repo.UpdateAsync(currRec);
}
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<bool> UpsertAsync(SellingItemModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
// 1. Cerco se esiste già
var currRec = await _repo.GetByIdAsync(upsRec.SellingItemID);
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 ISellingItemRepository _repo;
#endregion Private Fields
}
}