DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/EventService.cs
2021-12-07 18:27:52 +05:00

43 lines
1.3 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class EventService : IEventService
{
private readonly ITelemetryService telemetryService;
private readonly CacheTable<TelemetryEvent> cacheEvents;
public EventService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
{
this.telemetryService = telemetryService;
cacheEvents = cacheDb.GetCachedTable<TelemetryEvent>((AsbCloudDbContext)db);
}
public async Task UpsertAsync(string uid, IEnumerable<EventDto> dtos,
CancellationToken token = default)
{
if (!dtos.Any())
return;
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var entities = dtos.Select(dto => new TelemetryEvent
{
IdEvent = dto.Id,
IdTelemetry = telemetryId,
IdCategory = dto.IdCategory,
MessageTemplate = dto.Message
});
await cacheEvents.UpsertAsync(entities, token)
.ConfigureAwait(false);
}
}
}