DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/SAUB/EventService.cs

46 lines
1.3 KiB
C#
Raw Normal View History

using AsbCloudApp.Data.SAUB;
2021-04-23 10:21:25 +05:00
using AsbCloudApp.Services;
using AsbCloudDb;
2021-04-23 10:21:25 +05:00
using AsbCloudDb.Model;
using Microsoft.Extensions.Caching.Memory;
2021-04-23 10:21:25 +05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Services.SAUB;
public class EventService : IEventService
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly ITelemetryService telemetryService;
2024-08-19 10:01:07 +05:00
public EventService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
this.db = db;
this.memoryCache = memoryCache;
this.telemetryService = telemetryService;
}
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
public async Task UpsertAsync(string uid, IEnumerable<EventDto> dtos,
CancellationToken token = default)
{
if (!dtos.Any())
return;
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
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>();
2021-04-23 10:21:25 +05:00
}
}