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.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Requests.Import;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using AsbCloudWebApi.Controllers.Interfaces;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
{
|
||||
@ -20,7 +21,8 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>
|
||||
public abstract class TrajectoryEditableController<TDto> : TrajectoryController<TDto>,
|
||||
IHasExcelParser<TDto>
|
||||
where TDto : TrajectoryGeoDto
|
||||
{
|
||||
private readonly ParserServiceFactory parserServiceFactory;
|
||||
@ -41,7 +43,20 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
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>
|
||||
/// Возвращает excel шаблон для заполнения строк траектории
|
||||
@ -79,12 +94,9 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
if (!await CanUserAccessToWellAsync(idWell, token))
|
||||
return Forbid();
|
||||
|
||||
var parserService = parserServiceFactory.Create<TDto, TrajectoryParserRequest>(ParserOptions.IdParserService);
|
||||
|
||||
return this.ParseExcelFile(files, ParserOptions, parserService);
|
||||
return this.ParseExcelFile(files);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Добавить одну новую строчку координат для плановой траектории
|
||||
/// </summary>
|
||||
|
@ -1,6 +1,5 @@
|
||||
using AsbCloudApp.Data.Trajectory;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests.Import;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using AsbCloudInfrastructure.Services.Trajectory.Export;
|
||||
@ -25,10 +24,5 @@ public class TrajectoryFactManualController : TrajectoryEditableController<Traje
|
||||
{
|
||||
}
|
||||
|
||||
protected override TrajectoryParserRequest ParserOptions => new()
|
||||
{
|
||||
IdParserService = ParserServiceFactory.IdTrajectoryFactManualParserService,
|
||||
SheetName = "Фактическая траектория",
|
||||
HeaderRowsCount = 2
|
||||
};
|
||||
protected override int IdParserService => ParserServiceFactory.IdTrajectoryFactManualParserService;
|
||||
}
|
@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Requests.Import;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
@ -33,12 +32,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory
|
||||
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
||||
}
|
||||
|
||||
protected override TrajectoryParserRequest ParserOptions => new()
|
||||
{
|
||||
IdParserService = ParserServiceFactory.IdTrajectoryPlanParserService,
|
||||
SheetName = "Плановая траектория",
|
||||
HeaderRowsCount = 2
|
||||
};
|
||||
protected override int IdParserService => ParserServiceFactory.IdTrajectoryPlanParserService;
|
||||
|
||||
/// <summary>
|
||||
/// Получение координат для визуализации траектории (плановой и фактической)
|
||||
|
@ -8,8 +8,9 @@ using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Requests.Import;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudWebApi.Controllers.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
@ -87,32 +88,35 @@ namespace Microsoft.AspNetCore.Mvc
|
||||
return options;
|
||||
}
|
||||
|
||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(this ControllerBase controller,
|
||||
IFormFileCollection files,
|
||||
TOptions options,
|
||||
IParserService<TDto, TOptions> parserService)
|
||||
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto>(this IHasExcelParser<TDto> controller,
|
||||
IFormFileCollection files)
|
||||
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)
|
||||
return controller.ValidationBadRequest(nameof(files), "Нет файла");
|
||||
throw new ArgumentInvalidException(nameof(files), "Нет файла");
|
||||
|
||||
var file = files[0];
|
||||
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
||||
return controller.ValidationBadRequest(nameof(files), "Требуется .xlsx файл.");
|
||||
throw new ArgumentInvalidException(nameof(files), "Требуется .xlsx файл.");
|
||||
|
||||
using var stream = file.OpenReadStream();
|
||||
|
||||
try
|
||||
{
|
||||
var items = parserService.Parse(stream, options);
|
||||
|
||||
return controller.Ok(items);
|
||||
}
|
||||
catch (FileFormatException ex)
|
||||
{
|
||||
return controller.ValidationBadRequest(nameof(files), ex.Message);
|
||||
}
|
||||
return file.OpenReadStream();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user