diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs b/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs index 9e189411..086eba97 100644 --- a/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs +++ b/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs @@ -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 +public abstract class ExportExcelService : IExportService + where TOptions : IExportOptionsRequest { protected abstract ITemplateParameters TemplateParameters { get; } - - protected Stream Export(IEnumerable dtos) + + protected abstract Task BuildFileNameAsync(TOptions options, CancellationToken token); + + protected abstract Task> 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 dtos) { using var template = GetTemplateFile(); using var workbook = new XLWorkbook(template); diff --git a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanDrillingExportService.cs b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanDrillingExportService.cs index 18778ca1..5b0935d2 100644 --- a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanDrillingExportService.cs +++ b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanDrillingExportService.cs @@ -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

new ProcessMapPlanDrillingTemplate(); - protected override async Task BuildFileNameAsync(int idWell, CancellationToken token) + protected override async Task 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> GetProcessMapPlanAsync(int idWell, CancellationToken token) + protected override async Task> 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

"Слайд", _ => throw new ArgumentOutOfRangeException() }; - + return dto; }); diff --git a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanExportService.cs b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanExportService.cs index 6d684946..458877d1 100644 --- a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanExportService.cs +++ b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanExportService.cs @@ -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 : ExportExcelService, - IProcessMapPlanExportService +public abstract class ProcessMapPlanExportService : ExportExcelService where TDto : ChangeLogAbstract { protected readonly IWellService wellService; @@ -24,21 +23,10 @@ public abstract class ProcessMapPlanExportService : ExportExcelService ExportAsync(int idWell, CancellationToken token) + + protected override async Task> 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 BuildFileNameAsync(int idWell, CancellationToken token); - - protected virtual async Task> 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; } diff --git a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanReamExportService.cs b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanReamExportService.cs index b2452172..c3dc5d69 100644 --- a/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanReamExportService.cs +++ b/AsbCloudInfrastructure/Services/ProcessMapPlan/Export/ProcessMapPlanReamExportService.cs @@ -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 BuildFileNameAsync(int idWell, CancellationToken token) + + protected override async Task BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token) { - var caption = await wellService.GetWellCaptionByIdAsync(idWell, token); + var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token); return $"{caption}_РТК_План_проработка.xlsx"; } diff --git a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryExportService.cs b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryExportService.cs index 489b15f4..fe78b6b5 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryExportService.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryExportService.cs @@ -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 : ExportExcelService, - ITrajectoryExportService +public abstract class TrajectoryExportService : ExportExcelService where TDto : TrajectoryGeoDto { protected readonly IWellService wellService; @@ -22,14 +22,6 @@ public abstract class TrajectoryExportService : ExportExcelService, 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 BuildFileNameAsync(int idWell, CancellationToken token); + protected override Task> GetDtosAsync(WellRelatedExportRequest options, CancellationToken token) => + trajectoryRepository.GetAsync(options.IdWell, token); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactManualExportService.cs b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactManualExportService.cs index f1ccbc89..40b7d1c8 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactManualExportService.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactManualExportService.cs @@ -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 new TrajectoryFactManualTemplate(); - - protected override async Task BuildFileNameAsync(int idWell, CancellationToken token) + + protected override async Task BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token) { - var caption = await wellService.GetWellCaptionByIdAsync(idWell, token); + var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token); return $"{caption}_Фактическая_траектория.xlsx"; } diff --git a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactNnbExportService.cs b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactNnbExportService.cs index 2bab0783..3b3206f5 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactNnbExportService.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryFactNnbExportService.cs @@ -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 new TrajectoryFactNnbTemplate(); - protected override async Task BuildFileNameAsync(int idWell, CancellationToken token) + protected override async Task BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token) { - var caption = await wellService.GetWellCaptionByIdAsync(idWell, token); + var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token); return $"{caption}_Траектория_ННБ.xlsx"; } diff --git a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryPlanExportService.cs b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryPlanExportService.cs index 5a931939..37bce068 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryPlanExportService.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Export/TrajectoryPlanExportService.cs @@ -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 { - public TrajectoryPlanExportService(IWellService wellService, + public TrajectoryPlanExportService(IWellService wellService, ITrajectoryEditableRepository trajectoryRepository) : base(wellService, trajectoryRepository) { } protected override ITemplateParameters TemplateParameters => new TrajectoryPlanTemplate(); - - protected override async Task BuildFileNameAsync(int idWell, CancellationToken token) + + protected override async Task BuildFileNameAsync(WellRelatedExportRequest options, CancellationToken token) { - var caption = await wellService.GetWellCaptionByIdAsync(idWell, token); + var caption = await wellService.GetWellCaptionByIdAsync(options.IdWell, token); return $"{caption}_Плановая_Траектория.xlsx"; } diff --git a/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryExportTest.cs b/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryExportTest.cs index 1c026185..34d2077c 100644 --- a/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryExportTest.cs +++ b/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryExportTest.cs @@ -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 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(); @@ -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); } } diff --git a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs index c3afebb3..a7d1763e 100644 --- a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs @@ -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 : ControllerBase private readonly IChangeLogRepository repository; private readonly IWellService wellService; private readonly IParserService parserService; - private readonly IProcessMapPlanExportService processMapPlanExportService; + private readonly IExportService processMapPlanExportService; protected ProcessMapPlanBaseController(IChangeLogRepository repository, IWellService wellService, IParserService parserService, - IProcessMapPlanExportService processMapPlanExportService) + IExportService processMapPlanExportService) { this.repository = repository; this.wellService = wellService; @@ -280,7 +281,8 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task 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); } diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryController.cs index 6311d34a..a4521ef8 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryController.cs @@ -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 trajectoryExportService; private readonly ITrajectoryRepository trajectoryRepository; protected TrajectoryController(IWellService wellService, - ITrajectoryExportService trajectoryExportService, + TrajectoryExportService trajectoryExportService, ITrajectoryRepository 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); } diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs index a014e1bc..56a08c1e 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs @@ -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 parserService, - ITrajectoryExportService trajectoryExportService, + TrajectoryExportService trajectoryExportService, ITrajectoryEditableRepository trajectoryRepository) : base(wellService, trajectoryExportService, trajectoryRepository) {