Рефакторинг сервисов

This commit is contained in:
Степанов Дмитрий 2024-03-14 13:18:02 +03:00
parent e3a7767a41
commit bc4d52200d
12 changed files with 80 additions and 68 deletions

View File

@ -3,17 +3,35 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using ClosedXML.Excel;
using Mapster;
namespace AsbCloudInfrastructure.Services.ExcelServices;
public abstract class ExportExcelService<TDto>
public abstract class ExportExcelService<TDto, TOptions> : IExportService<TOptions>
where TOptions : IExportOptionsRequest
{
protected abstract ITemplateParameters TemplateParameters { get; }
protected Stream Export(IEnumerable<TDto> dtos)
protected abstract Task<string> BuildFileNameAsync(TOptions options, CancellationToken token);
protected abstract Task<IEnumerable<TDto>> GetDtosAsync(TOptions options, CancellationToken token);
public async Task<(string FileName, Stream File)> ExportAsync(TOptions options, CancellationToken token)
{
var dtos = await GetDtosAsync(options, token);
var fileName = await BuildFileNameAsync(options, token);
var file = BuildFile(dtos);
return (fileName, file);
}
private Stream BuildFile(IEnumerable<TDto> dtos)
{
using var template = GetTemplateFile();
using var workbook = new XLWorkbook(template);

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using AsbCloudInfrastructure.Services.ExcelServices.Templates.ProcessMapPlanTemplates;
@ -23,16 +24,17 @@ public class ProcessMapPlanDrillingExportService : ProcessMapPlanExportService<P
protected override ITemplateParameters TemplateParameters => new ProcessMapPlanDrillingTemplate();
protected override async Task<string> BuildFileNameAsync(int idWell, CancellationToken token)
protected override async Task<string> BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token)
{
var caption = await wellService.GetWellCaptionByIdAsync(idWell, token);
var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token);
return $"{caption}_РТК_План_бурение.xlsx";
}
protected override async Task<IEnumerable<ProcessMapPlanDrillingDto>> GetProcessMapPlanAsync(int idWell, CancellationToken token)
protected override async Task<IEnumerable<ProcessMapPlanDrillingDto>> GetDtosAsync(WellRelatedExportRequest options,
CancellationToken token)
{
var dtos = await base.GetProcessMapPlanAsync(idWell, token);
var dtos = await base.GetDtosAsync(options, token);
var dtosWithMode = dtos.Select(dto =>
{
dto.Mode = dto.IdMode switch
@ -41,7 +43,7 @@ public class ProcessMapPlanDrillingExportService : ProcessMapPlanExportService<P
2 => "Слайд",
_ => throw new ArgumentOutOfRangeException()
};
return dto;
});

View File

@ -1,17 +1,16 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices;
namespace AsbCloudInfrastructure.Services.ProcessMapPlan.Export;
public abstract class ProcessMapPlanExportService<TDto> : ExportExcelService<TDto>,
IProcessMapPlanExportService
public abstract class ProcessMapPlanExportService<TDto> : ExportExcelService<TDto, WellRelatedExportRequest>
where TDto : ChangeLogAbstract
{
protected readonly IWellService wellService;
@ -24,21 +23,10 @@ public abstract class ProcessMapPlanExportService<TDto> : ExportExcelService<TDt
this.processMapPlanRepository = processMapPlanRepository;
this.wellService = wellService;
}
public async Task<(string FileName, Stream File)> ExportAsync(int idWell, CancellationToken token)
protected override async Task<IEnumerable<TDto>> GetDtosAsync(WellRelatedExportRequest options, CancellationToken token)
{
var dtos = await GetProcessMapPlanAsync(idWell, token);
var fileName = await BuildFileNameAsync(idWell, token);
var file = Export(dtos);
return (fileName, file);
}
protected abstract Task<string> BuildFileNameAsync(int idWell, CancellationToken token);
protected virtual async Task<IEnumerable<TDto>> GetProcessMapPlanAsync(int idWell, CancellationToken token)
{
var request = new ProcessMapPlanBaseRequestWithWell(idWell);
var request = new ProcessMapPlanBaseRequestWithWell(options.IdWell);
var dtos = await processMapPlanRepository.Get(request, token);
return dtos;
}

View File

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using AsbCloudInfrastructure.Services.ExcelServices.Templates.ProcessMapPlanTemplates;
@ -19,10 +20,10 @@ public class ProcessMapPlanReamExportService : ProcessMapPlanExportService<Proce
: base(processMapPlanRepository, wellService)
{
}
protected override async Task<string> BuildFileNameAsync(int idWell, CancellationToken token)
protected override async Task<string> BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token)
{
var caption = await wellService.GetWellCaptionByIdAsync(idWell, token);
var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token);
return $"{caption}_РТК_План_проработка.xlsx";
}

