forked from ddrilling/AsbCloudServer
65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
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 : CrudWellRelatedCacheServiceBase<SetpointsRequestDto, SetpointsRequest>
|
|
{
|
|
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<int> UpdateRangeAsync(IEnumerable<SetpointsRequestDto> 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<SetpointsRequestDto>.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;
|
|
}
|
|
}
|
|
}
|