forked from ddrilling/AsbCloudServer
Рефакторинг контроллеров
This commit is contained in:
parent
94c7e1e7c9
commit
96a8c3fbd3
11
AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs
Normal file
11
AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
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);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
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);
|
||||||
|
}
|
@ -6,11 +6,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.Import;
|
|
||||||
using AsbCloudInfrastructure.Services;
|
using AsbCloudInfrastructure.Services;
|
||||||
|
using AsbCloudWebApi.Controllers.Interfaces;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||||
{
|
{
|
||||||
@ -20,14 +21,15 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>
|
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
|
||||||
|
IHasExcelParser<TDto>
|
||||||
where TDto : TrajectoryGeoDto
|
where TDto : TrajectoryGeoDto
|
||||||
{
|
{
|
||||||
private readonly ParserServiceFactory parserServiceFactory;
|
private readonly ParserServiceFactory parserServiceFactory;
|
||||||
private readonly TrajectoryExportService<TDto> trajectoryExportService;
|
private readonly TrajectoryExportService<TDto> trajectoryExportService;
|
||||||
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
private readonly ITrajectoryEditableRepository<TDto> trajectoryRepository;
|
||||||
|
|
||||||
protected TrajectoryEditableController(IWellService wellService,
|
protected TrajectoryEditableController(IWellService wellService,
|
||||||
ParserServiceFactory parserServiceFactory,
|
ParserServiceFactory parserServiceFactory,
|
||||||
TrajectoryExportService<TDto> trajectoryExportService,
|
TrajectoryExportService<TDto> trajectoryExportService,
|
||||||
ITrajectoryEditableRepository<TDto> trajectoryRepository)
|
ITrajectoryEditableRepository<TDto> trajectoryRepository)
|
||||||
@ -41,7 +43,20 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
this.parserServiceFactory = parserServiceFactory;
|
this.parserServiceFactory = parserServiceFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract TrajectoryParserRequest ParserOptions { get; }
|
protected abstract int IdParserService { get; }
|
||||||
|
|
||||||
|
ActionResult<ParserResultDto<TDto>> IHasExcelParser<TDto>.Parse(Stream file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var parserService = parserServiceFactory.GetParser<TDto>(IdParserService);
|
||||||
|
return Ok(parserService.Parse(file));
|
||||||
|
}
|
||||||
|
catch (FileFormatException ex)
|
||||||
|
{
|
||||||
|
return this.ValidationBadRequest("files", ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Возвращает excel шаблон для заполнения строк траектории
|
/// Возвращает excel шаблон для заполнения строк траектории
|
||||||
@ -79,12 +94,9 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
|
|
||||||
var parserService = parserServiceFactory.Create<TDto, TrajectoryParserRequest>(ParserOptions.IdParserService);
|
return this.ParseExcelFile(files);
|
||||||
|
|
||||||
return this.ParseExcelFile(files, ParserOptions, parserService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавить одну новую строчку координат для плановой траектории
|
/// Добавить одну новую строчку координат для плановой траектории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
using AsbCloudApp.Data.Trajectory;
|
using AsbCloudApp.Data.Trajectory;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Requests.Import;
|
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudInfrastructure.Services;
|
using AsbCloudInfrastructure.Services;
|
||||||
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
||||||
@ -25,10 +24,5 @@ public class TrajectoryFactManualController : TrajectoryEditableController<Traje
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override TrajectoryParserRequest ParserOptions => new()
|
protected override int IdParserService => ParserServiceFactory.IdTrajectoryFactManualParserService;
|
||||||
{
|
|
||||||
IdParserService = ParserServiceFactory.IdTrajectoryFactManualParserService,
|
|
||||||
SheetName = "Фактическая траектория",
|
|
||||||
HeaderRowsCount = 2
|
|
||||||
};
|
|
||||||
}
|
}
|
@ -7,7 +7,6 @@ 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 AsbCloudApp.Requests.Import;
|
|
||||||
using AsbCloudInfrastructure.Services;
|
using AsbCloudInfrastructure.Services;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||||
@ -32,13 +31,8 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
|||||||
{
|
{
|
||||||
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override TrajectoryParserRequest ParserOptions => new()
|
protected override int IdParserService => ParserServiceFactory.IdTrajectoryPlanParserService;
|
||||||
{
|
|
||||||
IdParserService = ParserServiceFactory.IdTrajectoryPlanParserService,
|
|
||||||
SheetName = "Плановая траектория",
|
|
||||||
HeaderRowsCount = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение координат для визуализации траектории (плановой и фактической)
|
/// Получение координат для визуализации траектории (плановой и фактической)
|
||||||
|
@ -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.Requests.Import;
|
using AsbCloudApp.Exceptions;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Requests.ParserOptions;
|
||||||
|
using AsbCloudWebApi.Controllers.Interfaces;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc
|
namespace Microsoft.AspNetCore.Mvc
|
||||||
@ -87,32 +88,35 @@ namespace Microsoft.AspNetCore.Mvc
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(this ControllerBase controller,
|
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto>(this IHasExcelParser<TDto> controller,
|
||||||
IFormFileCollection files,
|
IFormFileCollection files)
|
||||||
TOptions options,
|
|
||||||
IParserService<TDto, TOptions> parserService)
|
|
||||||
where TDto : class, IId
|
where TDto : class, IId
|
||||||
where TOptions : ParserOptionsRequestBase
|
{
|
||||||
|
using var file = GetExcelFile(files);
|
||||||
|
return controller.Parse(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ActionResult<ParserResultDto<TDto>> ParseExcelFileWithOptions<TDto, TOptions>(
|
||||||
|
this IHasExcelParserWithOptions<TDto, TOptions> controller,
|
||||||
|
IFormFileCollection files,
|
||||||
|
TOptions options)
|
||||||
|
where TDto : class, IId
|
||||||
|
where TOptions : class, IParserOptionsRequest
|
||||||
|
{
|
||||||
|
using var file = GetExcelFile(files);
|
||||||
|
return controller.Parse(file, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream GetExcelFile(IFormFileCollection files)
|
||||||
{
|
{
|
||||||
if (files.Count < 1)
|
if (files.Count < 1)
|
||||||
return controller.ValidationBadRequest(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 controller.ValidationBadRequest(nameof(files), "Требуется .xlsx файл.");
|
throw new ArgumentInvalidException(nameof(files), "Требуется .xlsx файл.");
|
||||||
|
|
||||||
using var stream = file.OpenReadStream();
|
return file.OpenReadStream();
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var items = parserService.Parse(stream, options);
|
|
||||||
|
|
||||||
return controller.Ok(items);
|
|
||||||
}
|
|
||||||
catch (FileFormatException ex)
|
|
||||||
{
|
|
||||||
return controller.ValidationBadRequest(nameof(files), ex.Message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user