DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/TelemetryUserService.cs
2021-07-21 15:29:19 +05:00

42 lines
1.4 KiB
C#

using AsbCloudApp.Comparators;
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 TelemetryUserService : ITelemetryUserService
{
private readonly ITelemetryService telemetryService;
private readonly CacheTable<TelemetryUser> cacheTelemetryUsers;
public TelemetryUserService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
{
this.telemetryService = telemetryService;
cacheTelemetryUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
}
public void Upsert(string uid, IEnumerable<TelemetryUserDto> dtos)
{
if (!dtos.Any())
return;
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
var entities = dtos.Distinct(new TelemetryUserDtoComparer()).Select(dto => new TelemetryUser
{
IdUser = dto.Id,
IdTelemetry = telemetryId,
Level = dto.Level,
Name = dto.Name,
Patronymic = dto.Patronymic,
Surname = dto.Surname,
});
cacheTelemetryUsers.Upsert(entities);
}
}
}