using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMaps; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository { public class WellCompositeRepository : IWellCompositeRepository { private readonly IAsbCloudDbContext db; private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository; public WellCompositeRepository(IAsbCloudDbContext db, IWellDrillingProcessMapRepository wellDrillingProcessMapRepository) { this.db = db; this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository; } /// public async Task> GetAsync(int idWell, CancellationToken token) { var entities = await db.WellComposites .Where(c => c.IdWell == idWell) .AsNoTracking() .ToListAsync(token) .ConfigureAwait(false); return entities.Select(Convert); } /// public Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token) { db.WellComposites.RemoveRange(db.WellComposites .Where(c => c.IdWell == idWell)); var entities = wellComposites .Select(w => Convert(idWell, w)); db.WellComposites.AddRange(entities); return db.SaveChangesAsync(token); } /// public async Task> GetCompositeProcessMap(int idWell, CancellationToken token) { var dtos = await GetAsync(idWell, token); var requests = dtos.Select(x => new WellDrillingProcessMapRequest { IdWell = x.IdWellSrc, IdWellSectionType = x.IdWellSectionType }); var result = await wellDrillingProcessMapRepository.GetAsync(requests, token); return result; } private static WellComposite Convert(int idWell, WellCompositeDto dto) { var entity = dto.Adapt(); entity.IdWell = idWell; return entity; } private static WellCompositeDto Convert(WellComposite entity) { var dto = entity.Adapt(); return dto; } } }