Исправление + фикс багов

1. Фикс бага с маппингом
2. Мелкие правки в репозитории, контроллере, сервисе
3. Добавлены проверки в методы сервиса
4. Добавлены новые конфигурации для маппинга
This commit is contained in:
Степанов Дмитрий 2023-11-14 16:43:39 +05:00
parent 4bd02ab348
commit 9446d32fca
5 changed files with 81 additions and 44 deletions

View File

@ -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<TimeBalanceRecord, TimeBalanceRecordDto>()
.Map(dest => dest.DurationHours, src => new PlanFactDto<double?>()
{
Plan = src.DurationHoursPlan,
Fact = src.DurationHoursFact
});
TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<TimeBalanceBlock, TimeBalanceBlockDto>()
.Map(dest => dest.WellDepth, src => new PlanFactDto<double?>()
{
Plan = src.WellDepthPlan
});
}
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)

View File

@ -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<DailyReportDto, DailyRep
public async Task<DailyReportDto?> 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<double?>()
var dto = new DailyReportDto
{
Plan = src.TimeBalanceBlock?.WellDepthPlan
Id = src.Id,
IdWell = src.IdWell,
DateLastUpdate = src.DateLastUpdate,
Date = src.Date,
SignBlock = src.SignBlock?.Adapt<SignBlockDto>(),
TimeBalanceBlock = src.TimeBalanceBlock?.Adapt<TimeBalanceBlockDto>(),
SubsystemBlock = src.SubsystemBlock?.Adapt<SubsystemBlockDto>()
};
if (src.TimeBalanceBlock?.WellOperations?.Any() == true)
{
dto.TimeBalanceBlock.WellOperations = src.TimeBalanceBlock.WellOperations.Select(w =>
{
var wellOperation = w.Adapt<TimeBalanceRecordDto>();
wellOperation.DurationHours = new PlanFactDto<double?>()
{
Plan = w.DurationHoursPlan,
Fact = w.DurationHoursFact
};
return wellOperation;
});
}
return dto;
}
}

View File

@ -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<DailyReportDto>()
};
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<bool> IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken)
{
var datesRange = await GetDatesRangeAsync(idWell, cancellationToken);
return dateDailyReport >= datesRange?.From && dateDailyReport <= datesRange.To;
}
}

View File

@ -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<ArgumentInvalidException>(() => 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<ArgumentInvalidException>(() => dailyReportService.GetAsync(idWell,
dateDaileReport,
CancellationToken.None));
//assert
Assert.Contains("Невозможно получить суточный отчёт", result.Message);
}
[Fact]
public async Task GetAsync_ShouldReturn_AddedWellInfo()
{

View File

@ -173,13 +173,6 @@ public class DailyReportController : ControllerBase
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> 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,