#408 Репозиторий хранения данных WITS0 #7

Merged
on.nemtina merged 9 commits from WitsData into master 2024-12-12 16:11:56 +05:00
24 changed files with 72 additions and 59 deletions
Showing only changes of commit ba949f2dff - Show all commits

View File

@ -101,11 +101,12 @@ public class SetpointController : ControllerBase, ISetpointApi
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Save(Guid setpointKey, object newValue, Guid idUser, CancellationToken token)
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
public async Task<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token)
{
await setpointRepository.Save(setpointKey, newValue, idUser, token);
var userId = User.GetUserId<Guid>();
await setpointRepository.Add(setpointKey, newValue, userId, token);
return Ok();
return CreatedAtAction(nameof(Add), true);
}
}

View File

@ -107,17 +107,13 @@ public class TechMessagesController : ControllerBase
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
public async Task<IActionResult> InsertRange([FromBody] IEnumerable<TechMessageDto> dtos, CancellationToken token)
public async Task<IActionResult> AddRange([FromBody] IEnumerable<TechMessageDto> dtos, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
foreach (var dto in dtos)
{
dto.UserId = userId;
}
var result = await techMessagesRepository.InsertRange(dtos, token);
var result = await techMessagesRepository.AddRange(dtos, userId, token);
return CreatedAtAction(nameof(InsertRange), result);
return CreatedAtAction(nameof(AddRange), result);
}
/// <summary>

View File

@ -66,9 +66,9 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
public async Task<IActionResult> AddRange(IEnumerable<TDto> dtos, CancellationToken token)
{
var result = await timeSeriesDataRepository.InsertRange(dtos, token);
var result = await timeSeriesDataRepository.AddRange(dtos, token);
return Ok(result);
}

View File

@ -32,9 +32,9 @@ public class TimestampedSetController : ControllerBase
/// <returns>кол-во затронутых записей</returns>
[HttpPost]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> InsertRange([FromRoute]Guid idDiscriminator, [FromBody]IEnumerable<TimestampedSetDto> sets, CancellationToken token)
public async Task<IActionResult> AddRange([FromRoute]Guid idDiscriminator, [FromBody]IEnumerable<TimestampedSetDto> sets, CancellationToken token)
{
var result = await repository.InsertRange(idDiscriminator, sets, token);
var result = await repository.AddRange(idDiscriminator, sets, token);
return Ok(result);
}

View File

@ -26,5 +26,5 @@ public interface ISetpointClient
Task<IApiResponse<IEnumerable<SetpointLogDto>>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token);
[Post($"{BaseRoute}/")]
Task<IApiResponse> Save(Guid setpointKey, object newValue);
Task<IApiResponse> Add(Guid setpointKey, object newValue);
}

View File

@ -14,7 +14,7 @@ namespace Persistence.Client.Clients
Task<IApiResponse<PaginationContainer<TechMessageDto>>> GetPage([Query] RequestDto request, CancellationToken token);
[Post($"{BaseRoute}")]
Task<IApiResponse<int>> InsertRange([Body] IEnumerable<TechMessageDto> dtos, CancellationToken token);
Task<IApiResponse<int>> AddRange([Body] IEnumerable<TechMessageDto> dtos, CancellationToken token);
[Get($"{BaseRoute}/systems")]
Task<IApiResponse<IEnumerable<string>>> GetSystems(CancellationToken token);

View File

@ -8,7 +8,7 @@ public interface ITimeSeriesClient<TDto>
private const string BaseRoute = "/api/dataSaub";
[Post($"{BaseRoute}")]
Task<IApiResponse<int>> InsertRange(IEnumerable<TDto> dtos);
Task<IApiResponse<int>> AddRange(IEnumerable<TDto> dtos);
[Get($"{BaseRoute}")]
Task<IApiResponse<IEnumerable<TDto>>> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd);

View File

@ -20,7 +20,7 @@ public interface ITimestampedSetClient
/// <param name="sets"></param>
/// <returns></returns>
[Post(baseUrl)]
Task<IApiResponse<int>> InsertRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets);
Task<IApiResponse<int>> AddRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets);
/// <summary>
/// Получение данных с фильтрацией. Значение фильтра null - отключен

View File

