Add WitsControllerAbstract.GetLastDataAsync(..)

This commit is contained in:
ngfrolov 2022-04-15 14:45:49 +05:00
parent 091464a390
commit 7b957d0edb
3 changed files with 36 additions and 2 deletions

View File

@ -11,6 +11,7 @@ namespace AsbCloudApp.Services
{
Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token);
Task<IEnumerable<TDto>> GetAsync(int idTelemetry, DateTime begin, DateTime end, CancellationToken token);
Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token);
Task<(DateTime begin, DateTime end, int count)?> GetStatAsync(int idTelemetry, CancellationToken token);
}
}

View File

@ -54,6 +54,17 @@ namespace AsbCloudInfrastructure.Services
return data.Select(d => Convert(d, timezoneHours));
}
public async Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token)
{
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
var query = dbset
.Where(d => d.IdTelemetry == idTelemetry)
.OrderBy(d => d.DateTime)
.AsNoTracking();
var data = await query.LastOrDefaultAsync(token);
return new TDto[] { Convert(data, timezoneHours) };
}
public Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
{
if (dtos?.Any() != true)
@ -86,5 +97,6 @@ namespace AsbCloudInfrastructure.Services
data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours);
return data;
}
}
}

View File

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -59,6 +60,26 @@ namespace AsbCloudWebApi.Controllers.WITS
return Ok();
}
/// <summary>
/// Получение последней пришедшей WITS записи.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}/last")]
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetLastDataAsync(
int idWell,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetLastAsync((int)idTelemetry, token);
return Ok(dtos);
}
/// <summary>
/// Получение списка архивных WITS записей за период.
/// </summary>
@ -71,8 +92,8 @@ namespace AsbCloudWebApi.Controllers.WITS
[HttpGet("{idWell}")]
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetDataAsync(
int idWell,
DateTime begin,
DateTime end,
[Required] DateTime begin,
[Required] DateTime end,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{