Merge branch 'feature/well_composite_repository' into dev

This commit is contained in:
ngfrolov 2022-10-19 15:01:56 +05:00
commit 501ea8efde
5 changed files with 112 additions and 23 deletions

View File

@ -3,20 +3,21 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Services
namespace AsbCloudApp.Repositories
{
#nullable enable
/// <summary>
/// Сервис создания композитной скважины
/// Репозиторий создания композитной скважины
/// </summary>
public interface IWellCompositeService
public interface IWellCompositeRepository
{
/// <summary>
/// Получить секции композитной скважины
/// </summary>
/// <param name="idWell"></param>
/// <param name="cancellationToken"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken cancellationToken);
Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token);
/// <summary>
/// сохранить секции композитной скважины
@ -27,4 +28,5 @@ namespace AsbCloudApp.Services
/// <returns></returns>
Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token);
}
#nullable disable
}

View File

@ -123,7 +123,6 @@ namespace AsbCloudInfrastructure
services.AddTransient<IUserService, UserService>();
services.AddTransient<IUserRoleService, UserRoleService>();
services.AddTransient<IWellService, WellService>();
services.AddTransient<IWellCompositeService, WellCompositeService>();
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
services.AddTransient<IWellOperationService, WellOperationService>();
services.AddTransient<IScheduleReportService, ScheduleReportService>();
@ -162,6 +161,7 @@ namespace AsbCloudInfrastructure
.Include(c => c.Deposit))); // может быть включен в сервис ClusterService
services.AddTransient<IFileRepository, FileRepository>();
services.AddTransient<IFileStorageRepository, FileStorageRepository>();
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
// Subsystem service
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
services.AddTransient<ISubsystemService, SubsystemService>();

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
@ -8,20 +8,22 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
namespace AsbCloudInfrastructure.Repository
{
public class WellCompositeService : IWellCompositeService
#nullable enable
public class WellCompositeRepository : IWellCompositeRepository
{
private readonly IAsbCloudDbContext context;
private readonly IAsbCloudDbContext db;
public WellCompositeService(IAsbCloudDbContext context)
public WellCompositeRepository(IAsbCloudDbContext db)
{
this.context = context;
this.db = db;
}
/// <inheritdoc/>
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token)
{
var entities = await context.WellComposites
var entities = await db.WellComposites
.Where(c => c.IdWell == idWell)
.AsNoTracking()
.ToListAsync(token)
@ -29,28 +31,30 @@ namespace AsbCloudInfrastructure.Services
return entities.Select(Convert);
}
/// <inheritdoc/>
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
{
context.WellComposites.RemoveRange(context.WellComposites
db.WellComposites.RemoveRange(db.WellComposites
.Where(c => c.IdWell == idWell));
var entities = wellComposites
.Select(w => Convert(idWell, w));
context.WellComposites.AddRange(entities);
return context.SaveChangesAsync(token);
db.WellComposites.AddRange(entities);
return db.SaveChangesAsync(token);
}
private WellComposite Convert(int idWell, WellCompositeDto dto)
private static WellComposite Convert(int idWell, WellCompositeDto dto)
{
var entity = dto.Adapt<WellComposite>();
entity.IdWell = idWell;
return entity;
}
private WellCompositeDto Convert(WellComposite entity)
private static WellCompositeDto Convert(WellComposite entity)
{
var dto = entity.Adapt<WellCompositeDto>();
return dto;
}
}
#nullable disable
}

View File

@ -0,0 +1,80 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests
{
public class WellCompositeRepositoryTest
{
private readonly Mock<IWellCompositeRepository> wellCompositeRepository;
private static List<WellCompositeDto> Data = new List<WellCompositeDto> {
new WellCompositeDto {
IdWell = 90,
IdWellSrc = 4,
IdWellSectionType = 2
},
new WellCompositeDto
{
IdWell = 90,
IdWellSrc = 4,
IdWellSectionType = 3
},
new WellCompositeDto {
IdWell = 90,
IdWellSrc = 44,
IdWellSectionType = 6
},
new WellCompositeDto {
IdWell = 4,
IdWellSrc = 4,
IdWellSectionType = 6
}
};
public WellCompositeRepositoryTest()
{
wellCompositeRepository = new Mock<IWellCompositeRepository>();
wellCompositeRepository.Setup(x => x.GetAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns((int idWell, CancellationToken token) => {
var data = Data.Where(x => x.IdWell == idWell);
return Task.FromResult(data);
});
wellCompositeRepository.Setup(x => x.SaveAsync(It.IsAny<int>(), It.IsAny<IEnumerable<WellCompositeDto>>(), It.IsAny<CancellationToken>()))
.Returns((int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token) => {
Data.AddRange(wellComposites);
return Task.FromResult(Data.Count);
});
}
[Fact]
public async Task GetAsync_returns_WellCompositeDto()
{
var data = await wellCompositeRepository.Object.GetAsync(90, CancellationToken.None);
Assert.NotNull(data);
}
[Fact]
public async Task SaveAsync()
{
var cnt = Data.Count;
var dtos = new List<WellCompositeDto> {
new WellCompositeDto {
IdWell = 4,
IdWellSrc = 44,
IdWellSectionType = 6
}
};
var result = await wellCompositeRepository.Object.SaveAsync(4, dtos, CancellationToken.None);
Assert.True(cnt < Data.Count);
}
}
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -8,6 +9,7 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// Композитная скважина
/// </summary>
@ -16,12 +18,12 @@ namespace AsbCloudWebApi.Controllers
[Authorize]
public class WellCompositeController : ControllerBase
{
private readonly IWellCompositeService wellCompositeService;
private readonly IWellCompositeRepository wellCompositeRepository;
private readonly IWellService wellService;
public WellCompositeController(IWellCompositeService wellCompositeService, IWellService wellService)
public WellCompositeController(IWellCompositeRepository wellCompositeRepository, IWellService wellService)
{
this.wellCompositeService = wellCompositeService;
this.wellCompositeRepository = wellCompositeRepository;
this.wellService = wellService;
}
@ -39,7 +41,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellCompositeService.GetAsync(idWell, token).ConfigureAwait(false);
var result = await wellCompositeRepository.GetAsync(idWell, token).ConfigureAwait(false);
return Ok(result);
}
@ -57,7 +59,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellCompositeService.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false);
var result = await wellCompositeRepository.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false);
return Ok(result);
}
@ -68,4 +70,5 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false);
}
}
#nullable disable
}