using AsbCloudApp.Comparators; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudInfrastructure.EfCache; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.SAUB { public class TelemetryUserService : ITelemetryUserService { private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; public TelemetryUserService(IAsbCloudDbContext db, ITelemetryService telemetryService) { this.db = db; this.telemetryService = telemetryService; } public async Task UpsertAsync(string uid, IEnumerable dtos, CancellationToken token = default) { if (!dtos.Any()) return; var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(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, }); var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryUsers, entities, token); db.TelemetryUsers.DropCache(); } } }