2021-10-12 12:17:46 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-10-19 13:56:34 +05:00
|
|
|
|
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;
|
|
|
|
|
|
2022-10-19 13:56:34 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
2021-10-12 12:17:46 +05:00
|
|
|
|
{
|
2022-10-19 13:56:34 +05:00
|
|
|
|
#nullable enable
|
|
|
|
|
public class WellCompositeRepository : IWellCompositeRepository
|
2021-10-12 12:17:46 +05:00
|
|
|
|
{
|
2022-10-19 13:56:34 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-10-12 12:17:46 +05:00
|
|
|
|
|
2022-10-19 13:56:34 +05:00
|
|
|
|
public WellCompositeRepository(IAsbCloudDbContext db)
|
2021-10-12 12:17:46 +05:00
|
|
|
|
{
|
2022-10-19 13:56:34 +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)
|
|
|
|
|
{
|
2022-10-19 13:56:34 +05:00
|
|
|
|
var entities = await db.WellComposites
|
2021-10-12 12:17:46 +05:00
|
|
|
|
.Where(c => c.IdWell == idWell)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2022-06-06 15:43:47 +05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-10-19 13:56:34 +05:00
|
|
|
|
db.WellComposites.RemoveRange(db.WellComposites
|
2021-10-12 12:17:46 +05:00
|
|
|
|
.Where(c => c.IdWell == idWell));
|
2021-10-14 14:46:20 +05:00
|
|
|
|
|
2021-10-12 12:17:46 +05:00
|
|
|
|
var entities = wellComposites
|
2022-06-06 15:43:47 +05:00
|
|
|
|
.Select(w => Convert(idWell, w));
|
2021-10-14 14:46:20 +05:00
|
|
|
|
|
2022-10-19 13:56:34 +05:00
|
|
|
|
db.WellComposites.AddRange(entities);
|
|
|
|
|
return db.SaveChangesAsync(token);
|
2021-10-12 12:17:46 +05:00
|
|
|
|
}
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2022-10-19 15:00:18 +05:00
|
|
|
|
private static WellComposite Convert(int idWell, WellCompositeDto dto)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<WellComposite>();
|
|
|
|
|
entity.IdWell = idWell;
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
2022-10-19 15:00:18 +05:00
|
|
|
|
private static WellCompositeDto Convert(WellComposite entity)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<WellCompositeDto>();
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
2021-10-12 12:17:46 +05:00
|
|
|
|
}
|
2022-10-19 13:56:34 +05:00
|
|
|
|
#nullable disable
|
2021-10-12 12:17:46 +05:00
|
|
|
|
}
|