Remove ClusterController, ClusterService.

This commit is contained in:
ngfrolov 2022-12-23 14:35:23 +05:00
parent 689872d1e5
commit 4a4b7852b7
7 changed files with 72 additions and 361 deletions

View File

@ -0,0 +1,44 @@
using AsbCloudApp.Data;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories
{
#nullable enable
/// <summary>
/// Сервис информации о кустах
/// </summary>
public interface IDepositRepository
{
/// <summary>
/// список месторождений, доступных для пользователя
/// </summary>
/// <param name="idCompany"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DepositDto>> GetAsync(int idCompany,
CancellationToken token);
/// <summary>
/// Список месторождений/кустов/скважин у которых заполненны параметры бурения
/// </summary>
/// <param name="idCompany"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DepositDto>> GetAllWithDrillParamsAsync(int idCompany,
CancellationToken token = default);
/// <summary>
/// Список кустов месторождения доступных компании
/// </summary>
/// <param name="idCompany"></param>
/// <param name="depositId"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
int depositId, CancellationToken token);
}
#nullable disable
}

View File

@ -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
/// <summary>
/// Сервис информации о кустах
/// </summary>
public interface IClusterService
{
/// <summary>
/// список месторождений
/// </summary>
/// <param name="idCompany"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
CancellationToken token);
/// <summary>
/// Список кустов с заполненными параметрами бурения
/// </summary>
/// <param name="idCompany"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DepositDto>> GetDepositsDrillParamsAsync(int idCompany,
CancellationToken token = default);
/// <summary>
/// Список кустов месторождения доступных компании
/// </summary>
/// <param name="idCompany"></param>
/// <param name="depositId"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
int depositId, CancellationToken token);
/// <summary>
/// Список кустов доступных компании
/// </summary>
/// <param name="idCompany"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
CancellationToken token);
/// <summary>
/// Список скважин доступных компании
/// </summary>
/// <param name="idCompany"></param>
/// <param name="clusterId"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<WellDto>> GetWellsAsync(int idCompany,
int clusterId, CancellationToken token);
}
}

View File

@ -108,7 +108,7 @@ namespace AsbCloudInfrastructure
services.AddSingleton<IReduceSamplingService>(provider => ReduceSamplingService.GetInstance(configuration));
services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IClusterService, ClusterService>();
services.AddTransient<IDepositRepository, DepositRepository>();
services.AddTransient<IProcessMapRepository, ProcessMapRepository>();
services.AddTransient<IDrillingProgramService, DrillingProgramService>();
services.AddTransient<IEventService, EventService>();

View File

@ -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<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
/// <inheritdoc/>
public async Task<IEnumerable<DepositDto>> 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<IEnumerable<DepositDto>> GetDepositsDrillParamsAsync(int idCompany,
/// <inheritdoc/>
public async Task<IEnumerable<DepositDto>> 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<IEnumerable<ClusterDto>> 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<IEnumerable<ClusterDto>>();
return dtos;
}
/// <inheritdoc/>
public async Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
int depositId, CancellationToken token = default)
{
@ -90,48 +80,22 @@ namespace AsbCloudInfrastructure.Services
return dtos;
}
public async Task<IEnumerable<WellDto>> 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<IGrouping<Deposit, IGrouping<Cluster, Well>>> GroupWells(IEnumerable<Well> wellEntities)
{
return wellEntities
=> wellEntities
.GroupBy(w => w.Cluster)
.GroupBy(c => c.Key.Deposit);
}
private IQueryable<Well> 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<DepositDto> CreateDepositDto(IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> 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
}

View File

@ -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<IWellService> wellService;
private readonly List<Deposit> 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<Cluster> 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<Well> 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<CompanyType> companiesTypes = new()
{
new CompanyType { Id = 1, Caption = "test company type"}
};
private readonly List<Company> 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<RelationCompanyWell> relations = new()
{
new RelationCompanyWell { IdCompany = 1, IdWell = 1 },
new RelationCompanyWell { IdCompany = 1, IdWell = 2 },
new RelationCompanyWell { IdCompany = 2, IdWell = 2 }
};
private readonly List<WellSectionType> wellSectionTypes = new()
{
new WellSectionType { Id = 1, Caption = "Test well section type 1" }
};
public ClusterServiceTest()
{
context = TestHelpter.MakeRealTestContext();
wellService = new Mock<IWellService>();
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);
}
}

View File

@ -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
{
/// <summary>
/// Инфо о кустах
/// </summary>
[Route("api/cluster")]
[ApiController]
[Authorize]
public class ClusterController : ControllerBase
{
private readonly IClusterService clusterService;
public ClusterController(IClusterService clusterService)
{
this.clusterService = clusterService;
}
/// <summary>
/// Получает список доступных пользователю кустов
/// </summary>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpGet()]
[Permission]
[ProducesResponseType(typeof(IEnumerable<ClusterDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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);
}
/// <summary>
/// Получение доступных пользователю скважин
/// </summary>
/// <param name="idCluster"></param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpGet("{idCluster}")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<WellDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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);
}
}
}

View File

@ -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;
}
/// <summary>
@ -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);
}