From 80d74067c049f1ca96d390e09f8d63548fb8237c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Sat, 28 Aug 2021 22:34:57 +0500 Subject: [PATCH] MeasureService Add GetLastAsync --- AsbCloudApp/Services/IMeasureService.cs | 1 + .../Services/MeasureService.cs | 23 ++++++++++++++++--- .../Controllers/MeasureController.cs | 17 +++++++++++--- .../Controllers/WellOperationController.cs | 16 ++++++------- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/AsbCloudApp/Services/IMeasureService.cs b/AsbCloudApp/Services/IMeasureService.cs index 756fbc9b..b980a75f 100644 --- a/AsbCloudApp/Services/IMeasureService.cs +++ b/AsbCloudApp/Services/IMeasureService.cs @@ -8,6 +8,7 @@ namespace AsbCloudApp.Services public interface IMeasureService { Task> GetCategoriesAsync(CancellationToken token); + Task GetLastAsync(int idWell, int idCategory, CancellationToken token); Task> GetHisoryAsync(int idWell, int idCategory, CancellationToken token); Task> GetAllLastAsync(int idWell, CancellationToken token); Task InsertAsync(int idWell, MeasureDto data, CancellationToken token); diff --git a/AsbCloudInfrastructure/Services/MeasureService.cs b/AsbCloudInfrastructure/Services/MeasureService.cs index 7cc58fde..8d86d07a 100644 --- a/AsbCloudInfrastructure/Services/MeasureService.cs +++ b/AsbCloudInfrastructure/Services/MeasureService.cs @@ -30,13 +30,30 @@ namespace AsbCloudInfrastructure.Services return dto; } + public async Task GetLastAsync(int idWell, int idCategory, CancellationToken token) + { + var query = db.Measures + .Include(m => m.Category) + .Where(m => m.IdWell == idWell && m.IdCategory == idCategory && !m.IsDeleted) + .OrderByDescending(m => m.Timestamp) + .Take(1); + + var entity = await query + .AsNoTracking() + .FirstOrDefaultAsync(token) + .ConfigureAwait(false); + + var dtos = entity?.Adapt((d, s) => d.CategoryName = s.Category?.Name); + return dtos; + } + public async Task> GetAllLastAsync(int idWell, CancellationToken token) { var categories = await cacheCategories.WhereAsync(token).ConfigureAwait(false); if (!categories.Any()) return default; - var queries = categories.Select(c => db.Measures + var queries = categories.Select(c => db.Measures.Include(m => m.Category) .Where(m => m.IdWell == idWell && m.IdCategory == c.Id && !m.IsDeleted) .OrderByDescending(m => m.Timestamp) .Take(1) @@ -62,7 +79,7 @@ namespace AsbCloudInfrastructure.Services .ToListAsync(token) .ConfigureAwait(false); - var dtos = entities.Adapt((d, s) => d.CategoryName = s.Category.Name); + var dtos = entities.Adapt((d, s) => d.CategoryName = s.Category?.Name); return dtos; } @@ -76,7 +93,7 @@ namespace AsbCloudInfrastructure.Services .ToListAsync(token) .ConfigureAwait(false); - var dtos = entities.Adapt((d, s) => d.CategoryName = s.Category.Name); + var dtos = entities.Adapt((d, s) => d.CategoryName = s.Category?.Name); return dtos; } diff --git a/AsbCloudWebApi/Controllers/MeasureController.cs b/AsbCloudWebApi/Controllers/MeasureController.cs index af166536..9efcb6ad 100644 --- a/AsbCloudWebApi/Controllers/MeasureController.cs +++ b/AsbCloudWebApi/Controllers/MeasureController.cs @@ -33,7 +33,7 @@ namespace AsbCloudWebApi.Controllers } [HttpGet] - [Route("last")] + [Route("lastAll")] public async Task GetAllLastAsync([FromRoute] int idWell, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) @@ -44,8 +44,19 @@ namespace AsbCloudWebApi.Controllers } [HttpGet] - [Route("{idCategory}/history")] - public async Task GetHisoryAsync([FromRoute] int idWell, [FromQuery] int idCategory, CancellationToken token = default) + [Route("last/{idCategory}")] + public async Task GetLastAsync([FromRoute] int idWell, [FromRoute] int idCategory, CancellationToken token = default) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + + var result = await lastDataService.GetLastAsync(idWell, idCategory, token).ConfigureAwait(false); + return Ok(result); + } + + [HttpGet] + [Route("history/{idCategory}")] + public async Task GetHisoryAsync([FromRoute] int idWell, [FromRoute] int idCategory, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 9a0a7317..e6684ba1 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -57,16 +57,16 @@ namespace AsbCloudWebApi.Controllers [HttpGet] [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] public async Task GetOperationsAsync( - int idWell, - int? opertaionType = default, + [FromRoute] int idWell, + [FromQuery] int? opertaionType = default, [FromQuery] IEnumerable sectionTypeIds = default, [FromQuery] IEnumerable operationCategoryIds = default, - DateTime begin = default, - DateTime end = default, - double minDepth = double.MinValue, - double maxDepth = double.MaxValue, - int skip = 0, - int take = 32, + [FromQuery] DateTime begin = default, + [FromQuery] DateTime end = default, + [FromQuery] double minDepth = double.MinValue, + [FromQuery] double maxDepth = double.MaxValue, + [FromQuery] int skip = 0, + [FromQuery] int take = 32, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))