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

61 lines
1.8 KiB
C#
Raw Normal View History

2021-10-12 12:17:46 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
2021-10-12 12:17:46 +05:00
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
2021-10-12 12:17:46 +05:00
{
#nullable enable
public class WellCompositeRepository : IWellCompositeRepository
2021-10-12 12:17:46 +05:00
{
private readonly IAsbCloudDbContext db;
2021-10-12 12:17:46 +05:00
public WellCompositeRepository(IAsbCloudDbContext db)
2021-10-12 12:17:46 +05:00
{
this.db = db;
2021-10-12 12:17:46 +05:00
}
2022-10-19 15:00:18 +05:00
/// <inheritdoc/>
2021-10-12 12:17:46 +05:00
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token)
{
var entities = await db.WellComposites
2021-10-12 12:17:46 +05:00
.Where(c => c.IdWell == idWell)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities.Select(Convert);
2021-10-12 12:17:46 +05:00
}
2022-10-19 15:00:18 +05:00
/// <inheritdoc/>
2021-10-12 12:17:46 +05:00
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
{
db.WellComposites.RemoveRange(db.WellComposites
2021-10-12 12:17:46 +05:00
.Where(c => c.IdWell == idWell));
2021-10-12 12:17:46 +05:00
var entities = wellComposites
.Select(w => Convert(idWell, w));
db.WellComposites.AddRange(entities);
return db.SaveChangesAsync(token);
2021-10-12 12:17:46 +05:00
}
2022-10-19 15:00:18 +05:00
private static WellComposite Convert(int idWell, WellCompositeDto dto)
{
var entity = dto.Adapt<WellComposite>();
entity.IdWell = idWell;
return entity;
}
2022-10-19 15:00:18 +05:00
private static WellCompositeDto Convert(WellComposite entity)
{
var dto = entity.Adapt<WellCompositeDto>();
return dto;
}
2021-10-12 12:17:46 +05:00
}
#nullable disable
2021-10-12 12:17:46 +05:00
}