Рефакторинг контроллеров использующий парсинг 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.Requests.ParserOptions;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers.Interfaces;
public interface IControllerWithParser<TDto, in TOptions>
public interface IWellControllerWithParser<TDto, in TOptions>
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();
}

View File

@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Http;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests;
using System;
using System.IO;
using AsbCloudApp.Services;
using System.Linq;
using AsbCloudApp.Data;
@ -26,12 +25,11 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
IControllerWithParser<TDto, IParserOptionsRequest>
IWellControllerWithParser<TDto, IParserOptionsRequest>
where TDto : ProcessMapPlanBaseDto
{
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
private readonly IWellService wellService;
private readonly IParserService<TDto, IParserOptionsRequest> parserService;
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService,
@ -40,24 +38,14 @@ public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase,
{
this.repository = repository;
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; }
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>
@ -246,7 +234,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

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

View File

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