2023-08-24 17:03:12 +10:00
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.ModelBinding ;
using PARR.API.Contracts.V1 ;
2023-08-25 10:25:24 +10:00
using PARR.API.Contracts.V1.Responses.Base ;
2023-08-24 17:03:12 +10:00
using PARR.API.Controllers.V1.Base ;
2023-08-25 10:25:24 +10:00
using PARR.BLL.Services.Interfaces ;
using PARR.BLL.Settings ;
2023-08-24 17:03:12 +10:00
namespace PARR.API.Controllers.V1
{
public class EsppDataController : BaseApiController
{
private readonly IFileService fileService ;
2023-08-25 10:25:24 +10:00
private readonly StorageSettings storageSettings ;
2023-08-24 17:03:12 +10:00
2023-08-25 10:25:24 +10:00
public EsppDataController (
ILogger < EsppDataController > logger ,
IFileService fileService ,
StorageSettings storageSettings
)
2023-08-24 17:03:12 +10:00
{
Logger = logger ;
this . fileService = fileService ;
2023-08-25 10:25:24 +10:00
this . storageSettings = storageSettings ;
2023-08-24 17:03:12 +10:00
}
public ILogger < EsppDataController > Logger { get ; }
/// <summary>
/// Загрузить файл списка шаблонов полученных из ЕСПП в формате csv
/// </summary>
/// <returns></returns>
[HttpPost(ApiRoutes.EsppData.UploadTemplates)]
2023-08-25 10:25:24 +10:00
public async Task < IActionResult > UploadTemplates ( [ BindRequired ] IFormFile file )
2023-08-24 17:03:12 +10:00
{
2023-08-25 10:25:24 +10:00
if ( ! fileService . IsExtensionAllowed ( file , storageSettings . EsppTemplates ! . AllowedExtensions ) )
return BadRequest (
new Response ( false ,
new List < ErrorModel > {
new ErrorModel {
Message = $"Недопустимое расширение файла. Разрешенные расширения: {string.Join(" , ", storageSettings.EsppTemplates.AllowedExtensions)}"
} } ) ) ;
if ( ! fileService . IsNotExceededLimit ( file , storageSettings . EsppTemplates . MaxFileSizeMb ) )
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { FieldName = nameof ( file ) , Message = $"Размер файла превышает {storageSettings.EsppTemplates.MaxFileSizeMb} МБайт" } } ) ) ;
var uploadResult = await fileService . UploadAsync ( file , storageSettings . EsppTemplates . TemplatePath ) ;
if ( uploadResult = = null )
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = "Ошибка при сохранении файла" } } ) ) ;
return Ok ( new Response < string > ( "" , true , new List < ErrorModel > ( ) , $"Файл загружен." ) ) ;
2023-08-24 17:03:12 +10:00
}
}
}