View File

@ -1,15 +1,15 @@
using AsbCloudApp.Data.Trajectory;
using System.Collections.Generic;
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudInfrastructure.Services.ExcelServices;
namespace AsbCloudInfrastructure.Services.Trajectory.Export;
public abstract class TrajectoryExportService<TDto> : ExportExcelService<TDto>,
ITrajectoryExportService
public abstract class TrajectoryExportService<TDto> : ExportExcelService<TDto, WellRelatedExportRequest>
where TDto : TrajectoryGeoDto
{
protected readonly IWellService wellService;
@ -22,14 +22,6 @@ public abstract class TrajectoryExportService<TDto> : ExportExcelService<TDto>,
this.trajectoryRepository = trajectoryRepository;
}
public async Task<(string FileName, Stream File)> ExportAsync(int idWell, CancellationToken token)
{
var dtos = await trajectoryRepository.GetAsync(idWell, token);
var fileName = await BuildFileNameAsync(idWell, token);
var file = Export(dtos);
return (fileName, file);
}
protected abstract Task<string> BuildFileNameAsync(int idWell, CancellationToken token);
protected override Task<IEnumerable<TDto>> GetDtosAsync(WellRelatedExportRequest options, CancellationToken token) =>
trajectoryRepository.GetAsync(options.IdWell, token);
}

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using AsbCloudInfrastructure.Services.ExcelServices.Templates.TrajectoryTemplates;
@ -17,10 +18,10 @@ public class TrajectoryFactManualExportService : TrajectoryExportService<Traject
}
protected override ITemplateParameters TemplateParameters => new TrajectoryFactManualTemplate();
protected override async Task<string> BuildFileNameAsync(int idWell, CancellationToken token)
protected override async Task<string> BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token)
{
var caption = await wellService.GetWellCaptionByIdAsync(idWell, token);
var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token);
return $"{caption}_Фактическая_траектория.xlsx";
}

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using AsbCloudInfrastructure.Services.ExcelServices.Templates.TrajectoryTemplates;
@ -18,9 +19,9 @@ public class TrajectoryFactNnbExportService : TrajectoryExportService<Trajectory
protected override ITemplateParameters TemplateParameters => new TrajectoryFactNnbTemplate();
protected override async Task<string> BuildFileNameAsync(int idWell, CancellationToken token)
protected override async Task<string> BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token)
{
var caption = await wellService.GetWellCaptionByIdAsync(idWell, token);
var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token);
return $"{caption}_Траектория_ННБ.xlsx";
}

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ExcelServices.Templates;
using AsbCloudInfrastructure.Services.ExcelServices.Templates.TrajectoryTemplates;
@ -10,17 +11,17 @@ namespace AsbCloudInfrastructure.Services.Trajectory.Export;
public class TrajectoryPlanExportService : TrajectoryExportService<TrajectoryGeoPlanDto>
{
public TrajectoryPlanExportService(IWellService wellService,
public TrajectoryPlanExportService(IWellService wellService,
ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryRepository)
: base(wellService, trajectoryRepository)
{
}
protected override ITemplateParameters TemplateParameters => new TrajectoryPlanTemplate();
protected override async Task<string> BuildFileNameAsync(int idWell, CancellationToken token)
protected override async Task<string> BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token)
{
var caption = await wellService.GetWellCaptionByIdAsync(idWell, token);
var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token);
return $"{caption}_Плановая_Траектория.xlsx";
}

View File

