diff --git a/Persistence.API/Controllers/SetpointController.cs b/Persistence.API/Controllers/SetpointController.cs index 3e8e94c..519dec9 100644 --- a/Persistence.API/Controllers/SetpointController.cs +++ b/Persistence.API/Controllers/SetpointController.cs @@ -16,31 +16,33 @@ namespace Persistence.API.Controllers } [HttpPost("current")] - public Task>> GetCurrentAsync(IEnumerable setpointKeys, CancellationToken token) + public async Task>> GetCurrent(IEnumerable setpointKeys, CancellationToken token) { - throw new NotImplementedException(); + var result = await setpointRepository.GetCurrent(setpointKeys, token); + + return Ok(result); } [HttpPost("history")] - public async Task>> GetHistoryAsync(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) + public async Task>> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { - var result = await setpointRepository.GetHistoryAsync(setpointKeys, historyMoment, token); + var result = await setpointRepository.GetHistory(setpointKeys, historyMoment, token); return Ok(result); } [HttpPost("log")] - public async Task>>> GetLogAsync([FromBody] IEnumerable setpointKeys, CancellationToken token) + public async Task>>> GetLog([FromBody] IEnumerable setpointKeys, CancellationToken token) { - var result = await setpointRepository.GetLogAsync(setpointKeys, token); + var result = await setpointRepository.GetLog(setpointKeys, token); return Ok(result); } [HttpPost("save")] - public async Task> SaveAsync(Guid setpointKey, object newValue, CancellationToken token) + public async Task> Save(Guid setpointKey, object newValue, CancellationToken token) { - var result = await setpointRepository.SaveAsync(setpointKey, newValue, token); + var result = await setpointRepository.Save(setpointKey, newValue, 0, token); return Ok(result); } diff --git a/Persistence.Database/Entity/ISetpointData.cs b/Persistence.Database/Entity/ISetpointData.cs deleted file mode 100644 index 630af70..0000000 --- a/Persistence.Database/Entity/ISetpointData.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Persistence.Database.Model -{ - public interface ISetpointData - { - public Guid Key { get; set; } - public object Value { get; set; } - public DateTimeOffset Created { get; set; } - public int IdUser { get; set; } - } -} diff --git a/Persistence.Database/Entity/Setpoint.cs b/Persistence.Database/Entity/Setpoint.cs index 50d7f24..c1b8037 100644 --- a/Persistence.Database/Entity/Setpoint.cs +++ b/Persistence.Database/Entity/Setpoint.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace Persistence.Database.Model { [PrimaryKey(nameof(Key), nameof(Created))] - public class Setpoint : ISetpointData + public class Setpoint { [Comment("Ключ")] public Guid Key { get; set; } diff --git a/Persistence.IntegrationTests/Clients/ISetpointClient.cs b/Persistence.IntegrationTests/Clients/ISetpointClient.cs index 831f119..2a526f5 100644 --- a/Persistence.IntegrationTests/Clients/ISetpointClient.cs +++ b/Persistence.IntegrationTests/Clients/ISetpointClient.cs @@ -5,13 +5,16 @@ namespace Persistence.IntegrationTests.Clients { public interface ISetpointClient { - [Post("/api/Setpoint/history")] - Task>> GetHistoryAsync(IEnumerable setpointKeys, DateTimeOffset historyMoment); + [Post("/current")] + Task>> GetCurrent(IEnumerable setpointKeys); + + [Post("/history")] + Task>> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment); - [Post("/api/Setpoint/log")] - Task>>> GetLogAsync(IEnumerable setpoitKeys); + [Post("/log")] + Task>>> GetLog(IEnumerable setpoitKeys); - [Post("/api/Setpoint/save")] - Task> SaveAsync(Guid setpointKey, object newValue); + [Post("/save")] + Task> Save(Guid setpointKey, object newValue); } } diff --git a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs index d82e5af..0401575 100644 --- a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs @@ -1,4 +1,6 @@ using System.Net; +using System.Text.Json; +using Mapster; using Persistence.IntegrationTests.Clients; using Xunit; @@ -10,16 +12,36 @@ namespace Persistence.IntegrationTests.Controllers private class TestObject { public string? value1 { get; set; } - public int value2 { get; set; } - public double value3 { get; set; } + public int? value2 { get; set; } } public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory) { + factory.ClientOptions.BaseAddress = new Uri($"http://localhost/api/Setpoint"); + client = factory.GetHttpClient(string.Empty); } [Fact] - public async Task GetHistoryAsync_returns_success() + public async Task GetCurrent_returns_success() + { + //arrange + var setpointKeys = new List() + { + Guid.NewGuid(), + Guid.NewGuid() + }; + + //act + var response = await client.GetCurrent(setpointKeys); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Empty(response.Content); + } + + [Fact] + public async Task GetHistory_returns_success() { //arrange var setpointKeys = new List() @@ -30,14 +52,16 @@ namespace Persistence.IntegrationTests.Controllers var historyMoment = DateTimeOffset.Now.ToUniversalTime(); //act - var response = await client.GetHistoryAsync(setpointKeys, historyMoment); + var response = await client.GetHistory(setpointKeys, historyMoment); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Empty(response.Content); } [Fact] - public async Task GetLogAsync_returns_success() + public async Task GetLog_returns_success() { //arrange var setpointKeys = new List() @@ -47,30 +71,77 @@ namespace Persistence.IntegrationTests.Controllers }; //act - var response = await client.GetLogAsync(setpointKeys); + var response = await client.GetLog(setpointKeys); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Empty(response.Content); } [Fact] - public async Task SaveAsync_returns_success() + public async Task Save_returns_success() { //arrange var setpointKey = Guid.NewGuid(); var setpointValue = new TestObject() { value1 = "1", - value2 = 2, - value3 = 3.3 + value2 = 2 }; //act - var response = await client.SaveAsync(setpointKey, setpointValue); + var response = await client.Save(setpointKey, setpointValue); //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); + } } } diff --git a/Persistence.Repository/Repositories/SetpointRepository.cs b/Persistence.Repository/Repositories/SetpointRepository.cs index ef346e6..ada1c82 100644 --- a/Persistence.Repository/Repositories/SetpointRepository.cs +++ b/Persistence.Repository/Repositories/SetpointRepository.cs @@ -16,7 +16,18 @@ namespace Persistence.Repository.Repositories protected virtual IQueryable GetQueryReadOnly() => db.Set(); - public async Task> GetHistoryAsync(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) + public async Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token) + { + var query = GetQueryReadOnly(); + var entities = await query + .Where(e => setpointKeys.Contains(e.Key)) + .ToArrayAsync(token); + var dtos = entities.Select(e => e.Adapt()); + + return dtos; + } + + public async Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { var query = GetQueryReadOnly(); var entities = await query @@ -27,7 +38,7 @@ namespace Persistence.Repository.Repositories return dtos; } - public async Task>> GetLogAsync(IEnumerable setpointKeys, CancellationToken token) + public async Task>> GetLog(IEnumerable setpointKeys, CancellationToken token) { var query = GetQueryReadOnly(); var entities = await query @@ -43,7 +54,7 @@ namespace Persistence.Repository.Repositories return dtos; } - public async Task SaveAsync(Guid setpointKey, object newValue, CancellationToken token) + public async Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token) { try { @@ -51,7 +62,7 @@ namespace Persistence.Repository.Repositories { Key = setpointKey, Value = newValue, - IdUser = 0, // ToDo: откуда тянуть? + IdUser = idUser, Created = DateTimeOffset.Now.ToUniversalTime() }; diff --git a/Persistence/API/ISetpointApi.cs b/Persistence/API/ISetpointApi.cs index d086557..7af0895 100644 --- a/Persistence/API/ISetpointApi.cs +++ b/Persistence/API/ISetpointApi.cs @@ -14,7 +14,7 @@ public interface ISetpointApi /// ключи уставок /// /// - Task>> GetCurrentAsync(IEnumerable setpoitKeys, CancellationToken token); + Task>> GetCurrent(IEnumerable setpoitKeys, CancellationToken token); /// /// Получить значения уставок за определенный момент времени @@ -23,7 +23,7 @@ public interface ISetpointApi /// дата, на которую получаем данные /// /// - Task>> GetHistoryAsync(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); + Task>> GetHistory(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); /// /// Получить историю изменений значений уставок @@ -31,7 +31,7 @@ public interface ISetpointApi /// ключи уставок /// /// - Task>>> GetLogAsync(IEnumerable setpoitKeys, CancellationToken token); + Task>>> GetLog(IEnumerable setpoitKeys, CancellationToken token); /// /// Метод сохранения уставки @@ -40,5 +40,5 @@ public interface ISetpointApi /// значение /// /// - Task> SaveAsync(Guid setpointKey, object newValue, CancellationToken token); + Task> Save(Guid setpointKey, object newValue, CancellationToken token); } diff --git a/Persistence/Repositories/ISetpointRepository.cs b/Persistence/Repositories/ISetpointRepository.cs index d315725..f7439fc 100644 --- a/Persistence/Repositories/ISetpointRepository.cs +++ b/Persistence/Repositories/ISetpointRepository.cs @@ -7,15 +7,22 @@ namespace Persistence.Repositories; /// public interface ISetpointRepository { - - /// - /// Получить значения уставок за определенный момент времени - /// - /// - /// дата, на которую получаем данные - /// - /// - Task> GetHistoryAsync(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token); + /// + /// Получить значения уставок по набору ключей + /// + /// + /// + /// + Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token); + + /// + /// Получить значения уставок за определенный момент времени + /// + /// + /// дата, на которую получаем данные + /// + /// + Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token); /// /// Получить историю изменений значений уставок @@ -23,7 +30,7 @@ public interface ISetpointRepository /// /// /// - Task>> GetLogAsync(IEnumerable setpointKeys, CancellationToken token); + Task>> GetLog(IEnumerable setpointKeys, CancellationToken token); /// /// Метод сохранения уставки @@ -35,5 +42,5 @@ public interface ISetpointRepository /// /// to do /// id User учесть в соответствующем методе репозитория - Task SaveAsync(Guid setpointKey, object newValue, CancellationToken token); + Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token); }