From 9446d32fca2c460a7b52405643dd4780be0fc182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Tue, 14 Nov 2023 16:43:39 +0500 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20+=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B3=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Фикс бага с маппингом 2. Мелкие правки в репозитории, контроллере, сервисе 3. Добавлены проверки в методы сервиса 4. Добавлены новые конфигурации для маппинга --- AsbCloudInfrastructure/DependencyInjection.cs | 18 +++++++++ .../Repository/DailyReportRepository.cs | 40 ++++++------------- .../DailyReport/DailyReportService.cs | 27 +++++++++---- .../Services/DailyReportServiceTest.cs | 33 ++++++++++++++- .../Controllers/DailyReportController.cs | 7 ---- 5 files changed, 81 insertions(+), 44 deletions(-) diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index cd6416d7..7bcd9a01 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -37,7 +37,9 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; +using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance; using AsbCloudApp.Services.DailyReport; +using AsbCloudDb.Model.DailyReports.Blocks.TimeBalance; namespace AsbCloudInfrastructure { @@ -143,6 +145,22 @@ namespace AsbCloudInfrastructure .Map(dest => dest.TopDriveSpeedLimitMax, src => src.TopDriveSpeed.LimitMax) .Map(dest => dest.TopDriveTorquePlan, src => src.TopDriveTorque.Plan) .Map(dest => dest.TopDriveTorqueLimitMax, src => src.TopDriveTorque.LimitMax); + + + TypeAdapterConfig.GlobalSettings.Default.Config + .ForType() + .Map(dest => dest.DurationHours, src => new PlanFactDto() + { + Plan = src.DurationHoursPlan, + Fact = src.DurationHoursFact + }); + + TypeAdapterConfig.GlobalSettings.Default.Config + .ForType() + .Map(dest => dest.WellDepth, src => new PlanFactDto() + { + Plan = src.WellDepthPlan + }); } public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) diff --git a/AsbCloudInfrastructure/Repository/DailyReportRepository.cs b/AsbCloudInfrastructure/Repository/DailyReportRepository.cs index add98668..efd2f79e 100644 --- a/AsbCloudInfrastructure/Repository/DailyReportRepository.cs +++ b/AsbCloudInfrastructure/Repository/DailyReportRepository.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Data; using AsbCloudApp.Data.DailyReport; +using AsbCloudApp.Data.DailyReport.Blocks.Sign; +using AsbCloudApp.Data.DailyReport.Blocks.Subsystems; using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; @@ -62,42 +63,25 @@ public class DailyReportRepository : CrudRepositoryBase GetOrDefaultAsync(int idWell, DateTime date, CancellationToken cancellationToken) { var entity = await GetQuery() - .Include(d => d.Well) .AsNoTracking() - .SingleOrDefaultAsync(d => d.IdWell == idWell && - d.Date == date, cancellationToken); + .SingleOrDefaultAsync(d => d.IdWell == idWell && d.Date == date, cancellationToken); return entity is null ? null : Convert(entity); } protected override DailyReportDto Convert(DailyReport src) { - var dto = base.Convert(src); - - if (dto.TimeBalanceBlock is null) - return dto; - - dto.TimeBalanceBlock.WellDepth = new PlanFactDto() + var dto = new DailyReportDto { - Plan = src.TimeBalanceBlock?.WellDepthPlan + Id = src.Id, + IdWell = src.IdWell, + DateLastUpdate = src.DateLastUpdate, + Date = src.Date, + SignBlock = src.SignBlock?.Adapt(), + TimeBalanceBlock = src.TimeBalanceBlock?.Adapt(), + SubsystemBlock = src.SubsystemBlock?.Adapt() }; - - if (src.TimeBalanceBlock?.WellOperations?.Any() == true) - { - dto.TimeBalanceBlock.WellOperations = src.TimeBalanceBlock.WellOperations.Select(w => - { - var wellOperation = w.Adapt(); - - wellOperation.DurationHours = new PlanFactDto() - { - Plan = w.DurationHoursPlan, - Fact = w.DurationHoursFact - }; - - return wellOperation; - }); - } - + return dto; } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 97cc4e85..a55b8e91 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -56,6 +56,9 @@ public class DailyReportService : IDailyReportService CancellationToken cancellationToken) where TBlock : ItemInfoDto { + if (!await IsDateDailyReportInRangeAsync(idWell, dateDailyReport, cancellationToken)) + throw new ArgumentInvalidException(nameof(dateDailyReport), "Невозможно обновить суточный отчёт"); + var dailyReport = await dailyReportRepository.GetOrDefaultAsync(idWell, dateDailyReport, cancellationToken) ?? new DailyReportDto { @@ -94,7 +97,11 @@ public class DailyReportService : IDailyReportService var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken); if (well is null) - throw new ArgumentInvalidException($"Скважина с Id: {idWell} не найдена", nameof(idWell)); + throw new ArgumentNullException(nameof(idWell), $"Скважина с Id: {idWell} не найдена"); + + if (!await IsDateDailyReportInRangeAsync(idWell, dateDailyReport, cancellationToken)) + throw new ArgumentInvalidException(nameof(dateDailyReport), "Невозможно получить суточный отчёт"); + var dailyReport = await dailyReportRepository.GetOrDefaultAsync(idWell, dateDailyReport, cancellationToken) ?? new DailyReportDto { @@ -137,9 +144,6 @@ public class DailyReportService : IDailyReportService Items = Enumerable.Empty() }; - var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken) - ?? throw new ArgumentInvalidException(nameof(idWell), "Скважина не найдена"); - var datesRange = await GetDatesRangeAsync(idWell, cancellationToken); if (datesRange is null) @@ -208,7 +212,7 @@ public class DailyReportService : IDailyReportService dailyReports.Add(new DailyReportDto { Date = date, - IdWell = well.Id + IdWell = idWell }); } } @@ -220,12 +224,12 @@ public class DailyReportService : IDailyReportService if (!factOperations.Any()) return null; - var minDateStart = factOperations.Min(o => o.DateStart); - var maxDateStart = factOperations.Max(o => o.DateStart); + var minDateStart = factOperations.Min(o => o.DateStart).Date; + var maxDateStart = factOperations.Max(o => o.DateStart).Date; return new DatesRangeDto { - From = minDateStart.Date.AddDays(1) <= DateTime.UtcNow ? minDateStart : DateTime.UtcNow.Date.AddDays(-1), + From = minDateStart.AddDays(1) <= DateTime.UtcNow ? minDateStart : DateTime.UtcNow.Date.AddDays(-1), To = maxDateStart.AddDays(1) <= DateTime.UtcNow ? maxDateStart : DateTime.UtcNow.Date.AddDays(-1) }; } @@ -374,4 +378,11 @@ public class DailyReportService : IDailyReportService GeDate = dateDailyReport, LtDate = dateDailyReport?.AddHours(24) }, cancellationToken); + + private async Task IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken) + { + var datesRange = await GetDatesRangeAsync(idWell, cancellationToken); + + return dateDailyReport >= datesRange?.From && dateDailyReport <= datesRange.To; + } } \ No newline at end of file diff --git a/AsbCloudWebApi.Tests/Services/DailyReportServiceTest.cs b/AsbCloudWebApi.Tests/Services/DailyReportServiceTest.cs index a8447fa2..9b090d5b 100644 --- a/AsbCloudWebApi.Tests/Services/DailyReportServiceTest.cs +++ b/AsbCloudWebApi.Tests/Services/DailyReportServiceTest.cs @@ -11,12 +11,12 @@ using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance; using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Data.ProcessMaps.Report; using AsbCloudApp.Data.Subsystems; +using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudApp.Services.ProcessMaps.WellDrilling; using AsbCloudApp.Services.Subsystems; -using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.DailyReport; using NSubstitute; using Xunit; @@ -281,6 +281,23 @@ public class DailyReportServiceTest Assert.Equal(idDailyReport, result); } + [Theory] + [InlineData("2090.01.01")] + [InlineData("2000.01.01")] + public async Task UpdateOrInsertAsync_ShouldReturn_UnableToUpdateDailyReport(DateTime dateDaileReport) + { + //act + var result = await Assert.ThrowsAsync(() => dailyReportService.UpdateOrInsertAsync( + idWell, + dateDaileReport, + idUser, + fakeSignBlock, + CancellationToken.None)); + + //assert + Assert.Contains("Невозможно обновить суточный отчёт", result.Message); + } + [Fact] public async Task UpdateOrInsertAsync_ShouldReturn_UpdatedSignBlock() { @@ -310,6 +327,20 @@ public class DailyReportServiceTest Assert.Equal(idDailyReport, result); } + [Theory] + [InlineData("2090.01.01")] + [InlineData("2000.01.01")] + public async Task GetAsync_ShouldReturn_UnableToGetDailyReport(DateTime dateDaileReport) + { + //act + var result = await Assert.ThrowsAsync(() => dailyReportService.GetAsync(idWell, + dateDaileReport, + CancellationToken.None)); + + //assert + Assert.Contains("Невозможно получить суточный отчёт", result.Message); + } + [Fact] public async Task GetAsync_ShouldReturn_AddedWellInfo() { diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index 0a73a881..4ff80ad7 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -173,13 +173,6 @@ public class DailyReportController : ControllerBase [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task ExportAsync(int idWell, DateOnly dateDailyReport, CancellationToken cancellationToken) { - var dateStartToDateTime = dateDailyReport.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc); - - var datesRange = await dailyReportService.GetDatesRangeAsync(idWell, cancellationToken); - - if (dateStartToDateTime < datesRange?.From || dateStartToDateTime > datesRange?.To) - throw new ArgumentInvalidException("Невозможно получить суточный отчёт", nameof(dateDailyReport)); - await AssertUserAccessToWell(idWell, cancellationToken); var dailyReport = await dailyReportExportService.ExportAsync(idWell,