From 9e55d6791cdbad56db77a6cbd3e4b112a024b51d Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 20 Nov 2024 15:29:58 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D1=81=D1=82=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF=D0=BE=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82=D0=B0=D0=BC=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SetpointController.cs | 19 +-- Persistence.Database/Entity/Setpoint.cs | 3 +- .../Entity/SetpointDictionary.cs | 18 --- .../Clients/ISetpointClient.cs | 21 ++-- .../Controllers/SetpointControllerTest.cs | 108 +++++++++--------- Persistence.Repository/Data/SetpointDto.cs | 18 +++ .../Repositories/SetpointRepository.cs | 40 +++---- .../Repositories/ISetpointRepository.cs | 2 +- 8 files changed, 116 insertions(+), 113 deletions(-) delete mode 100644 Persistence.Database/Entity/SetpointDictionary.cs diff --git a/Persistence.API/Controllers/SetpointController.cs b/Persistence.API/Controllers/SetpointController.cs index 519dec9..14f966c 100644 --- a/Persistence.API/Controllers/SetpointController.cs +++ b/Persistence.API/Controllers/SetpointController.cs @@ -15,36 +15,37 @@ namespace Persistence.API.Controllers this.setpointRepository = setpointRepository; } - [HttpPost("current")] - public async Task>> GetCurrent(IEnumerable setpointKeys, CancellationToken token) + [HttpGet("current")] + public async Task>> GetCurrent([FromQuery] IEnumerable setpointKeys, CancellationToken token) { var result = await setpointRepository.GetCurrent(setpointKeys, token); return Ok(result); } - [HttpPost("history")] - public async Task>> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) + [HttpGet("history")] + public async Task>> GetHistory([FromQuery] IEnumerable setpointKeys, [FromQuery] DateTimeOffset historyMoment, CancellationToken token) { var result = await setpointRepository.GetHistory(setpointKeys, historyMoment, token); return Ok(result); } - [HttpPost("log")] - public async Task>>> GetLog([FromBody] IEnumerable setpointKeys, CancellationToken token) + [HttpGet("log")] + public async Task>>> GetLog([FromQuery] IEnumerable setpointKeys, CancellationToken token) { var result = await setpointRepository.GetLog(setpointKeys, token); return Ok(result); } - [HttpPost("save")] + [HttpPost] public async Task> Save(Guid setpointKey, object newValue, CancellationToken token) { - var result = await setpointRepository.Save(setpointKey, newValue, 0, token); + // ToDo: вычитка idUser + await setpointRepository.Save(setpointKey, newValue, 0, token); - return Ok(result); + return Ok(); } } } diff --git a/Persistence.Database/Entity/Setpoint.cs b/Persistence.Database/Entity/Setpoint.cs index c1b8037..ef6b5dc 100644 --- a/Persistence.Database/Entity/Setpoint.cs +++ b/Persistence.Database/Entity/Setpoint.cs @@ -1,6 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace Persistence.Database.Model { @@ -13,7 +12,7 @@ namespace Persistence.Database.Model [Column(TypeName = "jsonb"), Comment("Значение уставки")] public required object Value { get; set; } - [Comment("Дата изменения уставки")] + [Comment("Дата создания уставки")] public DateTimeOffset Created { get; set; } [Comment("Id автора последнего изменения")] diff --git a/Persistence.Database/Entity/SetpointDictionary.cs b/Persistence.Database/Entity/SetpointDictionary.cs deleted file mode 100644 index cd20624..0000000 --- a/Persistence.Database/Entity/SetpointDictionary.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Internal; - -namespace Persistence.Database.Model -{ - public class SetpointDictionary - { - [Key, Comment("Ключ")] - public Guid Key { get; set; } - - [Comment("Наименование")] - public required string Name { get; set; } - - [Comment("Описание")] - public string? Description { get; set; } - } -} diff --git a/Persistence.IntegrationTests/Clients/ISetpointClient.cs b/Persistence.IntegrationTests/Clients/ISetpointClient.cs index 2a526f5..a4f79b5 100644 --- a/Persistence.IntegrationTests/Clients/ISetpointClient.cs +++ b/Persistence.IntegrationTests/Clients/ISetpointClient.cs @@ -3,18 +3,23 @@ using Refit; namespace Persistence.IntegrationTests.Clients { + /// + /// Интерфейс для тестирования API, предназначенного для работы с уставками + /// public interface ISetpointClient { - [Post("/current")] - Task>> GetCurrent(IEnumerable setpointKeys); + private const string BaseRoute = "/api/setpoint"; + + [Get($"{BaseRoute}/current")] + Task>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable setpointKeys); - [Post("/history")] - Task>> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment); + [Get($"{BaseRoute}/history")] + Task>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable setpointKeys, [Query] DateTimeOffset historyMoment); - [Post("/log")] - Task>>> GetLog(IEnumerable setpoitKeys); + [Get($"{BaseRoute}/log")] + Task>>> GetLog([Query(CollectionFormat.Multi)] IEnumerable setpointKeys); - [Post("/save")] - Task> Save(Guid setpointKey, object newValue); + [Post($"{BaseRoute}/")] + Task Save(Guid setpointKey, object newValue); } } diff --git a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs index 0401575..2fcfc52 100644 --- a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs @@ -1,6 +1,4 @@ using System.Net; -using System.Text.Json; -using Mapster; using Persistence.IntegrationTests.Clients; using Xunit; @@ -16,8 +14,6 @@ namespace Persistence.IntegrationTests.Controllers } public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory) { - factory.ClientOptions.BaseAddress = new Uri($"http://localhost/api/Setpoint"); - client = factory.GetHttpClient(string.Empty); } @@ -40,6 +36,22 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response.Content); } + [Fact] + public async Task GetCurrent_AfterSave_returns_success() + { + //arrange + var setpointKey = await Save(); + + //act + var response = await client.GetCurrent([setpointKey]); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotEmpty(response.Content); + Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey); + } + [Fact] public async Task GetHistory_returns_success() { @@ -49,7 +61,7 @@ namespace Persistence.IntegrationTests.Controllers Guid.NewGuid(), Guid.NewGuid() }; - var historyMoment = DateTimeOffset.Now.ToUniversalTime(); + var historyMoment = DateTimeOffset.UtcNow; //act var response = await client.GetHistory(setpointKeys, historyMoment); @@ -60,6 +72,24 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response.Content); } + [Fact] + public async Task GetHistory_AfterSave_returns_success() + { + //arrange + var setpointKey = await Save(); + var historyMoment = DateTimeOffset.UtcNow; + historyMoment = historyMoment.AddDays(1); + + //act + var response = await client.GetHistory([setpointKey], historyMoment); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotEmpty(response.Content); + Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey); + } + [Fact] public async Task GetLog_returns_success() { @@ -79,8 +109,29 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response.Content); } + [Fact] + public async Task GetLog_AfterSave_returns_success() + { + //arrange + var setpointKey = await Save(); + + //act + var response = await client.GetLog([setpointKey]); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.NotEmpty(response.Content); + Assert.Equal(response.Content.FirstOrDefault().Value.FirstOrDefault()?.Key, setpointKey); + } + [Fact] public async Task Save_returns_success() + { + await Save(); + } + + private async Task Save() { //arrange var setpointKey = Guid.NewGuid(); @@ -95,53 +146,8 @@ namespace Persistence.IntegrationTests.Controllers //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal(1, response.Content); - } - [Fact] - public async Task General_test_success() - { - //save - var setpointKey = Guid.NewGuid(); - var setpointValue = new TestObject() - { - value1 = "1", - value2 = 2 - }; - - var saveResponse = await client.Save(setpointKey, setpointValue); - Assert.Equal(HttpStatusCode.OK, saveResponse.StatusCode); - Assert.Equal(1, saveResponse.Content); - - //current - var currentResponse = await client.GetCurrent([setpointKey]); - Assert.Equal(HttpStatusCode.OK, currentResponse.StatusCode); - - var currentContent = currentResponse.Content; - Assert.NotNull(currentContent); - Assert.NotEmpty(currentContent); - - var currentContentValue = currentContent.FirstOrDefault()?.Value?.ToString(); - Assert.NotNull(currentContentValue); - Assert.NotEmpty(currentContentValue); - - var testObjectValue = JsonSerializer.Deserialize(currentContentValue); - Assert.NotNull(testObjectValue); - Assert.Equal(setpointValue.value1, testObjectValue.value1); - Assert.Equal(setpointValue.value2, testObjectValue.value2); - - //history - var historyMoment = DateTimeOffset.Now.ToUniversalTime(); - var historyResponse = await client.GetHistory([setpointKey], historyMoment); - Assert.Equal(HttpStatusCode.OK, historyResponse.StatusCode); - Assert.NotNull(historyResponse.Content); - Assert.NotEmpty(historyResponse.Content); - - //log - var logResponse = await client.GetLog([setpointKey]); - Assert.Equal(HttpStatusCode.OK, logResponse.StatusCode); - Assert.NotNull(logResponse.Content); - Assert.NotEmpty(logResponse.Content); + return setpointKey; } } } diff --git a/Persistence.Repository/Data/SetpointDto.cs b/Persistence.Repository/Data/SetpointDto.cs index 261b1bc..4a20aa4 100644 --- a/Persistence.Repository/Data/SetpointDto.cs +++ b/Persistence.Repository/Data/SetpointDto.cs @@ -1,10 +1,28 @@ namespace Persistence.Repository.Data { + /// + /// Модель для работы с уставкой + /// public class SetpointDto { + /// + /// Идентификатор уставки + /// public int Id { get; set; } + + /// + /// Значение уставки + /// public required object Value { get; set; } + + /// + /// Дата сохранения уставки + /// public DateTimeOffset Edit { get; set; } + + /// + /// Ключ пользователя + /// public int IdUser { get; set; } } } diff --git a/Persistence.Repository/Repositories/SetpointRepository.cs b/Persistence.Repository/Repositories/SetpointRepository.cs index ada1c82..2c08e71 100644 --- a/Persistence.Repository/Repositories/SetpointRepository.cs +++ b/Persistence.Repository/Repositories/SetpointRepository.cs @@ -31,9 +31,13 @@ namespace Persistence.Repository.Repositories { var query = GetQueryReadOnly(); var entities = await query - .Where(e => setpointKeys.Contains(e.Key) && e.Created.Date == historyMoment.Date) + .Where(e => setpointKeys.Contains(e.Key)) .ToArrayAsync(token); - var dtos = entities.Select(e => e.Adapt()); + var filteredEntities = entities + .GroupBy(e => e.Key) + .Select(e => e.Where(e => e.Created <= historyMoment).Last()); + var dtos = filteredEntities + .Select(e => e.Adapt()); return dtos; } @@ -46,35 +50,23 @@ namespace Persistence.Repository.Repositories .ToArrayAsync(token); var dtos = entities .GroupBy(e => e.Key) - .Select(e => new KeyValuePair>( - e.Key, - e.Select(s => s.Adapt()) - )).ToDictionary(); + .ToDictionary(e => e.Key, v => v.Select(z => z.Adapt())); return dtos; } - public async Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token) + public async Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token) { - try + var entity = new Setpoint() { - var entity = new Setpoint() - { - Key = setpointKey, - Value = newValue, - IdUser = idUser, - Created = DateTimeOffset.Now.ToUniversalTime() - }; + Key = setpointKey, + Value = newValue, + IdUser = idUser, + Created = DateTimeOffset.UtcNow + }; - await db.Set().AddAsync(entity, token); - var result = await db.SaveChangesAsync(token); - - return result; - } - catch (Exception) - { - return 0; - } + await db.Set().AddAsync(entity, token); + await db.SaveChangesAsync(token); } } } diff --git a/Persistence/Repositories/ISetpointRepository.cs b/Persistence/Repositories/ISetpointRepository.cs index 6d2690e..1d82b16 100644 --- a/Persistence/Repositories/ISetpointRepository.cs +++ b/Persistence/Repositories/ISetpointRepository.cs @@ -42,5 +42,5 @@ public interface ISetpointRepository /// /// to do /// id User учесть в соответствующем методе репозитория - Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token); + Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token); }