forked from ddrilling/AsbCloudServer
Рефакторинг контроллеров
This commit is contained in:
parent
63e2e1b180
commit
e2764504aa
@ -1,22 +0,0 @@
|
|||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudApp.Requests.ParserOptions;
|
|
||||||
using AsbCloudApp.Services;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Interfaces;
|
|
||||||
|
|
||||||
public interface IWellControllerWithParser<TDto, in TOptions>
|
|
||||||
where TDto : class, IId
|
|
||||||
where TOptions : class, IParserOptionsRequest
|
|
||||||
{
|
|
||||||
IParserService<TDto, TOptions> ParserService { get; }
|
|
||||||
|
|
||||||
Task<ActionResult<ParserResultDto<TDto>>> Parse(int idWell,
|
|
||||||
[FromForm] IFormFileCollection files,
|
|
||||||
CancellationToken token);
|
|
||||||
|
|
||||||
IActionResult GetTemplate();
|
|
||||||
}
|
|
@ -9,12 +9,12 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using AsbCloudApp.Exceptions;
|
using AsbCloudApp.Exceptions;
|
||||||
using AsbCloudApp.Requests;
|
using AsbCloudApp.Requests;
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Requests.ParserOptions;
|
using AsbCloudApp.Requests.ParserOptions;
|
||||||
using AsbCloudInfrastructure.Services.Parser;
|
using AsbCloudInfrastructure.Services.Parser;
|
||||||
using AsbCloudWebApi.Controllers.Interfaces;
|
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
||||||
|
|
||||||
@ -24,29 +24,25 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/well/{idWell}/[controller]")]
|
[Route("api/well/{idWell}/[controller]")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
|
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
||||||
IWellControllerWithParser<TDto, IParserOptionsRequest>
|
|
||||||
where TDto : ProcessMapPlanBaseDto
|
where TDto : ProcessMapPlanBaseDto
|
||||||
{
|
{
|
||||||
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
|
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
|
||||||
private readonly IWellService wellService;
|
private readonly IWellService wellService;
|
||||||
|
private readonly ParserExcelService<TDto> parserService;
|
||||||
|
|
||||||
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
|
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
|
||||||
IWellService wellService,
|
IWellService wellService,
|
||||||
ParserServiceFactory parserFactory,
|
ParserExcelService<TDto> parserService)
|
||||||
int idParserService)
|
|
||||||
{
|
{
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.wellService = wellService;
|
this.wellService = wellService;
|
||||||
|
this.parserService = parserService;
|
||||||
ParserService = parserFactory.Create<TDto, IParserOptionsRequest>(idParserService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
|
|
||||||
|
|
||||||
protected abstract string TemplateFileName { get; }
|
protected abstract string TemplateFileName { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление
|
/// Добавление
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idWell"></param>
|
/// <param name="idWell"></param>
|
||||||
@ -221,7 +217,17 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
|
|||||||
{
|
{
|
||||||
await AssertUserHasAccessToWell(idWell, token);
|
await AssertUserHasAccessToWell(idWell, token);
|
||||||
|
|
||||||
return this.ParseExcelFile(files, IParserOptionsRequest.Empty());
|
var stream = files.GetExcelFile();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var dto = parserService.Parse(stream, IParserOptionsRequest.Empty());
|
||||||
|
return Ok(dto);
|
||||||
|
}
|
||||||
|
catch (FileFormatException ex)
|
||||||
|
{
|
||||||
|
return this.ValidationBadRequest(nameof(files), ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -234,7 +240,7 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
|
|||||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
public IActionResult GetTemplate()
|
public IActionResult GetTemplate()
|
||||||
{
|
{
|
||||||
var stream = ParserService.GetTemplateFile();
|
var stream = parserService.GetTemplateFile();
|
||||||
return File(stream, "application/octet-stream", TemplateFileName);
|
return File(stream, "application/octet-stream", TemplateFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Requests;
|
using AsbCloudApp.Requests;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudInfrastructure.Services.Parser;
|
using AsbCloudInfrastructure.Services.ProcessMapPlan.Parser;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
||||||
|
|
||||||
@ -10,8 +10,8 @@ public class ProcessMapPlanDrillingController : ProcessMapPlanBaseController<Pro
|
|||||||
{
|
{
|
||||||
public ProcessMapPlanDrillingController(IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell> repository,
|
public ProcessMapPlanDrillingController(IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell> repository,
|
||||||
IWellService wellService,
|
IWellService wellService,
|
||||||
ParserServiceFactory parserFactory)
|
ProcessMapPlanDrillingParser parserService)
|
||||||
: base(repository, wellService, parserFactory, ParserIds.IdProcessMapPlanDrillingParser)
|
: base(repository, wellService, parserService)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data.Trajectory;
|
using System;
|
||||||
|
using AsbCloudApp.Data.Trajectory;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
||||||
@ -6,12 +7,12 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Requests.ParserOptions;
|
using AsbCloudApp.Requests.ParserOptions;
|
||||||
using AsbCloudInfrastructure.Services.Parser;
|
using AsbCloudInfrastructure.Services.Parser;
|
||||||
using AsbCloudWebApi.Controllers.Interfaces;
|
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||||
{
|
{
|
||||||
@ -21,28 +22,22 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
|
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>
|
||||||
IWellControllerWithParser<TDto, IParserOptionsRequest>
|
where TDto : TrajectoryGeoDto
|
||||||
where TDto : TrajectoryGeoDto
|
|
||||||
{
|
{
|
||||||
|
private readonly ParserExcelService<TDto> parserService;
|
||||||
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
||||||
|
|
||||||
protected TrajectoryEditableController(IWellService wellService,
|
protected TrajectoryEditableController(IWellService wellService,
|
||||||
ParserServiceFactory parserServiceFactory,
|
ParserExcelService<TDto> parserService,
|
||||||
TrajectoryExportService<TDto> trajectoryExportService,
|
TrajectoryExportService<TDto> trajectoryExportService,
|
||||||
ITrajectoryEditableRepository<TDto> trajectoryRepository,
|
ITrajectoryEditableRepository<TDto> trajectoryRepository)
|
||||||
int idParserService)
|
: base(wellService, trajectoryExportService, trajectoryRepository)
|
||||||
: base(
|
{
|
||||||
wellService,
|
this.parserService = parserService;
|
||||||
trajectoryExportService,
|
|
||||||
trajectoryRepository)
|
|
||||||
{
|
|
||||||
this.trajectoryRepository = trajectoryRepository;
|
this.trajectoryRepository = trajectoryRepository;
|
||||||
ParserService = parserServiceFactory.Create<TDto, IParserOptionsRequest>(idParserService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Возвращает excel шаблон для заполнения строк траектории
|
/// Возвращает excel шаблон для заполнения строк траектории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -53,7 +48,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
public IActionResult GetTemplate()
|
public IActionResult GetTemplate()
|
||||||
{
|
{
|
||||||
var stream = ParserService.GetTemplateFile();
|
var stream = parserService.GetTemplateFile();
|
||||||
return File(stream, "application/octet-stream", fileName);
|
return File(stream, "application/octet-stream", fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +74,17 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
|
|
||||||
return this.ParseExcelFile(files, IParserOptionsRequest.Empty());
|
var stream = files.GetExcelFile();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var dto = parserService.Parse(stream, IParserOptionsRequest.Empty());
|
||||||
|
return Ok(dto);
|
||||||
|
}
|
||||||
|
catch (FileFormatException ex)
|
||||||
|
{
|
||||||
|
return this.ValidationBadRequest(nameof(files), ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
using AsbCloudApp.Data.Trajectory;
|
using AsbCloudApp.Data.Trajectory;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudInfrastructure;
|
|
||||||
using AsbCloudInfrastructure.Services.Parser;
|
|
||||||
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
||||||
|
using AsbCloudInfrastructure.Services.Trajectory.Parser;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Trajectory;
|
namespace AsbCloudWebApi.Controllers.Trajectory;
|
||||||
@ -19,13 +18,9 @@ public class TrajectoryFactManualController : TrajectoryEditableController<Traje
|
|||||||
|
|
||||||
public TrajectoryFactManualController(IWellService wellService,
|
public TrajectoryFactManualController(IWellService wellService,
|
||||||
TrajectoryFactManualExportService trajectoryExportService,
|
TrajectoryFactManualExportService trajectoryExportService,
|
||||||
ParserServiceFactory parserServiceFactory,
|
TrajectoryFactManualParser parserService,
|
||||||
ITrajectoryEditableRepository<TrajectoryGeoFactDto> trajectoryRepository)
|
ITrajectoryEditableRepository<TrajectoryGeoFactDto> trajectoryRepository)
|
||||||
: base(wellService,
|
: base(wellService, parserService, trajectoryExportService, trajectoryRepository)
|
||||||
parserServiceFactory,
|
|
||||||
trajectoryExportService,
|
|
||||||
trajectoryRepository,
|
|
||||||
ParserIds.IdTrajectoryFactManualParser)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AsbCloudInfrastructure.Services.Parser;
|
using AsbCloudInfrastructure.Services.Trajectory.Parser;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||||
{
|
{
|
||||||
@ -23,15 +23,11 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
protected override string fileName => "ЕЦП_шаблон_файла_плановая_траектория.xlsx";
|
protected override string fileName => "ЕЦП_шаблон_файла_плановая_траектория.xlsx";
|
||||||
|
|
||||||
public TrajectoryPlanController(IWellService wellService,
|
public TrajectoryPlanController(IWellService wellService,
|
||||||
|
TrajectoryPlanParser parserService,
|
||||||
TrajectoryPlanExportService trajectoryExportService,
|
TrajectoryPlanExportService trajectoryExportService,
|
||||||
ParserServiceFactory parserServiceFactory,
|
|
||||||
ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryRepository,
|
ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryRepository,
|
||||||
TrajectoryService trajectoryVisualizationService)
|
TrajectoryService trajectoryVisualizationService)
|
||||||
: base(wellService,
|
: base(wellService, parserService, trajectoryExportService, trajectoryRepository)
|
||||||
parserServiceFactory,
|
|
||||||
trajectoryExportService,
|
|
||||||
trajectoryRepository,
|
|
||||||
ParserIds.IdTrajectoryPlanParser)
|
|
||||||
{
|
{
|
||||||
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,9 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Exceptions;
|
||||||
using AsbCloudApp.Requests.ParserOptions;
|
using AsbCloudApp.Requests.ParserOptions;
|
||||||
using AsbCloudWebApi.Controllers.Interfaces;
|
using AsbCloudInfrastructure.Services.Parser;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc;
|
namespace Microsoft.AspNetCore.Mvc;
|
||||||
@ -96,30 +97,20 @@ public static class Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вызов парсера со стандартной валидацией входного файла
|
/// Получение Excel
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDto"></typeparam>
|
|
||||||
/// <typeparam name="TOptions"></typeparam>
|
|
||||||
/// <param name="controller"></param>
|
|
||||||
/// <param name="files"></param>
|
/// <param name="files"></param>
|
||||||
/// <param name="options"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(
|
/// <exception cref="ArgumentInvalidException"></exception>
|
||||||
this IWellControllerWithParser<TDto, TOptions> controller,
|
public static Stream GetExcelFile(this IFormFileCollection files)
|
||||||
IFormFileCollection files,
|
|
||||||
TOptions options)
|
|
||||||
where TDto : class, IId
|
|
||||||
where TOptions : class, IParserOptionsRequest
|
|
||||||
{
|
{
|
||||||
if (files.Count < 1)
|
if (files.Count < 1)
|
||||||
return MakeBadRequestObjectResult(nameof(files), "Нет файла");
|
throw new ArgumentInvalidException(nameof(files), "Нет файла");
|
||||||
|
|
||||||
var file = files[0];
|
var file = files[0];
|
||||||
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
||||||
return MakeBadRequestObjectResult(nameof(files), "Требуется .xlsx файл.");
|
throw new ArgumentInvalidException(nameof(files), "Требуется .xlsx файл.");
|
||||||
|
|
||||||
var stream = file.OpenReadStream();
|
return file.OpenReadStream();
|
||||||
|
|
||||||
return controller.ParserService.Parse(stream, options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user