From 51d4e3010005c49e54c4509af8f58b97e7799abe Mon Sep 17 00:00:00 2001 From: "ai.astrakhantsev" Date: Wed, 19 Oct 2022 13:56:34 +0500 Subject: [PATCH 1/2] =?UTF-8?q?#7051313=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BD=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IWellCompositeRepository.cs} | 12 +-- AsbCloudInfrastructure/DependencyInjection.cs | 2 +- .../WellCompositeRepository.cs} | 22 ++--- .../WellCompositeRepositoryTest.cs | 80 +++++++++++++++++++ .../Controllers/WellCompositeController.cs | 13 +-- 5 files changed, 108 insertions(+), 21 deletions(-) rename AsbCloudApp/{Services/IWellCompositeService.cs => Repositories/IWellCompositeRepository.cs} (75%) rename AsbCloudInfrastructure/{Services/WellCompositeService.cs => Repository/WellCompositeRepository.cs} (70%) create mode 100644 AsbCloudWebApi.Tests/ServicesTests/WellCompositeRepositoryTest.cs diff --git a/AsbCloudApp/Services/IWellCompositeService.cs b/AsbCloudApp/Repositories/IWellCompositeRepository.cs similarity index 75% rename from AsbCloudApp/Services/IWellCompositeService.cs rename to AsbCloudApp/Repositories/IWellCompositeRepository.cs index 80088619..c14fd11c 100644 --- a/AsbCloudApp/Services/IWellCompositeService.cs +++ b/AsbCloudApp/Repositories/IWellCompositeRepository.cs @@ -3,20 +3,21 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudApp.Services +namespace AsbCloudApp.Repositories { +#nullable enable /// - /// Сервис создания композитной скважины + /// Репозиторий создания композитной скважины /// - public interface IWellCompositeService + public interface IWellCompositeRepository { /// /// Получить секции композитной скважины /// /// - /// + /// /// - Task> GetAsync(int idWell, CancellationToken cancellationToken); + Task> GetAsync(int idWell, CancellationToken token); /// /// сохранить секции композитной скважины @@ -27,4 +28,5 @@ namespace AsbCloudApp.Services /// Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token); } +#nullable disable } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 3f089a0c..f77735c7 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -123,7 +123,6 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -162,6 +161,7 @@ namespace AsbCloudInfrastructure .Include(c => c.Deposit))); // может быть включен в сервис ClusterService services.AddTransient(); services.AddTransient(); + services.AddTransient(); // Subsystem service services.AddTransient, CrudCacheServiceBase>(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/WellCompositeService.cs b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs similarity index 70% rename from AsbCloudInfrastructure/Services/WellCompositeService.cs rename to AsbCloudInfrastructure/Repository/WellCompositeRepository.cs index da532e2c..d6f6bc01 100644 --- a/AsbCloudInfrastructure/Services/WellCompositeService.cs +++ b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs @@ -1,5 +1,5 @@ using AsbCloudApp.Data; -using AsbCloudApp.Services; +using AsbCloudApp.Repositories; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; @@ -8,20 +8,21 @@ 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; } public async Task> GetAsync(int idWell, CancellationToken token) { - var entities = await context.WellComposites + var entities = await db.WellComposites .Where(c => c.IdWell == idWell) .AsNoTracking() .ToListAsync(token) @@ -31,14 +32,14 @@ namespace AsbCloudInfrastructure.Services public Task SaveAsync(int idWell, IEnumerable 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) @@ -53,4 +54,5 @@ namespace AsbCloudInfrastructure.Services return dto; } } +#nullable disable } diff --git a/AsbCloudWebApi.Tests/ServicesTests/WellCompositeRepositoryTest.cs b/AsbCloudWebApi.Tests/ServicesTests/WellCompositeRepositoryTest.cs new file mode 100644 index 00000000..ef4b7f55 --- /dev/null +++ b/AsbCloudWebApi.Tests/ServicesTests/WellCompositeRepositoryTest.cs @@ -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 wellCompositeRepository; + + private static List Data = new List { + 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(); + + wellCompositeRepository.Setup(x => x.GetAsync(It.IsAny(), It.IsAny())) + .Returns((int idWell, CancellationToken token) => { + var data = Data.Where(x => x.IdWell == idWell); + return Task.FromResult(data); + }); + + wellCompositeRepository.Setup(x => x.SaveAsync(It.IsAny(), It.IsAny>(), It.IsAny())) + .Returns((int idWell, IEnumerable 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 { + new WellCompositeDto { + IdWell = 4, + IdWellSrc = 44, + IdWellSectionType = 6 + } + }; + + var result = await wellCompositeRepository.Object.SaveAsync(4, dtos, CancellationToken.None); + Assert.True(cnt < Data.Count); + } + } +} diff --git a/AsbCloudWebApi/Controllers/WellCompositeController.cs b/AsbCloudWebApi/Controllers/WellCompositeController.cs index 398530d5..c6f5c9cf 100644 --- a/AsbCloudWebApi/Controllers/WellCompositeController.cs +++ b/AsbCloudWebApi/Controllers/WellCompositeController.cs @@ -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 /// /// Композитная скважина /// @@ -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 } From cc25e11c408c4c5b1a9c70c9c904525cf24a5d3e Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 19 Oct 2022 15:00:18 +0500 Subject: [PATCH 2/2] 7051313 nit --- .../Repository/WellCompositeRepository.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs index d6f6bc01..de56ee60 100644 --- a/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs @@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository this.db = db; } + /// public async Task> GetAsync(int idWell, CancellationToken token) { var entities = await db.WellComposites @@ -30,6 +31,7 @@ namespace AsbCloudInfrastructure.Repository return entities.Select(Convert); } + /// public Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token) { db.WellComposites.RemoveRange(db.WellComposites @@ -42,13 +44,13 @@ namespace AsbCloudInfrastructure.Repository return db.SaveChangesAsync(token); } - private WellComposite Convert(int idWell, WellCompositeDto dto) + private static WellComposite Convert(int idWell, WellCompositeDto dto) { var entity = dto.Adapt(); entity.IdWell = idWell; return entity; } - private WellCompositeDto Convert(WellComposite entity) + private static WellCompositeDto Convert(WellComposite entity) { var dto = entity.Adapt(); return dto;