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 60% rename from AsbCloudInfrastructure/Services/WellCompositeService.cs rename to AsbCloudInfrastructure/Repository/WellCompositeRepository.cs index da532e2c..de56ee60 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,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; } + /// 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) @@ -29,28 +31,30 @@ namespace AsbCloudInfrastructure.Services return entities.Select(Convert); } + /// 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) + 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; } } +#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 }