@ -1,4 +1,5 @@
using System.Net;
using Microsoft.AspNetCore.Mvc.TagHelpers.Cache;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Client;
using Persistence.Client.Clients;
@ -47,7 +48,7 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetCurrent_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
var setpointKey = await Add();
//act
var response = await setpointClient.GetCurrent([setpointKey]);
@ -83,7 +84,7 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetHistory_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
var setpointKey = await Add();
var historyMoment = DateTimeOffset.UtcNow;
historyMoment = historyMoment.AddDays(1);
@ -120,7 +121,7 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetLog_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
var setpointKey = await Add();
//act
var response = await setpointClient.GetLog([setpointKey]);
@ -144,8 +145,8 @@ namespace Persistence.IntegrationTests.Controllers
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal(DateTimeOffset.MinValue, response.Content?.From);
Assert.Equal(DateTimeOffset.MaxValue, response.Content?.To);
Assert.Equal(DateTimeOffset.MinValue, response.Content!.From);
Assert.Equal(DateTimeOffset.MaxValue, response.Content!.To);
}
[Fact]
@ -153,7 +154,12 @@ namespace Persistence.IntegrationTests.Controllers
{
//arrange
dbContext.CleanupDbSet<Setpoint>();
await Save();
await Add();
var dateBegin = DateTimeOffset.MinValue;
var take = 1;
var part = await setpointClient.GetPart(dateBegin, take, new CancellationToken());
//act
var response = await setpointClient.GetDatesRangeAsync(new CancellationToken());
@ -161,8 +167,17 @@ namespace Persistence.IntegrationTests.Controllers
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotNull(response.Content?.From);
Assert.NotNull(response.Content?.To);
var expectedValue = part.Content!
.FirstOrDefault()!.Created
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualValueFrom = response.Content.From
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedValue, actualValueFrom);
var actualValueTo = response.Content.To
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedValue, actualValueTo);
}
[Fact]
@ -187,7 +202,7 @@ namespace Persistence.IntegrationTests.Controllers
//arrange
var dateBegin = DateTimeOffset.UtcNow;
var take = 1;
await Save();
await Add();
//act
var response = await setpointClient.GetPart(dateBegin, take, new CancellationToken());
@ -201,10 +216,10 @@ namespace Persistence.IntegrationTests.Controllers
[Fact]
public async Task Save_returns_success()
{
await Save();
await Add();
}
private async Task<Guid> Save()
private async Task<Guid> Add()
{
//arrange
var setpointKey = Guid.NewGuid();
@ -215,10 +230,10 @@ namespace Persistence.IntegrationTests.Controllers
};
//act
var response = await setpointClient.Save(setpointKey, setpointValue);
var response = await setpointClient.Add(setpointKey, setpointValue);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return setpointKey;
}

View File

