diff --git a/Persistence.API/Controllers/DataSourceSystemController.cs b/Persistence.API/Controllers/DataSourceSystemController.cs new file mode 100644 index 0000000..599c11e --- /dev/null +++ b/Persistence.API/Controllers/DataSourceSystemController.cs @@ -0,0 +1,51 @@ +using System.Net; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; +using Persistence.Repositories; + +namespace Persistence.API.Controllers; + +/// +/// Работа с системами +/// +[ApiController] +[Authorize] +[Route("api/[controller]")] +public class DataSourceSystemController : ControllerBase +{ + private readonly IDataSourceSystemRepository dataSourceSystemRepository; + + public DataSourceSystemController(IDataSourceSystemRepository dataSourceSystemRepository) + { + this.dataSourceSystemRepository = dataSourceSystemRepository; + } + + /// + /// Получить системы + /// + /// + /// + [HttpGet] + public async Task> Get(CancellationToken token) + { + var result = await dataSourceSystemRepository.Get(token); + + return Ok(result); + } + + /// + /// Добавить систему + /// + /// + /// + /// + [HttpPost] + [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] + public async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token) + { + await dataSourceSystemRepository.Add(dataSourceSystemDto, token); + + return CreatedAtAction(nameof(Add), true); + } +} diff --git a/Persistence.API/Controllers/TechMessagesController.cs b/Persistence.API/Controllers/TechMessagesController.cs index 10efb04..50481a0 100644 --- a/Persistence.API/Controllers/TechMessagesController.cs +++ b/Persistence.API/Controllers/TechMessagesController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Persistence.Models; using Persistence.Models.Requests; @@ -44,17 +44,17 @@ public class TechMessagesController : ControllerBase return Ok(result); } - /// - /// Получить статистику по системам - /// - /// - /// - /// - /// - [HttpGet("statistics")] - public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) - { - var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token); + /// + /// Получить статистику по системам + /// + /// + /// + /// + /// + [HttpGet("statistics")] + public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) + { + var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token); return Ok(result); } @@ -78,11 +78,11 @@ public class TechMessagesController : ControllerBase /// /// [HttpGet("range")] - public async Task> GetDatesRangeAsync(CancellationToken token) + public async Task> GetDatesRangeAsync(CancellationToken token) { var result = await techMessagesRepository.GetDatesRangeAsync(token); - return Ok(result); + return result == null ? NoContent() : Ok(result); } /// @@ -100,19 +100,20 @@ public class TechMessagesController : ControllerBase return Ok(result); } - /// - /// Добавить новые технологические сообщения - /// - /// - /// - /// - [HttpPost] - [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] - public async Task AddRange([FromBody] IEnumerable dtos, CancellationToken token) - { - var userId = User.GetUserId(); + /// + /// Добавить новые технологические сообщения + /// + /// + /// + /// + /// + [HttpPost("{systemId}")] + [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] + public async Task AddRange([FromRoute] Guid systemId, [FromBody] IEnumerable dtos, CancellationToken token) + { + var userId = User.GetUserId(); - var result = await techMessagesRepository.AddRange(dtos, userId, token); + var result = await techMessagesRepository.AddRange(systemId, dtos, userId, token); return CreatedAtAction(nameof(AddRange), result); } diff --git a/Persistence.Client/Clients/Base/BaseClient.cs b/Persistence.Client/Clients/Base/BaseClient.cs index 2332cbd..c29cf14 100644 --- a/Persistence.Client/Clients/Base/BaseClient.cs +++ b/Persistence.Client/Clients/Base/BaseClient.cs @@ -12,11 +12,11 @@ public abstract class BaseClient this.logger = logger; } - public async Task ExecuteGetResponse(Func>> getMethod, CancellationToken token) + public async Task ExecuteGetResponse(Func>> getMethod, CancellationToken token) { var response = await getMethod.Invoke().WaitAsync(token); - if (response.IsSuccessful) + if (response.IsSuccessStatusCode) { return response.Content; } @@ -32,7 +32,7 @@ public abstract class BaseClient { var response = await postMethod.Invoke().WaitAsync(token); - if (response.IsSuccessful) + if (response.IsSuccessStatusCode) { return; } @@ -48,7 +48,7 @@ public abstract class BaseClient { var response = await postMethod.Invoke().WaitAsync(token); - if (response.IsSuccessful) + if (response.IsSuccessStatusCode) { return response.Content; } diff --git a/Persistence.Client/Clients/ChangeLogClient.cs b/Persistence.Client/Clients/ChangeLogClient.cs index 00aee71..78c71a7 100644 --- a/Persistence.Client/Clients/ChangeLogClient.cs +++ b/Persistence.Client/Clients/ChangeLogClient.cs @@ -16,7 +16,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task ClearAndAddRange(Guid idDiscriminator, IEnumerable dtos, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.ClearAndAddRange(idDiscriminator, dtos, token), token); return result; @@ -25,7 +25,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task> GetByDate(Guid idDiscriminator, DateTimeOffset moment, SectionPartRequest filterRequest, PaginationRequest paginationRequest, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, token), token); return result; @@ -33,10 +33,10 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task> GetChangeLogForInterval(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, token), token); - return result; + return result!; } public async Task Add(Guid idDiscriminator, DataWithWellDepthAndSectionDto dto, CancellationToken token) @@ -89,7 +89,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetDatesRange(idDiscriminator, token), token); return result; @@ -98,5 +98,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public void Dispose() { refitChangeLogClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/DataSourceSystemClient.cs b/Persistence.Client/Clients/DataSourceSystemClient.cs new file mode 100644 index 0000000..25ec605 --- /dev/null +++ b/Persistence.Client/Clients/DataSourceSystemClient.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Logging; +using Persistence.Client.Clients.Base; +using Persistence.Client.Clients.Interfaces; +using Persistence.Client.Clients.Interfaces.Refit; +using Persistence.Models; + +namespace Persistence.Client.Clients; +public class DataSourceSystemClient : BaseClient, IDataSourceSystemClient +{ + private readonly IRefitDataSourceSystemClient dataSourceSystemClient; + + public DataSourceSystemClient(IRefitDataSourceSystemClient dataSourceSystemClient, ILogger logger) : base(logger) + { + this.dataSourceSystemClient = dataSourceSystemClient; + } + + public async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token) + { + await ExecutePostResponse( + async () => await dataSourceSystemClient.Add(dataSourceSystemDto, token), token); + } + + public async Task> Get(CancellationToken token) + { + var result = await ExecuteGetResponse( + async () => await dataSourceSystemClient.Get(token), token); + + return result!; + } + + public void Dispose() + { + dataSourceSystemClient.Dispose(); + + GC.SuppressFinalize(this); + } +} diff --git a/Persistence.Client/Clients/Interfaces/IDataSourceSystemClient.cs b/Persistence.Client/Clients/Interfaces/IDataSourceSystemClient.cs new file mode 100644 index 0000000..fc25412 --- /dev/null +++ b/Persistence.Client/Clients/Interfaces/IDataSourceSystemClient.cs @@ -0,0 +1,25 @@ +using Persistence.Models; +using Refit; + +namespace Persistence.Client.Clients.Interfaces; + +/// +/// Клиент для работы с системами +/// +public interface IDataSourceSystemClient : IDisposable +{ + /// + /// Получить системы + /// + /// + /// + Task> Get(CancellationToken token); + + /// + /// Добавить систему + /// + /// + /// + /// + Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token); +} diff --git a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs index bbf635c..5545dad 100644 --- a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs @@ -14,14 +14,14 @@ public interface ITechMessagesClient : IDisposable /// /// /// - Task AddRange(IEnumerable dtos, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, CancellationToken token); /// /// Получить диапазон дат, для которых есть данные в репозитории /// /// /// - Task GetDatesRangeAsync(CancellationToken token); + Task GetDatesRangeAsync(CancellationToken token); /// /// Получить список технологических сообщений в виде страницы @@ -43,16 +43,16 @@ public interface ITechMessagesClient : IDisposable /// /// Получить статистику по системам /// - /// - /// + /// + /// /// /// - Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token); + Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token); /// /// Получить список всех систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); } \ No newline at end of file diff --git a/Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs b/Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs new file mode 100644 index 0000000..ba20504 --- /dev/null +++ b/Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs @@ -0,0 +1,14 @@ +using Persistence.Models; +using Refit; + +namespace Persistence.Client.Clients.Interfaces.Refit; +public interface IRefitDataSourceSystemClient : IDisposable +{ + private const string BaseRoute = "/api/dataSourceSystem"; + + [Get($"{BaseRoute}")] + Task>> Get(CancellationToken token); + + [Post($"{BaseRoute}")] + Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token); +} diff --git a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs index 64cb49e..3e4cd10 100644 --- a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs @@ -1,4 +1,5 @@ -using Persistence.Models; +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; using Persistence.Models.Requests; using Refit; @@ -11,19 +12,19 @@ namespace Persistence.Client.Clients.Interfaces.Refit [Get($"{BaseRoute}")] Task>> GetPage([Query] PaginationRequest request, CancellationToken token); - [Post($"{BaseRoute}")] - Task> AddRange([Body] IEnumerable dtos, CancellationToken token); + [Post($"{BaseRoute}/{{systemId}}")] + Task> AddRange(Guid systemId, [Body] IEnumerable dtos, CancellationToken token); [Get($"{BaseRoute}/systems")] - Task>> GetSystems(CancellationToken token); + Task>> GetSystems(CancellationToken token); [Get($"{BaseRoute}/range")] - Task> GetDatesRangeAsync(CancellationToken token); + Task> GetDatesRangeAsync(CancellationToken token); [Get($"{BaseRoute}/part")] Task>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token); [Get($"{BaseRoute}/statistics")] - Task>> GetStatistics([Query] string autoDrillingSystem, [Query] int categoryId, CancellationToken token); + Task>> GetStatistics([Query(CollectionFormat.Multi)] IEnumerable systemIds, [Query(CollectionFormat.Multi)] IEnumerable categoryIds, CancellationToken token); } } diff --git a/Persistence.Client/Clients/SetpointClient.cs b/Persistence.Client/Clients/SetpointClient.cs index b1df4b4..ea6d767 100644 --- a/Persistence.Client/Clients/SetpointClient.cs +++ b/Persistence.Client/Clients/SetpointClient.cs @@ -17,42 +17,42 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token); - return result; + return result!; } public async Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetHistory(setpointKeys, historyMoment, token), token); - return result; + return result!; } public async Task>> GetLog(IEnumerable setpointKeys, CancellationToken token) { - var result = await ExecuteGetResponse>>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetLog(setpointKeys, token), token); - return result; + return result!; } public async Task GetDatesRangeAsync(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetDatesRangeAsync(token), token); - return result; + return result!; } public async Task> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetPart(dateBegin, take, token), token); - return result; + return result!; } public async Task Add(Guid setpointKey, object newValue, CancellationToken token) @@ -64,5 +64,7 @@ public class SetpointClient : BaseClient, ISetpointClient public void Dispose() { refitSetpointClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TechMessagesClient.cs b/Persistence.Client/Clients/TechMessagesClient.cs index c1000b1..f69c110 100644 --- a/Persistence.Client/Clients/TechMessagesClient.cs +++ b/Persistence.Client/Clients/TechMessagesClient.cs @@ -18,31 +18,31 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetPage(PaginationRequest request, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetPage(request, token), token); - return result; + return result!; } - public async Task AddRange(IEnumerable dtos, CancellationToken token) + public async Task AddRange(Guid systemId, IEnumerable dtos, CancellationToken token) { var result = await ExecutePostResponse( - async () => await refitTechMessagesClient.AddRange(dtos, token), token); + async () => await refitTechMessagesClient.AddRange(systemId, dtos, token), token); return result; } - public async Task> GetSystems(CancellationToken token) + public async Task> GetSystems(CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetSystems(token), token); - return result; + return result!; } - public async Task GetDatesRangeAsync(CancellationToken token) + public async Task GetDatesRangeAsync(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetDatesRangeAsync(token), token); return result; @@ -50,22 +50,24 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetPart(dateBegin, take, token), token); - return result; + return result!; } - public async Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token) + public async Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token) { - var result = await ExecuteGetResponse>( - async () => await refitTechMessagesClient.GetStatistics(autoDrillingSystem, categoryId, token), token); + var result = await ExecuteGetResponse( + async () => await refitTechMessagesClient.GetStatistics(systemIds, categoryIds, token), token); - return result; + return result!; } public void Dispose() { refitTechMessagesClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TimeSeriesClient.cs b/Persistence.Client/Clients/TimeSeriesClient.cs index a3fba7d..72b8ed8 100644 --- a/Persistence.Client/Clients/TimeSeriesClient.cs +++ b/Persistence.Client/Clients/TimeSeriesClient.cs @@ -24,23 +24,23 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public async Task> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.Get(dateBegin, dateEnd, token), token); - return result; + return result!; } public async Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.GetResampledData(dateBegin, intervalSec, approxPointsCount, token), token); - return result; + return result!; } public async Task GetDatesRange(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.GetDatesRange(token), token); return result; @@ -49,5 +49,7 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public void Dispose() { timeSeriesClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TimestampedSetClient.cs b/Persistence.Client/Clients/TimestampedSetClient.cs index 4d40d8b..3009488 100644 --- a/Persistence.Client/Clients/TimestampedSetClient.cs +++ b/Persistence.Client/Clients/TimestampedSetClient.cs @@ -24,23 +24,23 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.Get(idDiscriminator, geTimestamp, columnNames, skip, take, token), token); - return result; + return result!; } public async Task> GetLast(Guid idDiscriminator, IEnumerable? columnNames, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.GetLast(idDiscriminator, columnNames, take, token), token); - return result; + return result!; } public async Task Count(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.Count(idDiscriminator, token), token); return result; @@ -48,7 +48,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.GetDatesRange(idDiscriminator, token), token); return result; @@ -57,5 +57,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public void Dispose() { refitTimestampedSetClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/WitsDataClient.cs b/Persistence.Client/Clients/WitsDataClient.cs index ad58be1..73e5873 100644 --- a/Persistence.Client/Clients/WitsDataClient.cs +++ b/Persistence.Client/Clients/WitsDataClient.cs @@ -24,30 +24,32 @@ public class WitsDataClient : BaseClient, IWitsDataClient public async Task GetDatesRangeAsync(Guid discriminatorId, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetDatesRangeAsync(discriminatorId, token), token); - return result; + return result!; } public async Task> GetPart(Guid discriminatorId, DateTimeOffset dateBegin, int take = 86400, CancellationToken token = default) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetPart(discriminatorId, dateBegin, take, token), token); - return result; + return result!; } public async Task> GetValuesForGraph(Guid discriminatorId, DateTimeOffset dateFrom, DateTimeOffset dateTo, int approxPointsCount, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointsCount, token), token); - return result; + return result!; } public void Dispose() { refitWitsDataClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/PersistenceClientFactory.cs b/Persistence.Client/PersistenceClientFactory.cs index 31b6fd9..4807367 100644 --- a/Persistence.Client/PersistenceClientFactory.cs +++ b/Persistence.Client/PersistenceClientFactory.cs @@ -128,5 +128,19 @@ namespace Persistence.Client return client; } + + /// + /// Получить клиент для работы c системами + /// + /// + public IDataSourceSystemClient GetDataSourceSystemClient() + { + var logger = provider.GetRequiredService>(); + + var restClient = RestService.For(httpClient, RefitSettings); + var client = new DataSourceSystemClient(restClient, logger); + + return client; + } } } diff --git a/Persistence.Client/Readme.md b/Persistence.Client/Readme.md index e9f4024..f7c35f6 100644 --- a/Persistence.Client/Readme.md +++ b/Persistence.Client/Readme.md @@ -15,6 +15,7 @@ Persistence сервисом посредством обращения к кон - `ITimestampedSetClient` - Клиент для работы с данными с отметкой времени - `IChangeLogClient` - Клиент для работы с записями ChangeLog - `IWitsDataClient` - Клиент для работы с параметрами Wits +- `IDataSourceSystemClient` - Клиент для работы с системами ## Использование Для получения того или иного Persistence - клиента нужно diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs similarity index 70% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs index 6ed33f7..0124f24 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs @@ -9,10 +9,10 @@ using Persistence.Database.Model; #nullable disable -namespace Persistence.Database.Postgres.Migrations +namespace Persistence.Database.Postgres.Migrations.PersistencePostgres { - [DbContext(typeof(PersistenceDbContext))] - [Migration("20241202072250_TechMessageMigration")] + [DbContext(typeof(PersistencePostgresContext))] + [Migration("20241212041758_TechMessageMigration")] partial class TechMessageMigration { /// @@ -48,6 +48,30 @@ namespace Persistence.Database.Postgres.Migrations b.ToTable("DrillingSystem"); }); + modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b => + { + b.Property("DiscriminatorId") + .HasColumnType("uuid") + .HasComment("Дискриминатор системы"); + + b.Property("ParameterId") + .HasColumnType("integer") + .HasComment("Id параметра"); + + b.Property("Timestamp") + .HasColumnType("timestamp with time zone") + .HasComment("Временная отметка"); + + b.Property("Value") + .IsRequired() + .HasColumnType("varchar(256)") + .HasComment("Значение параметра в виде строки"); + + b.HasKey("DiscriminatorId", "ParameterId", "Timestamp"); + + b.ToTable("ParameterData"); + }); + modelBuilder.Entity("Persistence.Database.Entity.TechMessage", b => { b.Property("EventId") @@ -59,27 +83,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("UserId") - .HasColumnType("uuid") - .HasComment("Id пользователя за пультом бурильщика"); - b.HasKey("EventId"); b.HasIndex("SystemId"); @@ -110,6 +130,59 @@ namespace Persistence.Database.Postgres.Migrations }); }); + modelBuilder.Entity("Persistence.Database.Model.ChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid") + .HasComment("Ключ записи"); + + b.Property("Creation") + .HasColumnType("timestamp with time zone") + .HasComment("Дата создания записи"); + + b.Property("DepthEnd") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату окончания интервала"); + + b.Property("DepthStart") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату начала интервала"); + + b.Property("IdAuthor") + .HasColumnType("uuid") + .HasComment("Автор изменения"); + + b.Property("IdDiscriminator") + .HasColumnType("uuid") + .HasComment("Дискриминатор таблицы"); + + b.Property("IdEditor") + .HasColumnType("uuid") + .HasComment("Редактор"); + + b.Property("IdNext") + .HasColumnType("uuid") + .HasComment("Id заменяющей записи"); + + b.Property("IdSection") + .HasColumnType("uuid") + .HasComment("Ключ секции"); + + b.Property("Obsolete") + .HasColumnType("timestamp with time zone") + .HasComment("Дата устаревания (например при удалении)"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb") + .HasComment("Значение"); + + b.HasKey("Id"); + + b.ToTable("ChangeLog"); + }); + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => { b.Property("Date") diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs similarity index 66% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs index 41ef8cb..c0066ae 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs @@ -2,7 +2,7 @@ #nullable disable -namespace Persistence.Database.Postgres.Migrations +namespace Persistence.Database.Postgres.Migrations.PersistencePostgres { /// public partial class TechMessageMigration : Migration @@ -11,16 +11,16 @@ namespace Persistence.Database.Postgres.Migrations protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "DrillingSystem", + name: "DataSourceSystem", columns: table => new { - SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы автобурения"), - Name = table.Column(type: "varchar(256)", nullable: false, comment: "Наименование системы автобурения"), - Description = table.Column(type: "text", nullable: true, comment: "Описание системы автобурения") + SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы - источника данных"), + Name = table.Column(type: "varchar(256)", nullable: false, comment: "Наименование системы - источника данных"), + Description = table.Column(type: "text", nullable: true, comment: "Описание системы - источника данных") }, constraints: table => { - table.PrimaryKey("PK_DrillingSystem", x => x.SystemId); + table.PrimaryKey("PK_DataSourceSystem", x => x.SystemId); }); migrationBuilder.CreateTable( @@ -30,18 +30,17 @@ namespace Persistence.Database.Postgres.Migrations EventId = table.Column(type: "uuid", nullable: false, comment: "Id события"), CategoryId = table.Column(type: "integer", nullable: false, comment: "Id Категории важности"), Timestamp = table.Column(type: "timestamp with time zone", nullable: false, comment: "Дата возникновения"), - Depth = table.Column(type: "double precision", nullable: true, comment: "Глубина забоя"), - MessageText = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), - SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы автобурения, к которой относится сообщение"), - UserId = table.Column(type: "uuid", nullable: false, comment: "Id пользователя за пультом бурильщика") + Text = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), + SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы, к которой относится сообщение"), + EventState = table.Column(type: "integer", nullable: false, comment: "Статус события") }, constraints: table => { table.PrimaryKey("PK_TechMessage", x => x.EventId); table.ForeignKey( - name: "FK_TechMessage_DrillingSystem_SystemId", + name: "FK_TechMessage_DataSourceSystem_SystemId", column: x => x.SystemId, - principalTable: "DrillingSystem", + principalTable: "DataSourceSystem", principalColumn: "SystemId", onDelete: ReferentialAction.Cascade); }); @@ -59,7 +58,7 @@ namespace Persistence.Database.Postgres.Migrations name: "TechMessage"); migrationBuilder.DropTable( - name: "DrillingSystem"); + name: "DataSourceSystem"); } } } diff --git a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs index 6ab8159..f233af8 100644 --- a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs +++ b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs @@ -1,6 +1,5 @@ // using System; -using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; @@ -25,31 +24,31 @@ namespace Persistence.Database.Postgres.Migrations NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "adminpack"); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - modelBuilder.Entity("Persistence.Database.Entity.DrillingSystem", b => + modelBuilder.Entity("Persistence.Database.Entity.DataSourceSystem", b => { b.Property("SystemId") .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasComment("Id системы автобурения"); + .HasComment("Id системы - источника данных"); b.Property("Description") .HasColumnType("text") - .HasComment("Описание системы автобурения"); + .HasComment("Описание системы - источника данных"); b.Property("Name") .IsRequired() .HasColumnType("varchar(256)") - .HasComment("Наименование системы автобурения"); + .HasComment("Наименование системы - источника данных"); b.HasKey("SystemId"); - b.ToTable("DrillingSystem"); + b.ToTable("DataSourceSystem"); }); modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b => { - b.Property("DiscriminatorId") - .HasColumnType("integer") + b.Property("DiscriminatorId") + .HasColumnType("uuid") .HasComment("Дискриминатор системы"); b.Property("ParameterId") @@ -81,27 +80,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("UserId") - .HasColumnType("uuid") - .HasComment("Id пользователя за пультом бурильщика"); - b.HasKey("EventId"); b.HasIndex("SystemId"); @@ -137,48 +132,48 @@ namespace Persistence.Database.Postgres.Migrations b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasColumnName("Id"); + .HasComment("Ключ записи"); b.Property("Creation") .HasColumnType("timestamp with time zone") - .HasColumnName("Creation"); + .HasComment("Дата создания записи"); b.Property("DepthEnd") .HasColumnType("double precision") - .HasColumnName("DepthEnd"); + .HasComment("Глубина забоя на дату окончания интервала"); b.Property("DepthStart") .HasColumnType("double precision") - .HasColumnName("DepthStart"); + .HasComment("Глубина забоя на дату начала интервала"); b.Property("IdAuthor") .HasColumnType("uuid") - .HasColumnName("IdAuthor"); + .HasComment("Автор изменения"); b.Property("IdDiscriminator") .HasColumnType("uuid") - .HasColumnName("IdDiscriminator"); + .HasComment("Дискриминатор таблицы"); b.Property("IdEditor") .HasColumnType("uuid") - .HasColumnName("IdEditor"); + .HasComment("Редактор"); b.Property("IdNext") .HasColumnType("uuid") - .HasColumnName("IdNext"); + .HasComment("Id заменяющей записи"); b.Property("IdSection") .HasColumnType("uuid") - .HasColumnName("IdSection"); + .HasComment("Ключ секции"); b.Property("Obsolete") .HasColumnType("timestamp with time zone") - .HasColumnName("Obsolete"); + .HasComment("Дата устаревания (например при удалении)"); - b.Property>("Value") + b.Property("Value") .IsRequired() .HasColumnType("jsonb") - .HasColumnName("Value"); + .HasComment("Значение"); b.HasKey("Id"); @@ -294,7 +289,7 @@ namespace Persistence.Database.Postgres.Migrations modelBuilder.Entity("Persistence.Database.Entity.TechMessage", b => { - b.HasOne("Persistence.Database.Entity.DrillingSystem", "System") + b.HasOne("Persistence.Database.Entity.DataSourceSystem", "System") .WithMany() .HasForeignKey("SystemId") .OnDelete(DeleteBehavior.Cascade) diff --git a/Persistence.Database/Entity/DataSourceSystem.cs b/Persistence.Database/Entity/DataSourceSystem.cs new file mode 100644 index 0000000..a2fcaf9 --- /dev/null +++ b/Persistence.Database/Entity/DataSourceSystem.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Persistence.Database.Entity; +public class DataSourceSystem +{ + [Key, Comment("Id системы - источника данных")] + public Guid SystemId { get; set; } + + [Required, Column(TypeName = "varchar(256)"), Comment("Наименование системы - источника данных")] + public required string Name { get; set; } + + [Comment("Описание системы - источника данных")] + public string? Description { get; set; } +} diff --git a/Persistence.Database/Entity/DrillingSystem.cs b/Persistence.Database/Entity/DrillingSystem.cs deleted file mode 100644 index dc52ce7..0000000 --- a/Persistence.Database/Entity/DrillingSystem.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace Persistence.Database.Entity; -public class DrillingSystem -{ - [Key, Comment("Id системы автобурения")] - public Guid SystemId { get; set; } - - [Required, Column(TypeName = "varchar(256)"), Comment("Наименование системы автобурения")] - public required string Name { get; set; } - - [Comment("Описание системы автобурения")] - public string? Description { get; set; } -} diff --git a/Persistence.Database/Entity/TechMessage.cs b/Persistence.Database/Entity/TechMessage.cs index 8051415..52fcecc 100644 --- a/Persistence.Database/Entity/TechMessage.cs +++ b/Persistence.Database/Entity/TechMessage.cs @@ -1,4 +1,4 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -15,19 +15,16 @@ namespace Persistence.Database.Entity [Comment("Дата возникновения")] public DateTimeOffset Timestamp { get; set; } - [Comment("Глубина забоя")] - public double? Depth { get; set; } + [Column(TypeName = "varchar(512)"), Comment("Текст сообщения")] + public required string Text { get; set; } - [Column(TypeName = "varchar(512)"), Comment("Текст сообщения")] - public required string MessageText { get; set; } + [Required, Comment("Id системы, к которой относится сообщение")] + public required Guid SystemId { get; set; } - [Required, Comment("Id системы автобурения, к которой относится сообщение")] - public required Guid SystemId { get; set; } + [Required, ForeignKey(nameof(SystemId)), Comment("Система, к которой относится сообщение")] + public virtual required DataSourceSystem System { get; set; } - [Required, ForeignKey(nameof(SystemId)), Comment("Система автобурения, к которой относится сообщение")] - public virtual required DrillingSystem System { get; set; } - - [Comment("Id пользователя за пультом бурильщика")] - public Guid UserId { get; set; } - } + [Comment("Статус события")] + public int EventState { get; set; } + } } diff --git a/Persistence.Database/PersistenceDbContext.cs b/Persistence.Database/PersistenceDbContext.cs index a61ea31..e43b1d7 100644 --- a/Persistence.Database/PersistenceDbContext.cs +++ b/Persistence.Database/PersistenceDbContext.cs @@ -21,7 +21,9 @@ public class PersistenceDbContext : DbContext public DbSet ParameterData => Set(); - public PersistenceDbContext(DbContextOptions options) + public DbSet DataSourceSystem => Set(); + + public PersistenceDbContext(DbContextOptions options) : base(options) { diff --git a/Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs b/Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs new file mode 100644 index 0000000..8638e90 --- /dev/null +++ b/Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs @@ -0,0 +1,82 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using Persistence.Client; +using Persistence.Client.Clients; +using Persistence.Client.Clients.Interfaces; +using Persistence.Database.Entity; +using Persistence.Models; +using Xunit; + +namespace Persistence.IntegrationTests.Controllers +{ + public class DataSourceSystemControllerTest : BaseIntegrationTest + { + private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; + private readonly IDataSourceSystemClient dataSourceSystemClient; + private readonly IMemoryCache memoryCache; + public DataSourceSystemControllerTest(WebAppFactoryFixture factory) : base(factory) + { + var scope = factory.Services.CreateScope(); + var persistenceClientFactory = scope.ServiceProvider + .GetRequiredService(); + + dataSourceSystemClient = persistenceClientFactory.GetDataSourceSystemClient(); + memoryCache = scope.ServiceProvider.GetRequiredService(); + } + + [Fact] + public async Task Get_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + + //act + var response = await dataSourceSystemClient.Get(CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.Empty(response); + } + + [Fact] + public async Task Get_AfterSave_returns_success() + { + //arrange + await Add(); + + //act + var response = await dataSourceSystemClient.Get(CancellationToken.None); + + //assert + Assert.NotNull(response); + + var expectedSystemCount = 1; + var actualSystemCount = response!.Count(); + Assert.Equal(expectedSystemCount, actualSystemCount); + } + + [Fact] + public async Task Add_returns_success() + { + await Add(); + } + + private async Task Add() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + + var dto = new DataSourceSystemDto() + { + SystemId = Guid.NewGuid(), + Name = "Test", + Description = "Test" + }; + + //act + await dataSourceSystemClient.Add(dto, CancellationToken.None); + } + } +} diff --git a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index 3ec7fce..c51804a 100644 --- a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -4,34 +4,35 @@ using Persistence.Client; using Persistence.Client.Clients.Interfaces; using Persistence.Database.Entity; using Persistence.Models; +using Persistence.Models.Enumerations; using Persistence.Models.Requests; using System.Net; using Xunit; namespace Persistence.IntegrationTests.Controllers { - public class TechMessagesControllerTest : BaseIntegrationTest - { - private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DrillingSystem).FullName}CacheKey"; - private readonly ITechMessagesClient techMessagesClient; - private readonly IMemoryCache memoryCache; - public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory) - { - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + public class TechMessagesControllerTest : BaseIntegrationTest + { + private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; + private readonly ITechMessagesClient techMessagesClient; + private readonly IMemoryCache memoryCache; + public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory) + { + var scope = factory.Services.CreateScope(); + var persistenceClientFactory = scope.ServiceProvider + .GetRequiredService(); techMessagesClient = persistenceClientFactory.GetTechMessagesClient(); memoryCache = scope.ServiceProvider.GetRequiredService(); } - [Fact] - public async Task GetPage_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + [Fact] + public async Task GetPage_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); var requestDto = new PaginationRequest() { @@ -50,18 +51,18 @@ namespace Persistence.IntegrationTests.Controllers Assert.Equal(requestDto.Take, response.Take); } - [Fact] - public async Task GetPage_AfterSave_returns_success() - { - //arrange - var dtos = await InsertRange(); - var dtosCount = dtos.Count(); - var requestDto = new PaginationRequest() - { - Skip = 0, - Take = 2, - SortSettings = nameof(TechMessage.CategoryId) - }; + [Fact] + public async Task GetPage_AfterSave_returns_success() + { + //arrange + var dtos = await InsertRange(Guid.NewGuid()); + var dtosCount = dtos.Count(); + var requestDto = new PaginationRequest() + { + Skip = 0, + Take = 2, + SortSettings = nameof(TechMessage.CategoryId) + }; //act var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); @@ -71,17 +72,18 @@ namespace Persistence.IntegrationTests.Controllers Assert.Equal(dtosCount, response.Count); } - [Fact] - public async Task InsertRange_returns_success() - { - await InsertRange(); - } + [Fact] + public async Task InsertRange_returns_success() + { + await InsertRange(Guid.NewGuid()); + } [Fact] public async Task InsertRange_returns_BadRequest() { //arrange const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса"; + var systemId = Guid.NewGuid(); var dtos = new List() { new TechMessageDto() @@ -89,17 +91,15 @@ namespace Persistence.IntegrationTests.Controllers EventId = Guid.NewGuid(), CategoryId = -1, // < 0 Timestamp = DateTimeOffset.UtcNow, - Depth = -1, // < 0 - MessageText = string.Empty, // length < 0 - System = string.Concat(Enumerable.Repeat(nameof(TechMessageDto.System), 100)), // length > 256 - UserId = Guid.NewGuid() - } - }; + Text = string.Empty, // length < 0 + EventState = EventState.Triggered + } + }; try { //act - var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); } catch (Exception ex) { @@ -108,102 +108,97 @@ namespace Persistence.IntegrationTests.Controllers } } - [Fact] - public async Task GetSystems_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); - - //act - var response = await techMessagesClient.GetSystems(CancellationToken.None); - - //assert - Assert.NotNull(response); - Assert.Empty(response); - } - - [Fact] - public async Task GetSystems_AfterSave_returns_success() - { - //arrange - var dtos = await InsertRange(); - var systems = dtos - .Select(e => e.System) - .Distinct() - .ToArray(); - - //act - var response = await techMessagesClient.GetSystems(CancellationToken.None); - - //assert - Assert.NotNull(response); - string?[]? content = response?.ToArray(); - Assert.Equal(systems, content); - } - - [Fact] - public async Task GetStatistics_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); - - var importantId = 1; - var autoDrillingSystem = nameof(TechMessageDto.System); - - //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, importantId, CancellationToken.None); - - //assert - Assert.NotNull(response); - Assert.Empty(response); - } - - [Fact] - public async Task GetStatistics_AfterSave_returns_success() - { - //arrange - var importantId = 0; - var autoDrillingSystem = nameof(TechMessageDto.System); - var dtos = await InsertRange(); - var filteredDtos = dtos.Where(e => e.CategoryId == importantId && e.System == autoDrillingSystem); - - //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, importantId, CancellationToken.None); - - //assert - Assert.NotNull(response); - var categories = response - .FirstOrDefault()?.Categories - .FirstOrDefault(e => e.Key == 0).Value; - Assert.Equal(filteredDtos.Count(), categories); - } - [Fact] - public async Task GetDatesRange_returns_success() + public async Task GetSystems_returns_success() { //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); - //act - var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); + //act + var response = await techMessagesClient.GetSystems(CancellationToken.None); //assert Assert.NotNull(response); - Assert.Equal(DateTimeOffset.MinValue, response?.From); - Assert.Equal(DateTimeOffset.MaxValue, response?.To); + Assert.Empty(response); } - [Fact] - public async Task GetDatesRange_AfterSave_returns_success() - { - //arrange - await InsertRange(); + [Fact] + public async Task GetSystems_AfterSave_returns_success() + { + //arrange + await InsertRange(Guid.NewGuid()); + + //act + var response = await techMessagesClient.GetSystems(CancellationToken.None); + + //assert + Assert.NotNull(response); + var expectedSystemCount = 1; + Assert.Equal(expectedSystemCount, response!.Count()); + } + + [Fact] + public async Task GetStatistics_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + + var categoryIds = new [] { 1, 2 }; + var systemIds = new [] { Guid.NewGuid() }; + + //act + var response = await techMessagesClient.GetStatistics(systemIds, categoryIds, CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.Empty(response); + } + + [Fact] + public async Task GetStatistics_AfterSave_returns_success() + { + //arrange + var categoryIds = new[] { 1 }; + var systemId = Guid.NewGuid(); + var dtos = await InsertRange(systemId); + var filteredDtos = dtos.Where(e => categoryIds.Contains(e.CategoryId)); + + //act + var response = await techMessagesClient.GetStatistics([systemId], categoryIds, CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.NotEmpty(response); + var categories = response + .FirstOrDefault()!.Categories + .Count(); + Assert.Equal(filteredDtos.Count(), categories); + } + + [Fact] + public async Task GetDatesRange_returns_NoContent() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + + //act + var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); + + //assert + Assert.Null(response); + } + + [Fact] + public async Task GetDatesRange_AfterSave_returns_success() + { + //arrange + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); @@ -229,13 +224,13 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response); } - [Fact] - public async Task GetPart_AfterSave_returns_success() - { - //arrange - var dateBegin = DateTimeOffset.UtcNow; - var take = 1; - await InsertRange(); + [Fact] + public async Task GetPart_AfterSave_returns_success() + { + //arrange + var dateBegin = DateTimeOffset.UtcNow; + var take = 1; + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); @@ -245,40 +240,36 @@ namespace Persistence.IntegrationTests.Controllers Assert.NotEmpty(response); } - private async Task> InsertRange() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + private async Task> InsertRange(Guid systemId) + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); - var dtos = new List() - { - new() - { - EventId = Guid.NewGuid(), - CategoryId = 1, - Timestamp = DateTimeOffset.UtcNow, - Depth = 1.11, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() - }, - new() - { - EventId = Guid.NewGuid(), - CategoryId = 2, - Timestamp = DateTimeOffset.UtcNow, - Depth = 2.22, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() - } - }; + var dtos = new List() + { + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 1, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + }, + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 2, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + } + }; - //act - var response = await techMessagesClient.AddRange(dtos, CancellationToken.None); + //act + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); //assert Assert.Equal(dtos.Count, response); diff --git a/Persistence.Repository/DependencyInjection.cs b/Persistence.Repository/DependencyInjection.cs index bb2f799..a7db05e 100644 --- a/Persistence.Repository/DependencyInjection.cs +++ b/Persistence.Repository/DependencyInjection.cs @@ -34,7 +34,8 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); - return services; + return services; } } diff --git a/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs b/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs new file mode 100644 index 0000000..5869992 --- /dev/null +++ b/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; +using Persistence.Models; + +namespace Persistence.Repository.Repositories; +public class DataSourceSystemCachedRepository : DataSourceSystemRepository +{ + private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; + private readonly IMemoryCache memoryCache; + private const int CacheExpirationInMinutes = 60; + private readonly TimeSpan? AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60); + + public DataSourceSystemCachedRepository(DbContext db, IMemoryCache memoryCache) : base(db) + { + this.memoryCache = memoryCache; + } + public override async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token) + { + await base.Add(dataSourceSystemDto, token); + + memoryCache.Remove(SystemCacheKey); + } + public override async Task> Get(CancellationToken token) + { + var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async (cacheEntry) => + { + cacheEntry.AbsoluteExpirationRelativeToNow = AbsoluteExpirationRelativeToNow; + + var dtos = await base.Get(token); + + return dtos; + }); + + return systems!; + } +} diff --git a/Persistence.Repository/Repositories/DataSourceSystemRepository.cs b/Persistence.Repository/Repositories/DataSourceSystemRepository.cs new file mode 100644 index 0000000..5862ea3 --- /dev/null +++ b/Persistence.Repository/Repositories/DataSourceSystemRepository.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Mapster; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json.Linq; +using Persistence.Database.Entity; +using Persistence.Models; +using Persistence.Repositories; + +namespace Persistence.Repository.Repositories; +public class DataSourceSystemRepository : IDataSourceSystemRepository +{ + protected DbContext db; + public DataSourceSystemRepository(DbContext db) + { + this.db = db; + } + protected virtual IQueryable GetQueryReadOnly() => db.Set(); + + public virtual async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token) + { + var entity = dataSourceSystemDto.Adapt(); + + await db.Set().AddAsync(entity, token); + await db.SaveChangesAsync(token); + } + + public virtual async Task> Get(CancellationToken token) + { + var query = GetQueryReadOnly(); + var entities = await query.ToArrayAsync(token); + var dtos = entities.Select(e => e.Adapt()); + + return dtos; + } +} diff --git a/Persistence.Repository/Repositories/TechMessagesRepository.cs b/Persistence.Repository/Repositories/TechMessagesRepository.cs index db59f63..74aff0a 100644 --- a/Persistence.Repository/Repositories/TechMessagesRepository.cs +++ b/Persistence.Repository/Repositories/TechMessagesRepository.cs @@ -1,6 +1,7 @@ using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json.Linq; using Persistence.Database.Entity; using Persistence.Models; using Persistence.Models.Requests; @@ -9,17 +10,15 @@ using UuidExtensions; namespace Persistence.Repository.Repositories { - public class TechMessagesRepository : ITechMessagesRepository - { - private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DrillingSystem).FullName}CacheKey"; - private const int CacheExpirationInMinutes = 60; - private readonly IMemoryCache memoryCache; - private readonly DbContext db; + public class TechMessagesRepository : ITechMessagesRepository + { + private readonly IDataSourceSystemRepository sourceSystemRepository; + private DbContext db; - public TechMessagesRepository(DbContext db, IMemoryCache memoryCache) + public TechMessagesRepository(DbContext db, IDataSourceSystemRepository sourceSystemRepository) { - this.memoryCache = memoryCache; this.db = db; + this.sourceSystemRepository = sourceSystemRepository; } protected virtual IQueryable GetQueryReadOnly() => db.Set() @@ -50,19 +49,18 @@ namespace Persistence.Repository.Repositories return dto; } - public async Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token) - { - var query = GetQueryReadOnly(); - var systems = autoDrillingSystem.Select(s => s.ToLower().Trim()); - var result = await query - .Where(e => !systems.Any() || systems.Contains(e.System.Name.ToLower().Trim())) - .GroupBy(e => e.System.Name, (key, group) => new - { - System = key, - Categories = group - .Where(g => !categoryIds.Any() || categoryIds.Contains(g.CategoryId)) - }) - .ToArrayAsync(token); + public async Task> GetStatistics(IEnumerable systems, IEnumerable categoryIds, CancellationToken token) + { + var query = GetQueryReadOnly(); + var result = await query + .Where(e => !systems.Any() || systems.Contains(e.System.SystemId)) + .GroupBy(e => e.System.SystemId, (key, group) => new + { + System = group.FirstOrDefault()!.System.Name, + Categories = group + .Where(g => !categoryIds.Any() || categoryIds.Contains(g.CategoryId)) + }) + .ToArrayAsync(token); var entities = new List(); foreach (var e in result) @@ -81,27 +79,18 @@ namespace Persistence.Repository.Repositories return entities; } - public async Task> GetSystems(CancellationToken token) - { - var entities = await GetDrillingSystems(token); - var result = entities.Select(e => e.Name); - - return result; - } - - public async Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token) - { + public async Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token) + { + await CreateSystemIfNotExist(systemId, token); var entities = new List(); - foreach (var dto in dtos) - { - var entity = dto.Adapt(); - var systems = await GetDrillingSystems(token); - var systemId = systems.FirstOrDefault(e => e.Name.ToLower().Trim() == dto.System.ToLower().Trim())?.SystemId - ?? await CreateDrillingSystem(dto.System, token); + foreach (var dto in dtos) + { + var entity = dto.Adapt(); + + await CreateSystemIfNotExist(systemId, token); entity.SystemId = systemId; - entity.UserId = userId; entities.Add(entity); } @@ -125,54 +114,50 @@ namespace Persistence.Repository.Repositories return dtos; } - public async Task GetDatesRangeAsync(CancellationToken token) - { - var query = GetQueryReadOnly() - .GroupBy(e => 1) - .Select(group => new - { - Min = group.Min(e => e.Timestamp), - Max = group.Max(e => e.Timestamp), - }); - var values = await query.FirstOrDefaultAsync(token); - var result = new DatesRangeDto() - { - From = values?.Min ?? DateTimeOffset.MinValue, - To = values?.Max ?? DateTimeOffset.MaxValue - }; + public async Task> GetSystems(CancellationToken token) + { + var systems = await sourceSystemRepository.Get(token); + + return systems!; + } + + public async Task GetDatesRangeAsync(CancellationToken token) + { + var query = GetQueryReadOnly() + .GroupBy(e => 1) + .Select(group => new + { + Min = group.Min(e => e.Timestamp), + Max = group.Max(e => e.Timestamp), + }); + + var values = await query.FirstOrDefaultAsync(token); + if (values == null) + return null; + + var result = new DatesRangeDto() + { + From = values?.Min ?? DateTimeOffset.MinValue, + To = values?.Max ?? DateTimeOffset.MaxValue + }; return result; } - private async Task> GetDrillingSystems(CancellationToken token) - { - var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => - { - f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes); + private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token) + { + var systems = await sourceSystemRepository.Get(token); + var system = systems?.FirstOrDefault(e => e.SystemId == systemId); - var query = db.Set(); - var entities = await query.ToListAsync(token); - var dtos = entities.Select(e => e.Adapt()); - - return dtos; - }); - - return systems!; - } - private async Task CreateDrillingSystem(string name, CancellationToken token) - { - memoryCache.Remove(SystemCacheKey); - - var entity = new DrillingSystem() - { - SystemId = Uuid7.Guid(), - Name = name.ToLower().Trim() - }; - - await db.Set().AddAsync(entity, token); - await db.SaveChangesAsync(token); - - return entity.SystemId; - } - } + if (system == null) + { + system = new DataSourceSystemDto() + { + SystemId = systemId, + Name = string.Empty + }; + await sourceSystemRepository.Add(system, token); + } + } + } } diff --git a/Persistence/Models/DrillingSystemDto.cs b/Persistence/Models/DataSourceSystemDto.cs similarity index 61% rename from Persistence/Models/DrillingSystemDto.cs rename to Persistence/Models/DataSourceSystemDto.cs index ad71ee8..56df611 100644 --- a/Persistence/Models/DrillingSystemDto.cs +++ b/Persistence/Models/DataSourceSystemDto.cs @@ -1,9 +1,9 @@ -namespace Persistence.Models; +namespace Persistence.Models; /// -/// Модель системы автобурения +/// Модель системы - источника данных /// -public class DrillingSystemDto +public class DataSourceSystemDto { /// /// Ключ @@ -13,7 +13,7 @@ public class DrillingSystemDto /// /// Наименование /// - public required string Name { get; set; } + public required string Name { get; set; } = string.Empty; /// /// Описание diff --git a/Persistence/Models/Enumerations/EventState.cs b/Persistence/Models/Enumerations/EventState.cs new file mode 100644 index 0000000..3f4103c --- /dev/null +++ b/Persistence/Models/Enumerations/EventState.cs @@ -0,0 +1,6 @@ +namespace Persistence.Models.Enumerations; +public enum EventState +{ + NotTriggered = 0, + Triggered = 1, +} diff --git a/Persistence/Models/TechMessageDto.cs b/Persistence/Models/TechMessageDto.cs index 927ba83..e076443 100644 --- a/Persistence/Models/TechMessageDto.cs +++ b/Persistence/Models/TechMessageDto.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; +using Persistence.Models.Enumerations; namespace Persistence.Models { @@ -24,29 +25,16 @@ namespace Persistence.Models /// public DateTimeOffset Timestamp { get; set; } - /// - /// Глубина забоя - /// - [Range(0, double.MaxValue, ErrorMessage = "Глубина забоя не может быть меньше 0")] - public double? Depth { get; set; } + /// + /// Текст сообщения + /// + [Required] + [StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")] + public required string Text { get; set; } - /// - /// Текст сообщения - /// - [Required] - [StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")] - public required string MessageText { get; set; } - - /// - /// Система автобурения, к которой относится сообщение - /// - [Required] - [StringLength(256, MinimumLength = 1, ErrorMessage = "Допустимая длина наименования системы АБ от 1 до 256 символов")] - public required string System { get; set; } - - /// - /// Id пользователя за пультом бурильщика - /// - public Guid UserId { get; set; } - } + /// + /// Статус события + /// + public EventState EventState { get; set; } + } } diff --git a/Persistence/Repositories/IDataSourceSystemRepository.cs b/Persistence/Repositories/IDataSourceSystemRepository.cs new file mode 100644 index 0000000..d6cf29c --- /dev/null +++ b/Persistence/Repositories/IDataSourceSystemRepository.cs @@ -0,0 +1,22 @@ +using Persistence.Models; + +namespace Persistence.Repositories; + +/// +/// Интерфейс по работе с системами - источниками данных +/// +public interface IDataSourceSystemRepository +{ + /// + /// Добавить систему + /// + /// + /// + public Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token); + + /// + /// Получить список систем + /// + /// + public Task> Get(CancellationToken token); +} diff --git a/Persistence/Repositories/ITechMessagesRepository.cs b/Persistence/Repositories/ITechMessagesRepository.cs index ae92912..ebb7563 100644 --- a/Persistence/Repositories/ITechMessagesRepository.cs +++ b/Persistence/Repositories/ITechMessagesRepository.cs @@ -22,14 +22,14 @@ namespace Persistence.Repositories /// /// /// - Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token); /// - /// Получение списка уникальных названий систем АБ + /// Получение списка систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); /// /// Получение количества сообщений по категориям и системам автобурения @@ -38,7 +38,7 @@ namespace Persistence.Repositories /// Система автобурения /// /// - Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); + Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); /// /// Получить порцию записей, начиная с заданной даты @@ -54,6 +54,6 @@ namespace Persistence.Repositories /// /// /// - Task GetDatesRangeAsync(CancellationToken token); + Task GetDatesRangeAsync(CancellationToken token); } }