DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/SAUB/EventService.cs
2023-06-20 14:41:19 +05:00

47 lines
1.4 KiB
C#

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Services;
using AsbCloudDb;
using AsbCloudDb.Model;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.SAUB
{
public class EventService : IEventService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly ITelemetryService telemetryService;
public EventService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
{
this.db = db;
this.memoryCache = memoryCache;
this.telemetryService = telemetryService;
}
public async Task UpsertAsync(string uid, IEnumerable<EventDto> dtos,
CancellationToken token = default)
{
if (!dtos.Any())
return;
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var entities = dtos.Select(dto => new TelemetryEvent
{
IdEvent = dto.Id,
IdTelemetry = telemetry.Id,
IdCategory = dto.IdCategory,
MessageTemplate = dto.Message
});
var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token);
memoryCache.DropBasic<TelemetryEvent>();
}
}
}