@ -97,7 +97,7 @@ namespace Persistence.IntegrationTests.Controllers
};
//act
var response = await techMessagesClient.InsertRange(dtos, new CancellationToken());
var response = await techMessagesClient.AddRange(dtos, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
@ -276,7 +276,7 @@ namespace Persistence.IntegrationTests.Controllers
//act
var response = await techMessagesClient.InsertRange(dtos, new CancellationToken());
var response = await techMessagesClient.AddRange(dtos, new CancellationToken());
//assert
Assert.Equal(HttpStatusCode.Created, response.StatusCode);

View File

@ -30,7 +30,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
var expected = dto.Adapt<TDto>();
//act
var response = await timeSeriesClient.InsertRange(new TDto[] { expected });
var response = await timeSeriesClient.AddRange(new TDto[] { expected });
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

View File

@ -25,7 +25,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
IEnumerable<TimestampedSetDto> testSets = Generate(10, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// act
var response = await client.InsertRange(idDiscriminator, testSets);
var response = await client.AddRange(idDiscriminator, testSets);
// assert
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
@ -39,7 +39,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
// act
var response = await client.Get(idDiscriminator, null, null, 0, int.MaxValue);
@ -58,7 +58,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
string[] props = ["A"];
// act
@ -86,7 +86,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
var dateMin = DateTimeOffset.Now;
var dateMax = DateTimeOffset.Now.AddSeconds(count);
IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
var tail = testSets.OrderBy(t => t.Timestamp).Skip(count / 2).Take(int.MaxValue);
var geDate = tail.First().Timestamp;
var tolerance = TimeSpan.FromSeconds(1);
@ -111,7 +111,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
var expectedCount = count / 2;
// act
@ -133,7 +133,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
var expectedCount = 1;
int count = 10 + expectedCount;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
// act
var response = await client.Get(idDiscriminator, null, null, count - expectedCount, count);
@ -152,7 +152,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
var expectedCount = 8;
// act
@ -174,7 +174,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
var dateMin = DateTimeOffset.Now;
var dateMax = DateTimeOffset.Now.AddSeconds(count-1);
IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
var tolerance = TimeSpan.FromSeconds(1);
// act
@ -195,7 +195,7 @@ public class TimestampedSetControllerTest : BaseIntegrationTest
Guid idDiscriminator = Guid.NewGuid();
int count = 144;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.InsertRange(idDiscriminator, testSets);
var insertResponse = await client.AddRange(idDiscriminator, testSets);
// act
var response = await client.Count(idDiscriminator);

View File

@ -47,7 +47,7 @@ namespace Persistence.Repository.Repositories
{
var query = GetQueryReadOnly();
var entities = await query
.Where(e => e.Created > dateBegin)
.Where(e => e.Created >= dateBegin)
.Take(take)
.ToArrayAsync(token);
var dtos = entities
@ -88,7 +88,7 @@ namespace Persistence.Repository.Repositories
return dtos;
}
public async Task Save(Guid setpointKey, object newValue, Guid idUser, CancellationToken token)
public async Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token)
{
var entity = new Setpoint()
{

View File

@ -88,7 +88,7 @@ namespace Persistence.Repository.Repositories
return result;
}
public async Task<int> InsertRange(IEnumerable<TechMessageDto> dtos, CancellationToken token)
public async Task<int> AddRange(IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
{
var entities = new List<TechMessage>();
@ -100,6 +100,7 @@ namespace Persistence.Repository.Repositories
?? await CreateDrillingSystem(dto.System, token);
entity.SystemId = systemId;
entity.UserId = userId;
entities.Add(entity);
}
@ -114,7 +115,7 @@ namespace Persistence.Repository.Repositories
{
var query = GetQueryReadOnly();
var entities = await query
.Where(e => e.Timestamp > dateBegin)
.Where(e => e.Timestamp >= dateBegin)
.Take(take)
.ToArrayAsync(token);
var dtos = entities

View File

@ -48,9 +48,9 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
return items;
}
public override async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
public override async Task<int> AddRange(IEnumerable<TDto> dtos, CancellationToken token)
{
var result = await base.InsertRange(dtos, token);
var result = await base.AddRange(dtos, token);
if (result > 0)
{

View File

@ -41,7 +41,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return dtos;
}
public virtual async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
public virtual async Task<int> AddRange(IEnumerable<TDto> dtos, CancellationToken token)
{
var entities = dtos.Select(d => d.Adapt<TEntity>());

View File

@ -20,7 +20,7 @@ public class TimestampedSetRepository : ITimestampedSetRepository
this.db = db;
}
public Task<int> InsertRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets, CancellationToken token)
public Task<int> AddRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets, CancellationToken token)
{
var entities = sets.Select(set => new TimestampedSet(idDiscriminator, set.Timestamp.ToUniversalTime(), set.Set));
var dbSet = db.Set<TimestampedSet>();

View File

@ -40,5 +40,5 @@ public interface ISetpointApi : ISyncApi<SetpointLogDto>
/// <param name="newValue">значение</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IActionResult> Save(Guid setpointKey, object newValue, Guid userId, CancellationToken token);
Task<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token);
}

View File

@ -28,7 +28,7 @@ public interface ITimeSeriesDataApi<TDto> : ITimeSeriesBaseDataApi<TDto>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IActionResult> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
Task<IActionResult> AddRange(IEnumerable<TDto> dtos, CancellationToken token);
}

View File

@ -74,7 +74,7 @@ namespace Persistence.Repositories;
// throw new NotImplementedException();
// }
// public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
// public async Task<int> AddRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
// {
// using var transaction = dbContext.Database.BeginTransaction();
// try

View File

@ -59,5 +59,5 @@ public interface ISetpointRepository
/// <returns></returns>
/// to do
/// id User учесть в соответствующем методе репозитория
Task Save(Guid setpointKey, object newValue, Guid idUser, CancellationToken token);
Task Add(Guid setpointKey, object newValue, Guid idUser, CancellationToken token);
}

View File

@ -22,7 +22,7 @@ namespace Persistence.Repositories
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(IEnumerable<TechMessageDto> dtos, CancellationToken token);
Task<int> AddRange(IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token);
/// <summary>
/// Получение списка уникальных названий систем АБ

View File

@ -15,5 +15,5 @@ public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>, ITimeS
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
Task<int> AddRange(IEnumerable<TDto> dtos, CancellationToken token);
}

View File

@ -55,5 +55,5 @@ public interface ITimestampedSetRepository
/// <param name="sets"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets, CancellationToken token);
Task<int> AddRange(Guid idDiscriminator, IEnumerable<TimestampedSetDto> sets, CancellationToken token);
}