From 4a4b7852b7daf8cff1b13790fa1bde7b9eaf835b Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 23 Dec 2022 14:35:23 +0500 Subject: [PATCH] Remove ClusterController, ClusterService. --- .../Repositories/IDepositRepository.cs | 44 +++++ AsbCloudApp/Services/IClusterService.cs | 62 ------- AsbCloudInfrastructure/DependencyInjection.cs | 2 +- .../DepositRepository.cs} | 74 +++----- .../ServicesTests/ClusterServiceTest.cs | 170 ------------------ .../Controllers/ClusterController.cs | 67 ------- .../Controllers/DepositController.cs | 14 +- 7 files changed, 72 insertions(+), 361 deletions(-) create mode 100644 AsbCloudApp/Repositories/IDepositRepository.cs delete mode 100644 AsbCloudApp/Services/IClusterService.cs rename AsbCloudInfrastructure/{Services/ClusterService.cs => Repository/DepositRepository.cs} (65%) delete mode 100644 AsbCloudWebApi.Tests/ServicesTests/ClusterServiceTest.cs delete mode 100644 AsbCloudWebApi/Controllers/ClusterController.cs diff --git a/AsbCloudApp/Repositories/IDepositRepository.cs b/AsbCloudApp/Repositories/IDepositRepository.cs new file mode 100644 index 00000000..44c5fe1d --- /dev/null +++ b/AsbCloudApp/Repositories/IDepositRepository.cs @@ -0,0 +1,44 @@ +using AsbCloudApp.Data; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Repositories +{ +#nullable enable + /// + /// Сервис информации о кустах + /// + public interface IDepositRepository + { + /// + /// список месторождений, доступных для пользователя + /// + /// + /// + /// + Task> GetAsync(int idCompany, + CancellationToken token); + + /// + /// Список месторождений/кустов/скважин у которых заполненны параметры бурения + /// + /// + /// + /// + Task> GetAllWithDrillParamsAsync(int idCompany, + CancellationToken token = default); + + /// + /// Список кустов месторождения доступных компании + /// + /// + /// + /// + /// + Task> GetClustersAsync(int idCompany, + int depositId, CancellationToken token); + + } +#nullable disable +} diff --git a/AsbCloudApp/Services/IClusterService.cs b/AsbCloudApp/Services/IClusterService.cs deleted file mode 100644 index 5a77d46d..00000000 --- a/AsbCloudApp/Services/IClusterService.cs +++ /dev/null @@ -1,62 +0,0 @@ -using AsbCloudApp.Data; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services -{ - //TODO: remove unused methods - - /// - /// Сервис информации о кустах - /// - public interface IClusterService - { - /// - /// список месторождений - /// - /// - /// - /// - Task> GetDepositsAsync(int idCompany, - CancellationToken token); - - /// - /// Список кустов с заполненными параметрами бурения - /// - /// - /// - /// - Task> GetDepositsDrillParamsAsync(int idCompany, - CancellationToken token = default); - - /// - /// Список кустов месторождения доступных компании - /// - /// - /// - /// - /// - Task> GetClustersAsync(int idCompany, - int depositId, CancellationToken token); - - /// - /// Список кустов доступных компании - /// - /// - /// - /// - Task> GetClustersAsync(int idCompany, - CancellationToken token); - - /// - /// Список скважин доступных компании - /// - /// - /// - /// - /// - Task> GetWellsAsync(int idCompany, - int clusterId, CancellationToken token); - } -} diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 34311da8..138a8644 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -108,7 +108,7 @@ namespace AsbCloudInfrastructure services.AddSingleton(provider => ReduceSamplingService.GetInstance(configuration)); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/ClusterService.cs b/AsbCloudInfrastructure/Repository/DepositRepository.cs similarity index 65% rename from AsbCloudInfrastructure/Services/ClusterService.cs rename to AsbCloudInfrastructure/Repository/DepositRepository.cs index 93fe292c..e937c271 100644 --- a/AsbCloudInfrastructure/Services/ClusterService.cs +++ b/AsbCloudInfrastructure/Repository/DepositRepository.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; @@ -8,20 +9,22 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Services +namespace AsbCloudInfrastructure.Repository { - public class ClusterService : IClusterService +#nullable enable + public class DepositRepository : IDepositRepository { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; - public ClusterService(IAsbCloudDbContext db, IWellService wellService) + public DepositRepository(IAsbCloudDbContext db, IWellService wellService) { this.db = db; this.wellService = wellService; } - public async Task> GetDepositsAsync(int idCompany, + /// + public async Task> GetAsync(int idCompany, CancellationToken token = default) { var wellEntities = await (from well in db.Wells @@ -40,7 +43,8 @@ namespace AsbCloudInfrastructure.Services return dtos; } - public async Task> GetDepositsDrillParamsAsync(int idCompany, + /// + public async Task> GetAllWithDrillParamsAsync(int idCompany, CancellationToken token = default) { var wellEntities = await (from well in db.Wells @@ -59,21 +63,7 @@ namespace AsbCloudInfrastructure.Services return dtos; } - public async Task> GetClustersAsync(int idCompany, - CancellationToken token = default) - { - var entities = await GetWellsForCompany(idCompany) - .Select(e => e.Cluster) - .Distinct() - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - var dtos = entities.Adapt>(); - - return dtos; - } - + /// public async Task> GetClustersAsync(int idCompany, int depositId, CancellationToken token = default) { @@ -90,48 +80,22 @@ namespace AsbCloudInfrastructure.Services return dtos; } - public async Task> GetWellsAsync(int idCompany, - int idCluster, CancellationToken token = default) - { - var entities = await GetWellsForCompany(idCompany) - .Where(e => e.IdCluster == idCluster) - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - var dtos = entities.Select(e => new WellDto - { - Id = e.Id, - Caption = e.Caption, - Latitude = e.Latitude, - Longitude = e.Longitude, - Cluster = e.Cluster.Caption, - Deposit = e.Cluster.Deposit.Caption, - }); - - return dtos; - } - private static IEnumerable>> GroupWells(IEnumerable wellEntities) - { - return wellEntities + => wellEntities .GroupBy(w => w.Cluster) .GroupBy(c => c.Key.Deposit); - } private IQueryable GetWellsForCompany(int idCompany) - { - return db.Wells - .Include(w => w.RelationCompaniesWells) - .ThenInclude(r => r.Company) - .Include(w => w.Cluster) - .ThenInclude(c => c.Deposit) - .Where(w => w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany)); - } + => db.Wells + .Include(w => w.RelationCompaniesWells) + .ThenInclude(r => r.Company) + .Include(w => w.Cluster) + .ThenInclude(c => c.Deposit) + .Where(w => w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany)); private IEnumerable CreateDepositDto(IEnumerable>> gDepositEntities) { - return gDepositEntities.Select(gDeposit => new DepositDto + var dtos = gDepositEntities.Select(gDeposit => new DepositDto { Id = gDeposit.Key.Id, Caption = gDeposit.Key.Caption, @@ -154,6 +118,8 @@ namespace AsbCloudInfrastructure.Services }), }), }); + return dtos; } } +#nullable disable } diff --git a/AsbCloudWebApi.Tests/ServicesTests/ClusterServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/ClusterServiceTest.cs deleted file mode 100644 index 3c1ff649..00000000 --- a/AsbCloudWebApi.Tests/ServicesTests/ClusterServiceTest.cs +++ /dev/null @@ -1,170 +0,0 @@ -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using AsbCloudInfrastructure.Services; -using Moq; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Xunit; - -namespace AsbCloudWebApi.Tests.ServicesTests; - -public class ClusterServiceTest -{ - private readonly AsbCloudDbContext context; - private readonly Mock wellService; - - private readonly List deposits = new() - { - new Deposit { Id = 1, Caption = "Test deposit 1" }, - new Deposit { Id = 2, Caption = "Test deposit 2" }, - new Deposit { Id = 3, Caption = "Test deposit 3" }, - new Deposit { Id = 4, Caption = "Test deposit 4" } - }; - - private readonly List clusters = new() - { - new Cluster { Id = 1, Caption = "Test cluster 1", IdDeposit = 1, Timezone = new SimpleTimezone()}, - new Cluster { Id = 2, Caption = "Test cluster 2", IdDeposit = 1, Timezone = new SimpleTimezone() }, - new Cluster { Id = 3, Caption = "Test cluster 3", IdDeposit = 2, Timezone = new SimpleTimezone() }, - new Cluster { Id = 4, Caption = "Test cluster 4", IdDeposit = 2, Timezone = new SimpleTimezone() } - }; - - private readonly List wells = new() - { - new Well { Id = 1, Caption = "Test well 1", IdCluster = 1 }, - new Well { Id = 2, Caption = "Test well 2", IdCluster = 2 }, - new Well { Id = 3, Caption = "Test well 3", IdCluster = 1 }, - new Well { Id = 4, Caption = "Test well 4", IdCluster = 2 } - }; - - private readonly List companiesTypes = new() - { - new CompanyType { Id = 1, Caption = "test company type"} - }; - - private readonly List companies = new() - { - new Company { Id = 1, Caption = "Test company 1", IdCompanyType = 1}, - new Company { Id = 2, Caption = "Test company 2", IdCompanyType = 1} - }; - - private readonly List relations = new() - { - new RelationCompanyWell { IdCompany = 1, IdWell = 1 }, - new RelationCompanyWell { IdCompany = 1, IdWell = 2 }, - new RelationCompanyWell { IdCompany = 2, IdWell = 2 } - }; - - private readonly List wellSectionTypes = new() - { - new WellSectionType { Id = 1, Caption = "Test well section type 1" } - }; - - public ClusterServiceTest() - { - context = TestHelpter.MakeRealTestContext(); - wellService = new Mock(); - context.Deposits.RemoveRange(context.Deposits); - context.Clusters.RemoveRange(context.Clusters); - context.Wells.RemoveRange(context.Wells); - context.CompaniesTypes.RemoveRange(context.CompaniesTypes); - context.Companies.RemoveRange(context.Companies); - context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells); - context.WellSectionTypes.RemoveRange(context.WellSectionTypes); - if (context.ChangeTracker.HasChanges()) - context.SaveChanges(); - context.Deposits.AddRange(deposits); - context.Clusters.AddRange(clusters); - context.Wells.AddRange(wells); - context.CompaniesTypes.AddRange(companiesTypes); - context.Companies.AddRange(companies); - context.RelationCompaniesWells.AddRange(relations); - context.WellSectionTypes.AddRange(wellSectionTypes); - context.SaveChanges(); - } - - ~ClusterServiceTest() - { - context.Deposits.RemoveRange(context.Deposits); - context.Clusters.RemoveRange(context.Clusters); - context.Wells.RemoveRange(context.Wells); - context.CompaniesTypes.RemoveRange(context.CompaniesTypes); - context.Companies.RemoveRange(context.Companies); - context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells); - context.SaveChanges(); - } - - [Fact] - public async Task GetDepositsAsync_returns_one_deposit() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetDepositsAsync(1); - - Assert.Single(dtos); - } - - [Fact] - public async Task GetDepositsAsync_returns_one_deposit_with_two_clusters() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetDepositsAsync(1); - - Assert.Equal(2, dtos.FirstOrDefault()?.Clusters.Count()); - } - [Fact] - public async Task GetDrillParamsAsync_returns_depositDtos() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetDepositsDrillParamsAsync(1); - - Assert.True(dtos.Any()); - } - - [Fact] - public async Task GetDrillParamsAsync_returns_one_deposit() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetDepositsDrillParamsAsync(1); - - Assert.Single(dtos); - } - - [Fact] - public async Task GetDrillParamsAsync_returns_one_deposit_with_two_clusters() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetDepositsDrillParamsAsync(1); - - Assert.Equal(2, dtos.FirstOrDefault()?.Clusters.Count()); - } - - [Fact] - public async Task GetClustersAsync_returns_two_dtos() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetClustersAsync(1); - - Assert.Equal(2, dtos.Count()); - } - - [Fact] - public async Task GetClustersAsync_with_deposit_returns_two_clusters() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetClustersAsync(1, 1); - - Assert.Equal(2, dtos.Count()); - } - - [Fact] - public async Task GetWellsAsync_returns_one_well_by_cluster_and_company() - { - var service = new ClusterService(context, wellService.Object); - var dtos = await service.GetWellsAsync(1, 1); - - Assert.Single(dtos); - } - - -} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/ClusterController.cs b/AsbCloudWebApi/Controllers/ClusterController.cs deleted file mode 100644 index 981dd5b6..00000000 --- a/AsbCloudWebApi/Controllers/ClusterController.cs +++ /dev/null @@ -1,67 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudWebApi.Controllers -{ - /// - /// Инфо о кустах - /// - [Route("api/cluster")] - [ApiController] - [Authorize] - public class ClusterController : ControllerBase - { - private readonly IClusterService clusterService; - - public ClusterController(IClusterService clusterService) - { - this.clusterService = clusterService; - } - - /// - /// Получает список доступных пользователю кустов - /// - /// Токен отмены задачи - /// - [HttpGet()] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetClustersAsync(CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null) - return Forbid(); - - var result = await clusterService.GetClustersAsync((int)idCompany, - token).ConfigureAwait(false); - return Ok(result); - } - - /// - /// Получение доступных пользователю скважин - /// - /// - /// Токен отмены задачи - /// - [HttpGet("{idCluster}")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetWellsAsync(int idCluster, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null) - return Forbid(); - - var result = await clusterService.GetWellsAsync((int)idCompany, - idCluster, token).ConfigureAwait(false); - return Ok(result); - } - } -} diff --git a/AsbCloudWebApi/Controllers/DepositController.cs b/AsbCloudWebApi/Controllers/DepositController.cs index 9fd4d03f..f1f89506 100644 --- a/AsbCloudWebApi/Controllers/DepositController.cs +++ b/AsbCloudWebApi/Controllers/DepositController.cs @@ -1,5 +1,5 @@ using AsbCloudApp.Data; -using AsbCloudApp.Services; +using AsbCloudApp.Repositories; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; @@ -16,11 +16,11 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class DepositController : ControllerBase { - private readonly IClusterService clusterService; + private readonly IDepositRepository depositService; - public DepositController(IClusterService clusterService) + public DepositController(IDepositRepository depositService) { - this.clusterService = clusterService; + this.depositService = depositService; } /// @@ -38,7 +38,7 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null) return Forbid(); - var result = await clusterService.GetDepositsAsync((int)idCompany, + var result = await depositService.GetAsync((int)idCompany, token).ConfigureAwait(false); return Ok(result); } @@ -58,7 +58,7 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null) return Forbid(); - var result = await clusterService.GetDepositsDrillParamsAsync((int)idCompany, + var result = await depositService.GetAllWithDrillParamsAsync((int)idCompany, token).ConfigureAwait(false); return Ok(result); } @@ -80,7 +80,7 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null) return Forbid(); - var result = await clusterService.GetClustersAsync((int)idCompany, + var result = await depositService.GetClustersAsync((int)idCompany, depositId, token).ConfigureAwait(false); return Ok(result); }