Рефакторинг контроллеров использующий парсинг Excel файлов

This commit is contained in:
Степанов Дмитрий 2024-02-15 10:00:17 +03:00
parent 7090daf494
commit 8a429cfe8b
4 changed files with 28 additions and 45 deletions

View File

@ -1,13 +1,22 @@
using System.IO; using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers.Interfaces; namespace AsbCloudWebApi.Controllers.Interfaces;
public interface IControllerWithParser<TDto, in TOptions> public interface IWellControllerWithParser<TDto, in TOptions>
where TDto : class, IId where TDto : class, IId
where TOptions : class, IParserOptionsRequest
{ {
ActionResult<ParserResultDto<TDto>> Parse(Stream file, TOptions options); IParserService<TDto, TOptions> ParserService { get; }
Task<ActionResult<ParserResultDto<TDto>>> Parse(int idWell,
[FromForm] IFormFileCollection files,
CancellationToken token);
IActionResult GetTemplate(); IActionResult GetTemplate();
} }

View File

@ -9,7 +9,6 @@ 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;
@ -26,12 +25,11 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
[Route("api/well/{idWell}/[controller]")] [Route("api/well/{idWell}/[controller]")]
[Authorize] [Authorize]
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase, public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
IControllerWithParser<TDto, IParserOptionsRequest> 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 IParserService<TDto, IParserOptionsRequest> parserService;
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository, protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService, IWellService wellService,
@ -40,24 +38,14 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
{ {
this.repository = repository; this.repository = repository;
this.wellService = wellService; this.wellService = wellService;
parserService = parserFactory.Create<TDto, IParserOptionsRequest>(idParserService);
ParserService = parserFactory.Create<TDto, IParserOptionsRequest>(idParserService);
} }
public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
protected abstract string TemplateFileName { get; } protected abstract string TemplateFileName { get; }
ActionResult<ParserResultDto<TDto>> IControllerWithParser<TDto, IParserOptionsRequest>.Parse(Stream file, IParserOptionsRequest options)
{
try
{
var parserResult = parserService.Parse(file, options);
return Ok(parserResult);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest("files", ex.Message);
}
}
/// <summary> /// <summary>
/// Добавление /// Добавление
/// </summary> /// </summary>
@ -246,7 +234,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);
} }

View File

@ -6,7 +6,6 @@ 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;
@ -23,10 +22,9 @@ namespace AsbCloudWebApi.Controllers.Trajectory
[ApiController] [ApiController]
[Authorize] [Authorize]
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>, public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
IControllerWithParser<TDto, IParserOptionsRequest> IWellControllerWithParser<TDto, IParserOptionsRequest>
where TDto : TrajectoryGeoDto where TDto : TrajectoryGeoDto
{ {
private readonly IParserService<TDto, IParserOptionsRequest> parserService;
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository; private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
protected TrajectoryEditableController(IWellService wellService, protected TrajectoryEditableController(IWellService wellService,
@ -39,25 +37,13 @@ namespace AsbCloudWebApi.Controllers.Trajectory
trajectoryExportService, trajectoryExportService,
trajectoryRepository) trajectoryRepository)
{ {
parserService = parserServiceFactory.Create<TDto, IParserOptionsRequest>(idParserService);
this.trajectoryRepository = trajectoryRepository; this.trajectoryRepository = trajectoryRepository;
} ParserService = parserServiceFactory.Create<TDto, IParserOptionsRequest>(idParserService);
ActionResult<ParserResultDto<TDto>> IControllerWithParser<TDto, IParserOptionsRequest>.Parse(Stream file,
IParserOptionsRequest options)
{
try
{
var parserResult = parserService.Parse(file, options);
return Ok(parserResult);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest("files", ex.Message);
}
} }
/// <summary> public IParserService<TDto, IParserOptionsRequest> ParserService { get; }
/// <summary>
/// Возвращает excel шаблон для заполнения строк траектории /// Возвращает excel шаблон для заполнения строк траектории
/// </summary> /// </summary>
/// <returns>Запрашиваемый файл</returns> /// <returns>Запрашиваемый файл</returns>
@ -67,7 +53,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);
} }

View File

@ -105,7 +105,7 @@ public static class Extensions
/// <param name="options"></param> /// <param name="options"></param>
/// <returns></returns> /// <returns></returns>
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>( public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(
this IControllerWithParser<TDto, TOptions> controller, this IWellControllerWithParser<TDto, TOptions> controller,
IFormFileCollection files, IFormFileCollection files,
TOptions options) TOptions options)
where TDto : class, IId where TDto : class, IId
@ -120,6 +120,6 @@ public static class Extensions
var stream = file.OpenReadStream(); var stream = file.OpenReadStream();
return controller.Parse(stream, options); return controller.ParserService.Parse(stream, options);
} }
} }