From e4f3adb101fe8c7dbbaaff6d822d86cdce374b9f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 1 Apr 2024 16:17:52 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B3=D0=B0=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D0=BE=20=D0=B1=D1=83=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D1=89=D0=B8=D0=BA=D0=B0=D0=BC=20=D0=B8=20=D1=81=D0=BA?= =?UTF-8?q?=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/SubsystemService.cs | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs index ed2571a9..d2400ba3 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs @@ -67,22 +67,16 @@ internal class SubsystemService : ISubsystemService var detectedOperations = await detectedOperationService .GetOperationsAsync(byWellRequest, token); + var detectedOperationsByCurrentDriller = detectedOperations.Where(d => d.Driller?.Id == schedule.IdDriller); - var groupByDriller = detectedOperations - .Where(operation => operation.Driller is not null) - .GroupBy(operation => operation.Driller); - - foreach (var entry in groupByDriller) + var drillerOperationsStat = await CalcStatAsync(detectedOperationsByCurrentDriller, token); + var dto = new DrillerDetectedOperationStatDto { - var drillerOperationsStat = await CalcStatAsync(entry, token); - var dto = new DrillerDetectedOperationStatDto - { - Statistic = drillerOperationsStat, - Schedule = schedule, - Well = well, - }; - result.Add(dto); - } + Statistic = drillerOperationsStat, + Schedule = schedule, + Well = well, + }; + result.Add(dto); } return result; From ddbd84e933587954bc145cbf415391113c0d4cb4 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 2 Apr 2024 14:16:40 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A4=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BD=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=BF=D0=BE=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8?= =?UTF-8?q?=D0=B2=D1=83=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=B9=20=D0=B1?= =?UTF-8?q?=D1=83=D1=80=D0=B8=D0=BB=D1=8C=D1=89=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20=D0=B1=D1=83=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D1=89=D0=B8=D0=BA=D0=BE=D0=B2=20=D0=B2=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=B9=20=D1=81=D0=BA?= =?UTF-8?q?=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Requests/GetStatRequest.cs | 4 +- .../Repository/ScheduleRepository.cs | 6 +-- .../Controllers/DrillerController.cs | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/AsbCloudApp/Requests/GetStatRequest.cs b/AsbCloudApp/Requests/GetStatRequest.cs index 5ebb9550..fbc53dd0 100644 --- a/AsbCloudApp/Requests/GetStatRequest.cs +++ b/AsbCloudApp/Requests/GetStatRequest.cs @@ -14,7 +14,7 @@ public class GetStatRequest: RequestBase public IEnumerable IdsWells { get; set; } = new List(); /// - /// id + /// /// - public int? IdDriller { get; set; } + public IEnumerable IdsDrillers { get; set; } = new List(); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/ScheduleRepository.cs b/AsbCloudInfrastructure/Repository/ScheduleRepository.cs index e7c2aa1f..29bf4f5b 100644 --- a/AsbCloudInfrastructure/Repository/ScheduleRepository.cs +++ b/AsbCloudInfrastructure/Repository/ScheduleRepository.cs @@ -57,11 +57,11 @@ namespace AsbCloudInfrastructure.Repository public async Task> GetPageAsync(GetStatRequest request, CancellationToken token) { var idWell = request.IdsWells; - var idDriller = request.IdDriller; + var idsDrillers = request.IdsDrillers; var query = GetQuery().Where(s => request.IdsWells.Contains(s.IdWell)); - if (idDriller is not null) + if (idsDrillers.Any()) { - query.Where(s => s.IdDriller == idDriller); + query = query.Where(s => idsDrillers.Contains(s.IdDriller)); } var result = await query.ToArrayAsync(token); diff --git a/AsbCloudWebApi/Controllers/DrillerController.cs b/AsbCloudWebApi/Controllers/DrillerController.cs index 0fa84e4a..48efca2c 100644 --- a/AsbCloudWebApi/Controllers/DrillerController.cs +++ b/AsbCloudWebApi/Controllers/DrillerController.cs @@ -1,7 +1,13 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { @@ -13,8 +19,38 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class DrillerController : CrudController> { - public DrillerController(ICrudRepository service) + private IScheduleRepository scheduleRepository; + + public DrillerController(ICrudRepository service, IScheduleRepository scheduleRepository) : base(service) - { } + { + this.scheduleRepository = scheduleRepository; + } + + /// + /// Получить список бурильщиков по ключам скважин + /// + /// массив ключей скважин + /// token + /// все записи + [HttpGet("/api/drillers")] + [Permission] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAsync([FromQuery] IEnumerable idsWells, CancellationToken token) + { + var request = new GetStatRequest() + { + IdsWells = idsWells, + }; + var schedulePage = await scheduleRepository.GetPageAsync(request, token); + var drillers = schedulePage + .Select(s => s.Driller) + .Where(d => d is not null) + .GroupBy(d => d.Id) + .Select(group => group.First()) + .OrderBy(d => d.Surname); + + return Ok(drillers); + } } } From 377cb762ed83addbb97f50ac1fdaff7c92592c0a Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 2 Apr 2024 14:25:50 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9A=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Requests/GetStatRequest.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/AsbCloudApp/Requests/GetStatRequest.cs b/AsbCloudApp/Requests/GetStatRequest.cs index fbc53dd0..6a942f48 100644 --- a/AsbCloudApp/Requests/GetStatRequest.cs +++ b/AsbCloudApp/Requests/GetStatRequest.cs @@ -1,20 +1,19 @@ using System.Collections.Generic; -using System.Linq; namespace AsbCloudApp.Requests; /// -/// +/// Запрос на получение статистики использования подсистем бурильщиком /// -public class GetStatRequest: RequestBase +public class GetStatRequest : RequestBase { /// - /// id + /// id скважин /// public IEnumerable IdsWells { get; set; } = new List(); /// - /// + /// список ключей бурильщиков /// public IEnumerable IdsDrillers { get; set; } = new List(); } \ No newline at end of file From 11285f236d9db84266c1dcd0c0ba0a36fdbe5b8f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 2 Apr 2024 17:28:38 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B=D0=B9=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B1=D1=83=D1=80=D0=B8=D0=BB=D1=8C=D1=89=D0=B8=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=BF=D0=BE=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=D0=BC=20?= =?UTF-8?q?=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Clients/IDrillerClient.cs | 11 +++ .../Controllers/DrillControllerTest.cs | 88 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs create mode 100644 AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs new file mode 100644 index 00000000..d8b9e26b --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs @@ -0,0 +1,11 @@ +using AsbCloudApp.Data; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface IDrillerClient +{ + + [Get("/api/drillers")] + Task>> GetAsync([Query(CollectionFormat.Multi)] IEnumerable idsWells, CancellationToken token); +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs new file mode 100644 index 00000000..638630ca --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs @@ -0,0 +1,88 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Requests; +using AsbCloudDb.Model; +using AsbCloudWebApi.IntegrationTests.Clients; +using AsbCloudWebApi.IntegrationTests.Data; +using System; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers; + +public class DrillerControllerTest : BaseIntegrationTest +{ + private readonly IDrillerClient client; + + public DrillerControllerTest(WebAppFactoryFixture factory) + : base(factory) + { + client = factory.GetAuthorizedHttpClient(string.Empty); + + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + } + + [Fact] + public async Task GetByWellIds_returns_success() + { + //arrange + var well1 = CreateWellAsync(2); + var well2 = CreateWellAsync(3); + var well3 = CreateWellAsync(4); + dbContext.Wells.Add(well1); + dbContext.Wells.Add(well2); + dbContext.Wells.Add(well3); + + var driller1 = CreateDrillerAsync(1); + var driller2 = CreateDrillerAsync(2); + var driller3 = CreateDrillerAsync(3); + + var schedule1= CreateScheduleAsync(well1.Id, driller1); + var schedule2 = CreateScheduleAsync(well2.Id, driller2); + var schedule3 = CreateScheduleAsync(well3.Id, driller3); + + dbContext.Schedule.Add(schedule1); + dbContext.Schedule.Add(schedule2); + dbContext.Schedule.Add(schedule3); + + await dbContext.SaveChangesAsync(); + + ////act + var idsWells = dbContext.Wells.ToArray().Select(w => w.Id); + var response = await client.GetAsync(idsWells, CancellationToken.None); + + ////assert + Assert.NotNull(response.Content); + Assert.Equal(3, response.Content.Count()); + } + + private static Schedule CreateScheduleAsync(int idWell, Driller driller) => new() + { + IdWell = idWell, + ShiftStart = new TimeOnly(8, 0, 0), + ShiftEnd = new TimeOnly(20, 0, 0), + DrillStart = new DateTimeOffset(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc)), + DrillEnd = new DateTimeOffset(new DateTime(2024, 2, 1, 0, 0, 0, DateTimeKind.Utc)), + Driller = driller + }; + + + private static Well CreateWellAsync(int idWell) => new() + { + Id = idWell, + IdWellType = 1, + IdState = 1, + Caption = $"Скважина {idWell}", + Latitude = 10, + Longitude = 20, + Timezone = Defaults.Timezone, + IdCluster = 1, + }; + + private static Driller CreateDrillerAsync(int idDriller) => new() + { + Id = idDriller, + Name = idDriller.ToString(), + Patronymic = idDriller.ToString(), + Surname= idDriller.ToString() + }; +} \ No newline at end of file