2024-11-22 17:52:15 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-12-16 15:38:46 +05:00
|
|
|
|
using DD.Persistence.Models;
|
|
|
|
|
using DD.Persistence.Repositories;
|
2024-11-22 17:52:15 +05:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
2024-12-16 15:38:46 +05:00
|
|
|
|
namespace DD.Persistence.API.Controllers;
|
2024-11-22 17:52:15 +05:00
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранение наборов данных с отметкой времени.
|
|
|
|
|
/// Не оптимизировано под большие данные.
|
|
|
|
|
/// </summary>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/[controller]/{idDiscriminator}")]
|
2024-12-09 13:19:55 +05:00
|
|
|
|
public class TimestampedSetController : ControllerBase
|
2024-11-22 17:52:15 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly ITimestampedSetRepository repository;
|
|
|
|
|
|
|
|
|
|
public TimestampedSetController(ITimestampedSetRepository repository)
|
|
|
|
|
{
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Записать новые данные
|
|
|
|
|
/// Предполагается что данные с одним дискриминатором имеют одинаковую структуру
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idDiscriminator">Дискриминатор (идентификатор) набора</param>
|
|
|
|
|
/// <param name="sets"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>кол-во затронутых записей</returns>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
|
2024-12-09 13:19:55 +05:00
|
|
|
|
public async Task<IActionResult> AddRange([FromRoute] Guid idDiscriminator, [FromBody] IEnumerable<TimestampedSetDto> sets, CancellationToken token)
|
2024-11-22 17:52:15 +05:00
|
|
|
|
{
|
2024-12-04 14:18:57 +05:00
|
|
|
|
var result = await repository.AddRange(idDiscriminator, sets, token);
|
2024-11-22 17:52:15 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение данных с фильтрацией. Значение фильтра null - отключен
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idDiscriminator">Дискриминатор (идентификатор) набора</param>
|
|
|
|
|
/// <param name="geTimestamp">Фильтр позднее даты</param>
|
|
|
|
|
/// <param name="columnNames">Фильтр свойств набора. Можно запросить только некоторые свойства из набора</param>
|
|
|
|
|
/// <param name="skip"></param>
|
|
|
|
|
/// <param name="take"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>Фильтрованный набор данных с сортировкой по отметке времени</returns>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<TimestampedSetDto>), (int)HttpStatusCode.OK)]
|
2024-12-09 13:19:55 +05:00
|
|
|
|
public async Task<IActionResult> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, [FromQuery] IEnumerable<string>? columnNames, int skip, int take, CancellationToken token)
|
2024-11-22 17:52:15 +05:00
|
|
|
|
{
|
2024-11-26 11:24:31 +05:00
|
|
|
|
var result = await repository.Get(idDiscriminator, geTimestamp, columnNames, skip, take, token);
|
2024-11-22 17:52:15 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить последние данные
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idDiscriminator">Дискриминатор (идентификатор) набора</param>
|
|
|
|
|
/// <param name="columnNames">Фильтр свойств набора. Можно запросить только некоторые свойства из набора</param>
|
|
|
|
|
/// <param name="take"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>Фильтрованный набор данных с сортировкой по отметке времени</returns>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[HttpGet("last")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<TimestampedSetDto>), (int)HttpStatusCode.OK)]
|
2024-12-09 13:19:55 +05:00
|
|
|
|
public async Task<IActionResult> GetLast(Guid idDiscriminator, [FromQuery] IEnumerable<string>? columnNames, int take, CancellationToken token)
|
2024-11-22 17:52:15 +05:00
|
|
|
|
{
|
2024-11-26 11:24:31 +05:00
|
|
|
|
var result = await repository.GetLast(idDiscriminator, columnNames, take, token);
|
2024-11-22 17:52:15 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Диапазон дат за которые есть данные
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idDiscriminator"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>Дата первой и последней записи</returns>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[HttpGet("datesRange")]
|
|
|
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)HttpStatusCode.OK)]
|
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
|
|
|
|
public async Task<IActionResult> GetDatesRange(Guid idDiscriminator, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await repository.GetDatesRange(idDiscriminator, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 11:24:31 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Количество записей по указанному набору в БД. Для пагинации.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idDiscriminator">Дискриминатор (идентификатор) набора</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[HttpGet("count")]
|
2024-11-26 11:24:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
|
2024-11-22 17:52:15 +05:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
|
|
|
|
public async Task<IActionResult> Count(Guid idDiscriminator, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await repository.Count(idDiscriminator, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
}
|