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

This commit is contained in:
Степанов Дмитрий 2024-02-16 11:54:46 +03:00
parent 63e2e1b180
commit e2764504aa
7 changed files with 63 additions and 92 deletions

View File

@ -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();
}

View File

@ -9,12 +9,12 @@ using Microsoft.AspNetCore.Http;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests;
using System;
using System.IO;
using AsbCloudApp.Services;
using System.Linq;
using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudInfrastructure.Services.Parser;
using AsbCloudWebApi.Controllers.Interfaces;
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
@ -24,26 +24,22 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
[ApiController]
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
IWellControllerWithParser<TDto, IParserOptionsRequest>
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
where TDto : ProcessMapPlanBaseDto
{
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
private readonly IWellService wellService;
private readonly ParserExcelService<TDto> parserService;
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService,
ParserServiceFactory parserFactory,
int idParserService)
ParserExcelService<TDto> parserService)
{
this.repository = repository;
this.wellService = wellService;
ParserService = parserFactory.Create<TDto, IParserOptionsRequest>(idParserService);
this.parserService = parserService;
}
public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
protected abstract string TemplateFileName { get; }
/// <summary>
@ -221,7 +217,17 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
{
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>
@ -234,7 +240,7 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetTemplate()
{
var stream = ParserService.GetTemplateFile();
var stream = parserService.GetTemplateFile();
return File(stream, "application/octet-stream", TemplateFileName);
}

View File

@ -2,7 +2,7 @@
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.Parser;
using AsbCloudInfrastructure.Services.ProcessMapPlan.Parser;
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
@ -10,8 +10,8 @@ public class ProcessMapPlanDrillingController : ProcessMapPlanBaseController<Pro
{
public ProcessMapPlanDrillingController(IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService,
ParserServiceFactory parserFactory)
: base(repository, wellService, parserFactory, ParserIds.IdProcessMapPlanDrillingParser)
ProcessMapPlanDrillingParser parserService)
: base(repository, wellService, parserService)
{
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data.Trajectory;
using System;
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.Trajectory.Export;
@ -6,12 +7,12 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudInfrastructure.Services.Parser;
using AsbCloudWebApi.Controllers.Interfaces;
namespace AsbCloudWebApi.Controllers.Trajectory
{
@ -21,28 +22,22 @@ namespace AsbCloudWebApi.Controllers.Trajectory
/// </summary>
[ApiController]
[Authorize]
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
IWellControllerWithParser<TDto, IParserOptionsRequest>
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>
where TDto : TrajectoryGeoDto
{
private readonly ParserExcelService<TDto> parserService;
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
protected TrajectoryEditableController(IWellService wellService,
ParserServiceFactory parserServiceFactory,
ParserExcelService<TDto> parserService,
TrajectoryExportService<TDto> trajectoryExportService,
ITrajectoryEditableRepository<TDto> trajectoryRepository,
int idParserService)
: base(
wellService,
trajectoryExportService,
trajectoryRepository)
ITrajectoryEditableRepository<TDto> trajectoryRepository)
: base(wellService, trajectoryExportService, trajectoryRepository)
{
this.parserService = parserService;
this.trajectoryRepository = trajectoryRepository;
ParserService = parserServiceFactory.Create<TDto, IParserOptionsRequest>(idParserService);
}
public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
/// <summary>
/// Возвращает excel шаблон для заполнения строк траектории
/// </summary>
@ -53,7 +48,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetTemplate()
{
var stream = ParserService.GetTemplateFile();
var stream = parserService.GetTemplateFile();
return File(stream, "application/octet-stream", fileName);
}
@ -79,7 +74,17 @@ namespace AsbCloudWebApi.Controllers.Trajectory
if (!await CanUserAccessToWellAsync(idWell, token))
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>

View File

@ -1,9 +1,8 @@
using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure;
using AsbCloudInfrastructure.Services.Parser;
using AsbCloudInfrastructure.Services.Trajectory.Export;
using AsbCloudInfrastructure.Services.Trajectory.Parser;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers.Trajectory;
@ -19,13 +18,9 @@ public class TrajectoryFactManualController : TrajectoryEditableController<Traje
public TrajectoryFactManualController(IWellService wellService,
TrajectoryFactManualExportService trajectoryExportService,
ParserServiceFactory parserServiceFactory,
TrajectoryFactManualParser parserService,
ITrajectoryEditableRepository<TrajectoryGeoFactDto> trajectoryRepository)
: base(wellService,
parserServiceFactory,
trajectoryExportService,
trajectoryRepository,
ParserIds.IdTrajectoryFactManualParser)
: base(wellService, parserService, trajectoryExportService, trajectoryRepository)
{
}
}

View File

@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudInfrastructure.Services.Parser;
using AsbCloudInfrastructure.Services.Trajectory.Parser;
namespace AsbCloudWebApi.Controllers.Trajectory
{
@ -23,15 +23,11 @@ namespace AsbCloudWebApi.Controllers.Trajectory
protected override string fileName => "ЕЦП_шаблон_файла_плановая_траектория.xlsx";
public TrajectoryPlanController(IWellService wellService,
TrajectoryPlanParser parserService,
TrajectoryPlanExportService trajectoryExportService,
ParserServiceFactory parserServiceFactory,
ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryRepository,
TrajectoryService trajectoryVisualizationService)
: base(wellService,
parserServiceFactory,
trajectoryExportService,
trajectoryRepository,
ParserIds.IdTrajectoryPlanParser)
: base(wellService, parserService, trajectoryExportService, trajectoryRepository)
{
this.trajectoryVisualizationService = trajectoryVisualizationService;
}

View File

@ -8,8 +8,9 @@ using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudWebApi.Controllers.Interfaces;
using AsbCloudInfrastructure.Services.Parser;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Mvc;
@ -96,30 +97,20 @@ public static class Extensions
}
/// <summary>
/// Вызов парсера со стандартной валидацией входного файла
/// Получение Excel
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TOptions"></typeparam>
/// <param name="controller"></param>
/// <param name="files"></param>
/// <param name="options"></param>
/// <returns></returns>
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(
this IWellControllerWithParser<TDto, TOptions> controller,
IFormFileCollection files,
TOptions options)
where TDto : class, IId
where TOptions : class, IParserOptionsRequest
/// <exception cref="ArgumentInvalidException"></exception>
public static Stream GetExcelFile(this IFormFileCollection files)
{
if (files.Count < 1)
return MakeBadRequestObjectResult(nameof(files), "Нет файла");
throw new ArgumentInvalidException(nameof(files), "Нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return MakeBadRequestObjectResult(nameof(files), "Требуется .xlsx файл.");
throw new ArgumentInvalidException(nameof(files), "Требуется .xlsx файл.");
var stream = file.OpenReadStream();
return controller.ParserService.Parse(stream, options);
return file.OpenReadStream();
}
}