93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
namespace EgwCoreLib.Lux.Data.Services.Cost
|
|
{
|
|
public class ResourceService : BaseServ, IResourceService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ResourceService(
|
|
IConfiguration config,
|
|
IConnectionMultiplexer redis,
|
|
IResourceRepository repo) : base(config, redis)
|
|
{
|
|
_className = "Resource";
|
|
_repo = repo;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(ResourceModel rec2del)
|
|
{
|
|
return await TraceAsync($"{_className}.Delete", async (activity) =>
|
|
{
|
|
var dbResult = await _repo.GetByIdAsync(rec2del.ResourceID);
|
|
if (dbResult == null) return false;
|
|
|
|
bool success = await _repo.DeleteAsync(dbResult);
|
|
|
|
if (success)
|
|
{
|
|
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
|
|
}
|
|
|
|
return success;
|
|
});
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<ResourceModel>> GetAllAsync()
|
|
{
|
|
return await TraceAsync($"{_className}.GetAllAsync", async (activity) =>
|
|
{
|
|
return await GetOrSetCacheAsync(
|
|
$"{_redisBaseKey}:{_className}:ALL",
|
|
async () => await _repo.GetAllAsync(),
|
|
UltraLongCache
|
|
);
|
|
});
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpsertAsync(ResourceModel upsRec)
|
|
{
|
|
return await TraceAsync($"{_className}.Upsert", async (activity) =>
|
|
{
|
|
var currRec = await _repo.GetByIdAsync(upsRec.ResourceID);
|
|
|
|
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 IResourceRepository _repo;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
}
|