DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs

59 lines
1.7 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class WellCompositeRepository : IWellCompositeRepository
{
private readonly IAsbCloudDbContext db;
public WellCompositeRepository(IAsbCloudDbContext db)
{
this.db = db;
}
public async Task<IEnumerable<WellCompositeDto>> 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<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> 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);
}
private WellComposite Convert(int idWell, WellCompositeDto dto)
{
var entity = dto.Adapt<WellComposite>();
entity.IdWell = idWell;
return entity;
}
private WellCompositeDto Convert(WellComposite entity)
{
var dto = entity.Adapt<WellCompositeDto>();
return dto;
}
}
#nullable disable
}