Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
e2b2fed68f | |||
ee5425cf6d | |||
457579c88d | |||
2a0fa0f3f9 | |||
5497b89c3b | |||
d5942cd670 | |||
e50bd64be1 |
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using DD.Persistence.Models;
|
using DD.Persistence.Models;
|
||||||
using DD.Persistence.Repositories;
|
using DD.Persistence.Repositories;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.API.Controllers;
|
namespace DD.Persistence.API.Controllers;
|
||||||
|
|
||||||
@ -28,9 +29,9 @@ public class SetpointController : ControllerBase, ISetpointApi
|
|||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("current")]
|
[HttpGet("current")]
|
||||||
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
|
public async Task<ActionResult<Dictionary<Guid, object>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
|
||||||
{
|
{
|
||||||
var result = await setpointRepository.GetCurrent(setpointKeys, token);
|
var result = await setpointRepository.GetCurrentDictionary(setpointKeys, token);
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
@ -104,7 +105,7 @@ public class SetpointController : ControllerBase, ISetpointApi
|
|||||||
public async Task<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token)
|
public async Task<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token)
|
||||||
{
|
{
|
||||||
var userId = User.GetUserId<Guid>();
|
var userId = User.GetUserId<Guid>();
|
||||||
await setpointRepository.Add(setpointKey, newValue, userId, token);
|
await setpointRepository.Add(setpointKey, (JsonElement)newValue, userId, token);
|
||||||
|
|
||||||
return CreatedAtAction(nameof(Add), true);
|
return CreatedAtAction(nameof(Add), true);
|
||||||
}
|
}
|
||||||
|
@ -24,12 +24,21 @@ public interface ISetpointClient : IDisposable
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
/// Получить актуальные значения уставок
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="token"></param>
|
/// <param name="setpointConfigs"></param>
|
||||||
/// <returns></returns>
|
/// <param name="token"></param>
|
||||||
Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token);
|
/// <returns></returns>s
|
||||||
|
Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointConfigs, CancellationToken token);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить значения уставок за определенный момент времени
|
/// Получить значения уставок за определенный момент времени
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using DD.Persistence.Models;
|
using DD.Persistence.Models;
|
||||||
using Refit;
|
using Refit;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.Client.Clients.Interfaces.Refit;
|
namespace DD.Persistence.Client.Clients.Interfaces.Refit;
|
||||||
|
|
||||||
@ -7,8 +8,11 @@ public interface IRefitSetpointClient : IRefitClient, IDisposable
|
|||||||
{
|
{
|
||||||
private const string BaseRoute = "/api/setpoint";
|
private const string BaseRoute = "/api/setpoint";
|
||||||
|
|
||||||
|
//[Get($"{BaseRoute}/current")]
|
||||||
|
//Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||||
|
|
||||||
[Get($"{BaseRoute}/current")]
|
[Get($"{BaseRoute}/current")]
|
||||||
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token);
|
Task<IApiResponse<Dictionary<Guid, JsonElement>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||||
|
|
||||||
[Get($"{BaseRoute}/history")]
|
[Get($"{BaseRoute}/history")]
|
||||||
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, [Query] DateTimeOffset historyMoment, CancellationToken token);
|
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, [Query] DateTimeOffset historyMoment, CancellationToken token);
|
||||||
|
@ -3,27 +3,76 @@ using DD.Persistence.Client.Clients.Base;
|
|||||||
using DD.Persistence.Client.Clients.Interfaces;
|
using DD.Persistence.Client.Clients.Interfaces;
|
||||||
using DD.Persistence.Client.Clients.Interfaces.Refit;
|
using DD.Persistence.Client.Clients.Interfaces.Refit;
|
||||||
using DD.Persistence.Models;
|
using DD.Persistence.Models;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace DD.Persistence.Client.Clients;
|
namespace DD.Persistence.Client.Clients;
|
||||||
|
|
||||||
public class SetpointClient : BaseClient, ISetpointClient
|
public class SetpointClient : BaseClient, ISetpointClient
|
||||||
{
|
{
|
||||||
private readonly IRefitSetpointClient refitSetpointClient;
|
private readonly IRefitSetpointClient refitSetpointClient;
|
||||||
|
private readonly ISetpointConfigStorage setpointConfigStorage;
|
||||||
|
|
||||||
public SetpointClient(IRefitClientFactory<IRefitSetpointClient> refitSetpointClientFactory, ILogger<SetpointClient> logger) : base(logger)
|
public SetpointClient(
|
||||||
|
IRefitClientFactory<IRefitSetpointClient> refitSetpointClientFactory,
|
||||||
|
ISetpointConfigStorage setpointConfigStorage,
|
||||||
|
ILogger<SetpointClient> logger) : base(logger)
|
||||||
{
|
{
|
||||||
this.refitSetpointClient = refitSetpointClientFactory.Create();
|
this.refitSetpointClient = refitSetpointClientFactory.Create();
|
||||||
}
|
this.setpointConfigStorage = setpointConfigStorage;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
||||||
{
|
{
|
||||||
var result = await ExecuteGetResponse(
|
var result = await ExecuteGetResponse(
|
||||||
async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token);
|
async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token);
|
||||||
|
|
||||||
return result!;
|
return result!.Select(x => new SetpointValueDto {
|
||||||
|
Key = x.Key,
|
||||||
|
Value = DeserializeValue(x.Key, x.Value)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
private object DeserializeValue(Guid key, JsonElement value)
|
||||||
|
{
|
||||||
|
if (setpointConfigStorage.TryGetType(key, out var type))
|
||||||
|
return value.Deserialize(type)!;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointConfigs, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = await ExecuteGetResponse(
|
||||||
|
async () => await refitSetpointClient.GetCurrent(setpointConfigs, token), token);
|
||||||
|
|
||||||
|
|
||||||
|
return result!.ToDictionary(x => x.Key,x => DeserializeValue(x.Key,x.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//private Dictionary<Guid, object?> DeserializeResultToDict(IEnumerable<SetpointValueDto> data)
|
||||||
|
//{
|
||||||
|
// var dict = new Dictionary<Guid, object?>();
|
||||||
|
|
||||||
|
|
||||||
|
// foreach (var valueDto in data)
|
||||||
|
// {
|
||||||
|
// if (valueDto.Value is not null &&
|
||||||
|
// valueDto.Value is JsonElement element &&
|
||||||
|
// setpointConfigStorage.TryGetType(valueDto.Key, out var type) &&
|
||||||
|
// type is not null)
|
||||||
|
|
||||||
|
// dict[valueDto.Key] = element.Deserialize(type) ?? valueDto.Value;
|
||||||
|
// else
|
||||||
|
// dict[valueDto.Key] = valueDto.Value;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return dict;
|
||||||
|
//}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
||||||
{
|
{
|
||||||
var result = await ExecuteGetResponse(
|
var result = await ExecuteGetResponse(
|
||||||
async () => await refitSetpointClient.GetHistory(setpointKeys, historyMoment, token), token);
|
async () => await refitSetpointClient.GetHistory(setpointKeys, historyMoment, token), token);
|
||||||
@ -67,4 +116,6 @@ public class SetpointClient : BaseClient, ISetpointClient
|
|||||||
|
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@
|
|||||||
<!--Наименование-->
|
<!--Наименование-->
|
||||||
<Title>DD.Persistence.Client</Title>
|
<Title>DD.Persistence.Client</Title>
|
||||||
<!--Версия пакета-->
|
<!--Версия пакета-->
|
||||||
<VersionPrefix>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</VersionPrefix>
|
<VersionPrefix>1.4.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH)).1</VersionPrefix>
|
||||||
<!--Версия сборки-->
|
<!--Версия сборки-->
|
||||||
<AssemblyVersion>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</AssemblyVersion>
|
<AssemblyVersion>1.4.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH)).1</AssemblyVersion>
|
||||||
<!--Id пакета-->
|
<!--Id пакета-->
|
||||||
<PackageId>DD.Persistence.Client</PackageId>
|
<PackageId>DD.Persistence.Client</PackageId>
|
||||||
|
|
||||||
@ -33,15 +33,15 @@
|
|||||||
<!--Формат пакета с символами-->
|
<!--Формат пакета с символами-->
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<!--Путь к пакету-->
|
<!--Путь к пакету-->
|
||||||
<PackageOutputPath>C:\Projects\Nuget\Persistence\Client</PackageOutputPath>
|
<PackageOutputPath>C:\Projects\Nuget\Persistence</PackageOutputPath>
|
||||||
|
|
||||||
<!--Readme-->
|
<!--Readme-->
|
||||||
<PackageReadmeFile>Readme.md</PackageReadmeFile>
|
<PackageReadmeFile>Readme.md</PackageReadmeFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</VersionPrefix>
|
<VersionPrefix>1.4.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</VersionPrefix>
|
||||||
<AssemblyVersion>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</AssemblyVersion>
|
<AssemblyVersion>1.4.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</AssemblyVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -15,7 +15,7 @@ public static class DependencyInjection
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddPersistenceClients(this IServiceCollection services)
|
public static IServiceCollection AddPersistenceClients(this IServiceCollection services, Dictionary<Guid, Type>? setpointTypeConfigs = null)
|
||||||
{
|
{
|
||||||
services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>));
|
services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>));
|
||||||
services.AddTransient<IChangeLogClient, ChangeLogClient>();
|
services.AddTransient<IChangeLogClient, ChangeLogClient>();
|
||||||
@ -25,6 +25,11 @@ public static class DependencyInjection
|
|||||||
services.AddTransient<ITimeSeriesClient<DataSaubDto>, TimeSeriesClient<DataSaubDto>>();
|
services.AddTransient<ITimeSeriesClient<DataSaubDto>, TimeSeriesClient<DataSaubDto>>();
|
||||||
services.AddTransient<ITimestampedSetClient, TimestampedSetClient>();
|
services.AddTransient<ITimestampedSetClient, TimestampedSetClient>();
|
||||||
services.AddTransient<IWitsDataClient, WitsDataClient>();
|
services.AddTransient<IWitsDataClient, WitsDataClient>();
|
||||||
|
|
||||||
|
services.AddSingleton<ISetpointConfigStorage, SetpointConfigStorage>(provider =>
|
||||||
|
{
|
||||||
|
return new SetpointConfigStorage(setpointTypeConfigs);
|
||||||
|
});
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
DD.Persistence.Client/ISetpointConfigStorage.cs
Normal file
11
DD.Persistence.Client/ISetpointConfigStorage.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DD.Persistence.Client;
|
||||||
|
public interface ISetpointConfigStorage
|
||||||
|
{
|
||||||
|
bool TryGetType(Guid id, out Type type);
|
||||||
|
}
|
15
DD.Persistence.Client/SetpointConfigStorage.cs
Normal file
15
DD.Persistence.Client/SetpointConfigStorage.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
namespace DD.Persistence.Client;
|
||||||
|
internal class SetpointConfigStorage : ISetpointConfigStorage
|
||||||
|
{
|
||||||
|
private readonly Dictionary<Guid, Type> setpointTypeConfigs;
|
||||||
|
|
||||||
|
public SetpointConfigStorage(Dictionary<Guid, Type>? setpointTypeConfigs)
|
||||||
|
{
|
||||||
|
this.setpointTypeConfigs = setpointTypeConfigs?? new Dictionary<Guid, Type>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetType(Guid id, out Type type)
|
||||||
|
{
|
||||||
|
return setpointTypeConfigs.TryGetValue(id, out type);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.Database.Model
|
namespace DD.Persistence.Database.Model
|
||||||
{
|
{
|
||||||
@ -10,7 +11,7 @@ namespace DD.Persistence.Database.Model
|
|||||||
public Guid Key { get; set; }
|
public Guid Key { get; set; }
|
||||||
|
|
||||||
[Column(TypeName = "jsonb"), Comment("Значение уставки")]
|
[Column(TypeName = "jsonb"), Comment("Значение уставки")]
|
||||||
public required object Value { get; set; }
|
public required JsonElement Value { get; set; }
|
||||||
|
|
||||||
[Comment("Дата создания уставки")]
|
[Comment("Дата создания уставки")]
|
||||||
public DateTimeOffset Created { get; set; }
|
public DateTimeOffset Created { get; set; }
|
||||||
|
@ -11,9 +11,9 @@
|
|||||||
<!--Наименование-->
|
<!--Наименование-->
|
||||||
<Title>DD.Persistence.Models</Title>
|
<Title>DD.Persistence.Models</Title>
|
||||||
<!--Версия пакета-->
|
<!--Версия пакета-->
|
||||||
<VersionPrefix>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</VersionPrefix>
|
<VersionPrefix>1.2.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</VersionPrefix>
|
||||||
<!--Версия сборки-->
|
<!--Версия сборки-->
|
||||||
<AssemblyVersion>1.0.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</AssemblyVersion>
|
<AssemblyVersion>1.2.$([System.DateTime]::UtcNow.ToString(yyMM.ddHH))</AssemblyVersion>
|
||||||
<!--Id пакета-->
|
<!--Id пакета-->
|
||||||
<PackageId>DD.Persistence.Models</PackageId>
|
<PackageId>DD.Persistence.Models</PackageId>
|
||||||
|
|
||||||
@ -33,7 +33,7 @@
|
|||||||
<!--Формат пакета с символами-->
|
<!--Формат пакета с символами-->
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<!--Путь к пакету-->
|
<!--Путь к пакету-->
|
||||||
<PackageOutputPath>C:\Projects\Nuget\Persistence\Models</PackageOutputPath>
|
<PackageOutputPath>C:\Projects\Nuget\Persistence</PackageOutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
|
<PackageReference Include="Shouldly" Version="4.2.1" />
|
||||||
|
<PackageReference Include="Testcontainers" Version="4.1.0" />
|
||||||
|
<PackageReference Include="Testcontainers.PostgreSql" Version="4.1.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
|
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\DD.Persistence.Database.Postgres\DD.Persistence.Database.Postgres.csproj" />
|
||||||
|
<ProjectReference Include="..\DD.Persistence.Repository\DD.Persistence.Repository.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Xunit" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
32
DD.Persistence.Repository.Test/RepositoryTestFixture.cs
Normal file
32
DD.Persistence.Repository.Test/RepositoryTestFixture.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
using DD.Persistence.Database;
|
||||||
|
using DD.Persistence.Database.Model;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Testcontainers.PostgreSql;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DD.Persistence.Repository.Test;
|
||||||
|
|
||||||
|
public class RepositoryTestFixture : IAsyncLifetime
|
||||||
|
{
|
||||||
|
public readonly PostgreSqlContainer dbContainer = new PostgreSqlBuilder().Build();
|
||||||
|
|
||||||
|
|
||||||
|
public PersistencePostgresContext GetDbContext() => new(new DbContextOptionsBuilder<PersistencePostgresContext>()
|
||||||
|
.UseNpgsql(dbContainer.GetConnectionString()).Options);
|
||||||
|
|
||||||
|
public IMemoryCache GetMemoryCache() => new MemoryCache(new MemoryCacheOptions());
|
||||||
|
|
||||||
|
public virtual async Task InitializeAsync()
|
||||||
|
{
|
||||||
|
await dbContainer.StartAsync();
|
||||||
|
var forumDbContext = new PersistencePostgresContext(new DbContextOptionsBuilder<PersistencePostgresContext>()
|
||||||
|
.UseNpgsql(dbContainer.GetConnectionString()).Options);
|
||||||
|
|
||||||
|
await forumDbContext.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DisposeAsync() => await dbContainer.DisposeAsync();
|
||||||
|
}
|
||||||
|
|
56
DD.Persistence.Repository.Test/SetpointRepositoryShould.cs
Normal file
56
DD.Persistence.Repository.Test/SetpointRepositoryShould.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
using DD.Persistence.Database.Model;
|
||||||
|
using DD.Persistence.Repository.Repositories;
|
||||||
|
using Shouldly;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DD.Persistence.Repository.Test;
|
||||||
|
public class SetpointRepositoryShould : IClassFixture<RepositoryTestFixture>
|
||||||
|
{
|
||||||
|
private readonly RepositoryTestFixture fixture;
|
||||||
|
private readonly PersistencePostgresContext context;
|
||||||
|
private readonly SetpointRepository sut;
|
||||||
|
|
||||||
|
public SetpointRepositoryShould(RepositoryTestFixture fixture)
|
||||||
|
{
|
||||||
|
this.fixture = fixture;
|
||||||
|
context = fixture.GetDbContext();
|
||||||
|
sut = new SetpointRepository(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ReturnValueKindNumber()
|
||||||
|
{
|
||||||
|
var id = Guid.NewGuid();
|
||||||
|
var value = GetJsonFromObject(22);
|
||||||
|
await sut.Add(id, value, Guid.NewGuid(), CancellationToken.None);
|
||||||
|
|
||||||
|
var t = fixture.dbContainer.GetConnectionString();
|
||||||
|
//act
|
||||||
|
var result = await sut.GetCurrent([id], CancellationToken.None);
|
||||||
|
|
||||||
|
|
||||||
|
//assert
|
||||||
|
result.ShouldNotBeNull();
|
||||||
|
result.ShouldNotBeEmpty();
|
||||||
|
|
||||||
|
var setpoint = result.First();
|
||||||
|
|
||||||
|
setpoint.Value.ShouldNotBeNull();
|
||||||
|
setpoint
|
||||||
|
.Value.ShouldBeOfType<JsonElement>()
|
||||||
|
.ValueKind.ShouldBe(JsonValueKind.Number);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonElement GetJsonFromObject(object value)
|
||||||
|
{
|
||||||
|
var jsonString = JsonSerializer.Serialize(value);
|
||||||
|
var doc = JsonDocument.Parse(jsonString);
|
||||||
|
return doc.RootElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using DD.Persistence.Database.Model;
|
using DD.Persistence.Database.Model;
|
||||||
using DD.Persistence.Models;
|
using DD.Persistence.Models;
|
||||||
using DD.Persistence.Repositories;
|
using DD.Persistence.Repositories;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.Repository.Repositories
|
namespace DD.Persistence.Repository.Repositories
|
||||||
{
|
{
|
||||||
@ -16,16 +17,33 @@ namespace DD.Persistence.Repository.Repositories
|
|||||||
|
|
||||||
protected virtual IQueryable<Setpoint> GetQueryReadOnly() => db.Set<Setpoint>();
|
protected virtual IQueryable<Setpoint> GetQueryReadOnly() => db.Set<Setpoint>();
|
||||||
|
|
||||||
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(
|
||||||
|
IEnumerable<Guid> setpointKeys,
|
||||||
|
CancellationToken token)
|
||||||
{
|
{
|
||||||
var query = GetQueryReadOnly();
|
var query = GetQueryReadOnly();
|
||||||
|
|
||||||
var entities = await query
|
var entities = await query
|
||||||
.Where(e => setpointKeys.Contains(e.Key))
|
.Where(e => setpointKeys.Contains(e.Key))
|
||||||
|
.GroupBy(e => e.Key)
|
||||||
|
.Select(g => g.OrderByDescending(x => x.Created).FirstOrDefault())
|
||||||
.ToArrayAsync(token);
|
.ToArrayAsync(token);
|
||||||
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
|
|
||||||
|
|
||||||
|
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
public async Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = GetQueryReadOnly();
|
||||||
|
|
||||||
|
var entities = await query
|
||||||
|
.Where(e => setpointKeys.Contains(e.Key))
|
||||||
|
.GroupBy(e => e.Key)
|
||||||
|
.Select(g => g.OrderByDescending(x => x.Created).FirstOrDefault())
|
||||||
|
.ToDictionaryAsync(x=> x.Key, x => (object)x.Value, token);
|
||||||
|
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
||||||
{
|
{
|
||||||
@ -88,7 +106,7 @@ namespace DD.Persistence.Repository.Repositories
|
|||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token)
|
public async Task Add(Guid setpointKey, JsonElement newValue, Guid idUser, CancellationToken token)
|
||||||
{
|
{
|
||||||
var entity = new Setpoint()
|
var entity = new Setpoint()
|
||||||
{
|
{
|
||||||
@ -101,5 +119,7 @@ namespace DD.Persistence.Repository.Repositories
|
|||||||
await db.Set<Setpoint>().AddAsync(entity, token);
|
await db.Set<Setpoint>().AddAsync(entity, token);
|
||||||
await db.SaveChangesAsync(token);
|
await db.SaveChangesAsync(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DD.Persistence.App", "DD.Pe
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Persistence.Models", "DD.Persistence.Models\DD.Persistence.Models.csproj", "{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Persistence.Models", "DD.Persistence.Models\DD.Persistence.Models.csproj", "{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Persistence.Repository.Test", "DD.Persistence.Repository.Test\DD.Persistence.Repository.Test.csproj", "{08B03623-A1C9-482F-B60E-09F293E04999}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -63,6 +65,10 @@ Global
|
|||||||
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
{698B4571-BB7A-4A42-8B0B-6C7F2F5360FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{08B03623-A1C9-482F-B60E-09F293E04999}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{08B03623-A1C9-482F-B60E-09F293E04999}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{08B03623-A1C9-482F-B60E-09F293E04999}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{08B03623-A1C9-482F-B60E-09F293E04999}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -14,7 +14,7 @@ public interface ISetpointApi : ISyncApi<SetpointLogDto>
|
|||||||
/// <param name="setpoitKeys">ключи уставок</param>
|
/// <param name="setpoitKeys">ключи уставок</param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent(IEnumerable<Guid> setpoitKeys, CancellationToken token);
|
Task<ActionResult<Dictionary<Guid, object>>> GetCurrent(IEnumerable<Guid> setpoitKeys, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить значения уставок за определенный момент времени
|
/// Получить значения уставок за определенный момент времени
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using DD.Persistence.Models;
|
using DD.Persistence.Models;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.Repositories;
|
namespace DD.Persistence.Repositories;
|
||||||
|
|
||||||
@ -15,6 +16,14 @@ public interface ISetpointRepository
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить значения уставок по набору ключей
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="setpointKeys"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить значения уставок за определенный момент времени
|
/// Получить значения уставок за определенный момент времени
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -58,5 +67,5 @@ public interface ISetpointRepository
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// to do
|
/// to do
|
||||||
/// id User учесть в соответствующем методе репозитория
|
/// id User учесть в соответствующем методе репозитория
|
||||||
Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token);
|
Task Add(Guid setpointKey, JsonElement newValue, Guid idUser, CancellationToken token);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user