#7051313 Перенос создания композитной скважины в репозиторий

This commit is contained in:
ai.astrakhantsev 2022-10-19 13:56:34 +05:00
parent 1f5867e4b3
commit 51d4e30100
5 changed files with 108 additions and 21 deletions

View File

@ -3,20 +3,21 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudApp.Services namespace AsbCloudApp.Repositories
{ {
#nullable enable
/// <summary> /// <summary>
/// Сервис создания композитной скважины /// Репозиторий создания композитной скважины
/// </summary> /// </summary>
public interface IWellCompositeService public interface IWellCompositeRepository
{ {
/// <summary> /// <summary>
/// Получить секции композитной скважины /// Получить секции композитной скважины
/// </summary> /// </summary>
/// <param name="idWell"></param> /// <param name="idWell"></param>
/// <param name="cancellationToken"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken cancellationToken); Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token);
/// <summary> /// <summary>
/// сохранить секции композитной скважины /// сохранить секции композитной скважины
@ -27,4 +28,5 @@ namespace AsbCloudApp.Services
/// <returns></returns> /// <returns></returns>
Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token); 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<IUserService, UserService>();
services.AddTransient<IUserRoleService, UserRoleService>(); services.AddTransient<IUserRoleService, UserRoleService>();
services.AddTransient<IWellService, WellService>(); services.AddTransient<IWellService, WellService>();
services.AddTransient<IWellCompositeService, WellCompositeService>();
services.AddTransient<IWellOperationImportService, WellOperationImportService>(); services.AddTransient<IWellOperationImportService, WellOperationImportService>();
services.AddTransient<IWellOperationService, WellOperationService>(); services.AddTransient<IWellOperationService, WellOperationService>();
services.AddTransient<IScheduleReportService, ScheduleReportService>(); services.AddTransient<IScheduleReportService, ScheduleReportService>();
@ -162,6 +161,7 @@ namespace AsbCloudInfrastructure
.Include(c => c.Deposit))); // может быть включен в сервис ClusterService .Include(c => c.Deposit))); // может быть включен в сервис ClusterService
services.AddTransient<IFileRepository, FileRepository>(); services.AddTransient<IFileRepository, FileRepository>();
services.AddTransient<IFileStorageRepository, FileStorageRepository>(); services.AddTransient<IFileStorageRepository, FileStorageRepository>();
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
// Subsystem service // Subsystem service
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>(); services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
services.AddTransient<ISubsystemService, SubsystemService>(); services.AddTransient<ISubsystemService, SubsystemService>();

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Repositories;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -8,20 +8,21 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; 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;
} }
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token) 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) .Where(c => c.IdWell == idWell)
.AsNoTracking() .AsNoTracking()
.ToListAsync(token) .ToListAsync(token)
@ -31,14 +32,14 @@ namespace AsbCloudInfrastructure.Services
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token) 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)); .Where(c => c.IdWell == idWell));
var entities = wellComposites var entities = wellComposites
.Select(w => Convert(idWell, w)); .Select(w => Convert(idWell, w));
context.WellComposites.AddRange(entities); db.WellComposites.AddRange(entities);
return context.SaveChangesAsync(token); return db.SaveChangesAsync(token);
} }
private WellComposite Convert(int idWell, WellCompositeDto dto) private WellComposite Convert(int idWell, WellCompositeDto dto)
@ -53,4 +54,5 @@ namespace AsbCloudInfrastructure.Services
return dto; 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.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -8,6 +9,7 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers namespace AsbCloudWebApi.Controllers
{ {
#nullable enable
/// <summary> /// <summary>
/// Композитная скважина /// Композитная скважина
/// </summary> /// </summary>
@ -16,12 +18,12 @@ namespace AsbCloudWebApi.Controllers
[Authorize] [Authorize]
public class WellCompositeController : ControllerBase public class WellCompositeController : ControllerBase
{ {
private readonly IWellCompositeService wellCompositeService; private readonly IWellCompositeRepository wellCompositeRepository;
private readonly IWellService wellService; 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; this.wellService = wellService;
} }
@ -39,7 +41,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await wellCompositeService.GetAsync(idWell, token).ConfigureAwait(false); var result = await wellCompositeRepository.GetAsync(idWell, token).ConfigureAwait(false);
return Ok(result); return Ok(result);
} }
@ -57,7 +59,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid(); 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); return Ok(result);
} }
@ -68,4 +70,5 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false); idWell, token).ConfigureAwait(false);
} }
} }
#nullable disable
} }