forked from ddrilling/AsbCloudServer
Рефакторинг контроллеров
This commit is contained in:
parent
e361ccf4c1
commit
dc19829948
@ -0,0 +1,13 @@
|
||||
using System.IO;
|
||||
using AsbCloudApp.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.Interfaces;
|
||||
|
||||
public interface IControllerWithParser<TDto, in TOptions>
|
||||
where TDto : class, IId
|
||||
{
|
||||
ActionResult<ParserResultDto<TDto>> Parse(Stream file, TOptions options);
|
||||
|
||||
IActionResult GetTemplate();
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
using System.IO;
|
||||
using AsbCloudApp.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.Interfaces;
|
||||
|
||||
public interface IHasExcelParser<TDto>
|
||||
where TDto : class, IId
|
||||
{
|
||||
ActionResult<ParserResultDto<TDto>> Parse(Stream file);
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
using System.IO;
|
||||
using AsbCloudApp.Data;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.Interfaces;
|
||||
|
||||
public interface IHasExcelParserWithOptions<TDto, in TOptions>
|
||||
where TDto : class, IId
|
||||
{
|
||||
ParserResultDto<TDto> Parse(Stream file, TOptions options);
|
||||
}
|
@ -10,6 +10,7 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using AsbCloudWebApi.Controllers.Interfaces;
|
||||
|
||||
@ -22,35 +23,33 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
|
||||
IHasExcelParser<TDto>
|
||||
IControllerWithParser<TDto, IParserOptionsRequest>
|
||||
where TDto : TrajectoryGeoDto
|
||||
{
|
||||
private readonly ParserServiceFactory parserServiceFactory;
|
||||
private readonly TrajectoryExportService<TDto> trajectoryExportService;
|
||||
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
||||
private readonly IParserService<TDto, IParserOptionsRequest> parserService;
|
||||
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
||||
|
||||
protected TrajectoryEditableController(IWellService wellService,
|
||||
ParserServiceFactory parserServiceFactory,
|
||||
TrajectoryExportService<TDto> trajectoryExportService,
|
||||
ITrajectoryEditableRepository<TDto> trajectoryRepository)
|
||||
ITrajectoryEditableRepository<TDto> trajectoryRepository,
|
||||
int idParserService)
|
||||
: base(
|
||||
wellService,
|
||||
trajectoryExportService,
|
||||
trajectoryRepository)
|
||||
{
|
||||
this.trajectoryExportService = trajectoryExportService;
|
||||
this.trajectoryRepository = trajectoryRepository;
|
||||
this.parserServiceFactory = parserServiceFactory;
|
||||
parserService = parserServiceFactory.Create<TDto, IParserOptionsRequest>(idParserService);
|
||||
this.trajectoryRepository = trajectoryRepository;
|
||||
}
|
||||
|
||||
protected abstract int IdParserService { get; }
|
||||
|
||||
ActionResult<ParserResultDto<TDto>> IHasExcelParser<TDto>.Parse(Stream file)
|
||||
ActionResult<ParserResultDto<TDto>> IControllerWithParser<TDto, IParserOptionsRequest>.Parse(Stream file,
|
||||
IParserOptionsRequest options)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parserService = parserServiceFactory.GetParser<TDto>(IdParserService);
|
||||
return Ok(parserService.Parse(file));
|
||||
var parserResult = parserService.Parse(file, options);
|
||||
return Ok(parserResult);
|
||||
}
|
||||
catch (FileFormatException ex)
|
||||
{
|
||||
@ -68,7 +67,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public IActionResult GetTemplate()
|
||||
{
|
||||
var stream = trajectoryExportService.GetTemplateFile();
|
||||
var stream = parserService.GetTemplateFile();
|
||||
return File(stream, "application/octet-stream", fileName);
|
||||
}
|
||||
|
||||
@ -94,46 +93,19 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||
return Forbid();
|
||||
|
||||
return this.ParseExcelFile(files);
|
||||
return this.ParseExcelFile(files, IParserOptionsRequest.Empty());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить одну новую строчку координат для плановой траектории
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="row"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns>количество успешно записанных строк в БД</returns>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> AddAsync(int idWell, [FromBody] TDto row,
|
||||
CancellationToken token)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
var idUser = User.GetUserId();
|
||||
if (!idUser.HasValue)
|
||||
return Forbid();
|
||||
row.IdUser = idUser.Value;
|
||||
row.IdWell = idWell;
|
||||
var result = await trajectoryRepository.AddAsync(row, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить массив строчек координат для плановой траектории
|
||||
/// Добавление
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="deleteBeforeInsert"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns>количество успешно записанных строк в БД</returns>
|
||||
[HttpPost("range/{deleteBeforeInsert:bool}")]
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> AddRangeAsync(int idWell,
|
||||
[FromBody] IEnumerable<TDto> dtos,
|
||||
bool deleteBeforeInsert,
|
||||
CancellationToken token)
|
||||
public async Task<IActionResult> InsertRangeAsync(int idWell, [FromBody] IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||
return Forbid();
|
||||
@ -149,9 +121,39 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
dto.IdWell = idWell;
|
||||
}
|
||||
|
||||
if (deleteBeforeInsert)
|
||||
await trajectoryRepository.DeleteByIdWellAsync(idWell, token);
|
||||
var result = await trajectoryRepository.AddRangeAsync(dtos, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удалить все по скважине и добавить новые
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("replace")]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> ClearAndInsertRangeAsync(int idWell, [FromBody] IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
//TODO: это вся радость требует рефакторинга.
|
||||
//Удаление с добавлением новых записей должно происходить в рамках одной транзакции, да и вообще должно быть реализовано на уровне репозиторий.
|
||||
//Рефакторинг будет когда доберёмся до журнала изменений для траекторий.
|
||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||
return Forbid();
|
||||
|
||||
var idUser = User.GetUserId();
|
||||
|
||||
if (!idUser.HasValue)
|
||||
return Forbid();
|
||||
|
||||
foreach (var dto in dtos)
|
||||
{
|
||||
dto.IdUser = idUser.Value;
|
||||
dto.IdWell = idWell;
|
||||
}
|
||||
|
||||
await trajectoryRepository.DeleteByIdWellAsync(idWell, token);
|
||||
var result = await trajectoryRepository.AddRangeAsync(dtos, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
@ -20,9 +20,11 @@ public class TrajectoryFactManualController : TrajectoryEditableController<Traje
|
||||
TrajectoryFactManualExportService trajectoryExportService,
|
||||
ParserServiceFactory parserServiceFactory,
|
||||
ITrajectoryEditableRepository<TrajectoryGeoFactDto> trajectoryRepository)
|
||||
: base(wellService, parserServiceFactory, trajectoryExportService, trajectoryRepository)
|
||||
: base(wellService,
|
||||
parserServiceFactory,
|
||||
trajectoryExportService,
|
||||
trajectoryRepository,
|
||||
ParserServiceFactory.IdTrajectoryFactManualParserService)
|
||||
{
|
||||
}
|
||||
|
||||
protected override int IdParserService => ParserServiceFactory.IdTrajectoryFactManualParserService;
|
||||
}
|
@ -27,13 +27,15 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
ParserServiceFactory parserServiceFactory,
|
||||
ITrajectoryEditableRepository<TrajectoryGeoPlanDto> trajectoryRepository,
|
||||
TrajectoryService trajectoryVisualizationService)
|
||||
: base(wellService, parserServiceFactory, trajectoryExportService, trajectoryRepository)
|
||||
: base(wellService,
|
||||
parserServiceFactory,
|
||||
trajectoryExportService,
|
||||
trajectoryRepository,
|
||||
ParserServiceFactory.IdTrajectoryPlanParserService)
|
||||
{
|
||||
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
||||
}
|
||||
|
||||
protected override int IdParserService => ParserServiceFactory.IdTrajectoryPlanParserService;
|
||||
|
||||
/// <summary>
|
||||
/// Получение координат для визуализации траектории (плановой и фактической)
|
||||
/// </summary>
|
||||
|
@ -87,17 +87,9 @@ namespace Microsoft.AspNetCore.Mvc
|
||||
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
||||
return options;
|
||||
}
|
||||
|
||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto>(this IHasExcelParser<TDto> controller,
|
||||
IFormFileCollection files)
|
||||
where TDto : class, IId
|
||||
{
|
||||
using var file = GetExcelFile(files);
|
||||
return controller.Parse(file);
|
||||
}
|
||||
|
||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFileWithOptions<TDto, TOptions>(
|
||||
this IHasExcelParserWithOptions<TDto, TOptions> controller,
|
||||
|
||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(
|
||||
this IControllerWithParser<TDto, TOptions> controller,
|
||||
IFormFileCollection files,
|
||||
TOptions options)
|
||||
where TDto : class, IId
|
||||
|
Loading…
Reference in New Issue
Block a user