forked from ddrilling/AsbCloudServer
4874a9288b
1. Добавлен метод расширения для парсинга Excel файлов 2. Рефакторинг контроллеров траекторий
95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using AsbCloudApp.Data.User;
|
|
using AsbCloudWebApi.Converters;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Security.Claims;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Requests.Import;
|
|
using AsbCloudApp.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Microsoft.AspNetCore.Mvc
|
|
{
|
|
public static class Extentions
|
|
{
|
|
public static int? GetCompanyId(this ClaimsPrincipal user)
|
|
{
|
|
var claimIdCompany = user.FindFirst(nameof(UserDto.IdCompany));
|
|
if (claimIdCompany is null)
|
|
return null;
|
|
|
|
return int.TryParse(claimIdCompany.Value, out int uid)
|
|
? uid
|
|
: null;
|
|
}
|
|
|
|
public static int? GetUserId(this ClaimsPrincipal user)
|
|
{
|
|
var userId = user.FindFirst(nameof(UserDto.Id));
|
|
if (userId is null)
|
|
return null;
|
|
|
|
return int.TryParse(userId.Value, out int uid)
|
|
? uid
|
|
: null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>
|
|
/// Returns BadRequest with ValidationProblemDetails as body
|
|
/// </para>
|
|
/// <para>
|
|
/// Используйте этот метод только если валидацию нельзя сделать через
|
|
/// атрибуты валидации или IValidatableObject модели.
|
|
/// </para>
|
|
/// </summary>
|
|
/// <param name="controller"></param>
|
|
/// <param name="paramName"></param>
|
|
/// <param name="error"></param>
|
|
/// <returns></returns>
|
|
public static BadRequestObjectResult ValidationBadRequest(this ControllerBase controller, string paramName, string error)
|
|
{
|
|
var errors = new Dictionary<string, string[]> {
|
|
{ paramName, new[]{ error } }
|
|
};
|
|
var problem = new ValidationProblemDetails(errors);
|
|
return controller.BadRequest(problem);
|
|
}
|
|
|
|
public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options)
|
|
{
|
|
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
|
return options;
|
|
}
|
|
|
|
public static ActionResult<ParserResultDto<TDto>> ParseExcelFile<TDto, TOptions>(this ControllerBase controller,
|
|
IFormFileCollection files,
|
|
TOptions options,
|
|
IParserService<TDto, TOptions> parserService)
|
|
where TDto : class, IId
|
|
where TOptions : ParserOptionsRequestBase
|
|
{
|
|
if (files.Count < 1)
|
|
return controller.ValidationBadRequest(nameof(files), "Нет файла");
|
|
|
|
var file = files[0];
|
|
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
|
return controller.ValidationBadRequest(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);
|
|
}
|
|
}
|
|
}
|
|
} |