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