using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Threading; using System.Linq; namespace AsbCloudInfrastructure.Repository { public class SetpointsRequestRepository : CrudWellRelatedCacheRepositoryBase { private readonly IWellService wellService; public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService) : base(dbContext, memoryCache, q => q.Include(s => s.Author) .Include(s => s.Well)) { this.wellService = wellService; } public virtual async Task UpdateRangeAsync(IEnumerable dtos, CancellationToken token) { var ids = dtos.Select(d => d.Id); var existingEntities = await dbSet .AsNoTracking() .Where(d => ids.Contains(d.Id)) .Select(d => d.Id) .ToListAsync(token) .ConfigureAwait(false); if (ids.Count() > existingEntities.Count) return ICrudRepository.ErrorIdNotFound; foreach (var dto in dtos) { var entity = Convert(dto); var entry = dbSet.Update(entity); } var affected = await dbContext.SaveChangesAsync(token); return affected; } protected override SetpointsRequestDto Convert(SetpointsRequest src) { var result = base.Convert(src); var timezoneOffsetHours = wellService.GetTimezone(src.IdWell).Hours; result.UploadDate = src.UploadDate.ToRemoteDateTime(timezoneOffsetHours); return result; } protected override SetpointsRequest Convert(SetpointsRequestDto src) { var result = base.Convert(src); var timezoneOffsetHours = wellService.GetTimezone(src.IdWell).Hours; result.UploadDate = src.UploadDate.ToUtcDateTimeOffset(timezoneOffsetHours); return result; } } }