@ -6,12 +6,15 @@ using NSubstitute;
using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Requests.ExportOptions;
using Xunit;
namespace AsbCloudWebApi.Tests.Services.Trajectory
{
public class TrajectoryExportTest
{
private const int idWell = 4;
private IWellService wellService;
private readonly ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryPlanRepository;
private readonly TrajectoryPlanExportService trajectoryPlanExportService;
@ -22,8 +25,6 @@ namespace AsbCloudWebApi.Tests.Services.Trajectory
private readonly ITrajectoryNnbRepository trajectoryFactNnbRepository;
private readonly TrajectoryFactNnbExportService trajectoryFactNnbExportService;
private readonly int idWell = 4;
private readonly TrajectoryGeoPlanDto[] trajectoryPlanRows = new TrajectoryGeoPlanDto[2] {
new TrajectoryGeoPlanDto() {
Id = 1,
@ -80,6 +81,8 @@ namespace AsbCloudWebApi.Tests.Services.Trajectory
},
};
private readonly WellRelatedExportRequest exportOptions = new(idWell);
public TrajectoryExportTest()
{
wellService = Substitute.For<IWellService>();
@ -98,10 +101,9 @@ namespace AsbCloudWebApi.Tests.Services.Trajectory
{
trajectoryPlanRepository.GetAsync(idWell, CancellationToken.None)
.Returns(trajectoryPlanRows);
var stream = await trajectoryPlanExportService.ExportAsync(idWell, CancellationToken.None);
var stream = await trajectoryPlanExportService.ExportAsync(exportOptions, CancellationToken.None);
Assert.True(stream.File.Length > 0);
}
[Fact]
@ -109,8 +111,8 @@ namespace AsbCloudWebApi.Tests.Services.Trajectory
{
trajectoryFactManualReposirory.GetAsync(idWell, CancellationToken.None)
.Returns(trajectoryFactRows);
var stream = await trajectoryFactManualExportService.ExportAsync(idWell, CancellationToken.None);
var stream = await trajectoryFactManualExportService.ExportAsync(exportOptions, CancellationToken.None);
Assert.True(stream.File.Length > 0);
}
@ -119,8 +121,8 @@ namespace AsbCloudWebApi.Tests.Services.Trajectory
{
trajectoryFactNnbRepository.GetAsync(idWell, CancellationToken.None)
.Returns(trajectoryFactRows);
var stream = await trajectoryFactNnbExportService.ExportAsync(idWell, CancellationToken.None);
var stream = await trajectoryFactNnbExportService.ExportAsync(exportOptions, CancellationToken.None);
Assert.True(stream.File.Length > 0);
}
}

View File

@ -15,6 +15,7 @@ using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudApp.Data.ProcessMaps;
using System.ComponentModel.DataAnnotations;
using AsbCloudApp.Requests.ExportOptions;
namespace AsbCloudWebApi.Controllers.ProcessMaps;
@ -30,12 +31,12 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
private readonly IWellService wellService;
private readonly IParserService<TDto, WellRelatedParserRequest> parserService;
private readonly IProcessMapPlanExportService processMapPlanExportService;
private readonly IExportService<WellRelatedExportRequest> processMapPlanExportService;
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService,
IParserService<TDto, WellRelatedParserRequest> parserService,
IProcessMapPlanExportService processMapPlanExportService)
IExportService<WellRelatedExportRequest> processMapPlanExportService)
{
this.repository = repository;
this.wellService = wellService;
@ -280,7 +281,8 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> ExportAsync([FromRoute] int idWell, CancellationToken token)
{
var (fileName, file) = await processMapPlanExportService.ExportAsync(idWell, token);
var exportOptions = new WellRelatedExportRequest(idWell);
var (fileName, file) = await processMapPlanExportService.ExportAsync(exportOptions, token);
return File(file, "application/octet-stream", fileName);
}

View File

@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudInfrastructure.Services.Trajectory.Export;
namespace AsbCloudWebApi.Controllers.Trajectory
{
@ -22,11 +24,11 @@ namespace AsbCloudWebApi.Controllers.Trajectory
protected abstract string TemplateFileName { get; }
private readonly IWellService wellService;
private readonly ITrajectoryExportService trajectoryExportService;
private readonly TrajectoryExportService<TDto> trajectoryExportService;
private readonly ITrajectoryRepository<TDto> trajectoryRepository;
protected TrajectoryController(IWellService wellService,
ITrajectoryExportService trajectoryExportService,
TrajectoryExportService<TDto> trajectoryExportService,
ITrajectoryRepository<TDto> trajectoryRepository)
{
this.trajectoryExportService = trajectoryExportService;
@ -48,8 +50,9 @@ namespace AsbCloudWebApi.Controllers.Trajectory
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
return Forbid();
var (fileName, file) = await trajectoryExportService.ExportAsync(idWell, token);
var exportOptions = new WellRelatedExportRequest(idWell);
var (fileName, file) = await trajectoryExportService.ExportAsync(exportOptions, token);
return File(file, "application/octet-stream", fileName);
}

View File

@ -10,6 +10,7 @@ using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudInfrastructure.Services.Trajectory.Export;
namespace AsbCloudWebApi.Controllers.Trajectory
{
@ -27,7 +28,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory
protected TrajectoryEditableController(IWellService wellService,
IParserService<TDto, WellRelatedParserRequest> parserService,
ITrajectoryExportService trajectoryExportService,
TrajectoryExportService<TDto> trajectoryExportService,
ITrajectoryEditableRepository<TDto> trajectoryRepository)
: base(wellService, trajectoryExportService, trajectoryRepository)
{