forked from ddrilling/AsbCloudServer
57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class EventService : IEventService
|
|
{
|
|
private readonly IAsbCloudDbContext db;
|
|
private readonly ITelemetryService telemetryService;
|
|
private readonly ICacheTable<Event> cacheEvents;
|
|
|
|
public EventService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
|
|
{
|
|
this.db = db;
|
|
this.telemetryService = telemetryService;
|
|
cacheEvents = cacheDb.GetCachedTable<Event>((AsbCloudDbContext)db);
|
|
}
|
|
|
|
public void Upsert(string uid, IEnumerable<EventDto> dtos)
|
|
{
|
|
if (dtos.Count() == 0)
|
|
return;
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
|
|
|
|
var ids = dtos.Select(e => e.Id).ToList();
|
|
|
|
var dbIds = (from e in db.Events
|
|
where e.IdTelemetry == telemetryId && ids.Contains(e.IdEvent)
|
|
select e.IdEvent).ToList();
|
|
|
|
foreach (var dto in dtos)
|
|
{
|
|
var entity = new Event
|
|
{
|
|
IdEvent = dto.Id,
|
|
IdTelemetry = telemetryId,
|
|
IdCategory = dto.IdCategory,
|
|
MessageTemplate = dto.Message
|
|
};
|
|
|
|
if (dbIds.Contains(dto.Id))
|
|
db.Events.Update(entity);
|
|
else
|
|
db.Events.Add(entity);
|
|
}
|
|
|
|
db.SaveChanges();
|
|
cacheEvents.Refresh();
|
|
}
|
|
}
|
|
}
|