New Layout
This commit is contained in:
@@ -15,14 +15,8 @@ namespace GeradoresWS.Controllers
|
||||
}
|
||||
|
||||
#region NIF
|
||||
[HttpGet("GetNIFTypes")]
|
||||
public List<NIF.NIFType> GetNIFTypes()
|
||||
{
|
||||
return new List<NIF.NIFType>();
|
||||
}
|
||||
|
||||
[HttpGet("GenerateNIF")]
|
||||
public string GenerateNIF(string? type)
|
||||
public string GenerateNIF(NIF.NIFType type)
|
||||
{
|
||||
var nif = new NIF(_context);
|
||||
return nif.Generate(type);
|
||||
@@ -49,5 +43,13 @@ namespace GeradoresWS.Controllers
|
||||
return NISS.Validate(nif);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CC
|
||||
[HttpGet("GenerateCC")]
|
||||
public string GenerateCC()
|
||||
{
|
||||
return CartaoCidadao.Generate();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
65
GeradoresWS/Filters/EnumDescriptionFilter.cs
Normal file
65
GeradoresWS/Filters/EnumDescriptionFilter.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Primus.ProfitabilityModel.WebAPI.Filters
|
||||
{
|
||||
public class EnumDescriptionFilter : ISchemaFilter
|
||||
{
|
||||
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
|
||||
{
|
||||
if (context.Type.IsEnum)
|
||||
{
|
||||
// Obter os valores do enum
|
||||
var enumValues = Enum.GetValues(context.Type)
|
||||
.Cast<Enum>()
|
||||
.ToList();
|
||||
|
||||
// Obter os nomes dos enums
|
||||
var enumNames = enumValues.Select(e => e.ToString()).ToList();
|
||||
|
||||
// Obter as descrições dos enums
|
||||
var enumDescriptions = enumValues.Select(e => GetEnumDescription(e)).ToList();
|
||||
|
||||
// Adicionar as extensões com os nomes e descrições
|
||||
var enumNamesArray = new OpenApiArray();
|
||||
foreach (var name in enumNames)
|
||||
{
|
||||
enumNamesArray.Add(new OpenApiString(name));
|
||||
}
|
||||
|
||||
var enumDescriptionsArray = new OpenApiArray();
|
||||
foreach (var description in enumDescriptions)
|
||||
{
|
||||
enumDescriptionsArray.Add(new OpenApiString(description));
|
||||
}
|
||||
|
||||
schema.Extensions.Add("x-enumNames", enumDescriptionsArray);
|
||||
schema.Extensions.Add("x-enumDescriptions", enumDescriptionsArray);
|
||||
|
||||
// Adicionar os valores do enum ao campo Enum
|
||||
schema.Enum = enumNames
|
||||
.Select(name => new OpenApiString(name))
|
||||
.Cast<IOpenApiAny>()
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetEnumDescription(Enum value)
|
||||
{
|
||||
// Verificar se o campo é nulo antes de chamar GetCustomAttribute
|
||||
var field = value.GetType().GetField(value.ToString());
|
||||
if (field == null)
|
||||
{
|
||||
return value.ToString(); // Se o campo for nulo, retorna o nome do valor do enum
|
||||
}
|
||||
|
||||
// Acessa o atributo Description, caso exista
|
||||
var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
|
||||
|
||||
// Retorna a descrição ou o nome do valor do enum, se a descrição não estiver presente
|
||||
return attribute?.Description ?? value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,35 @@
|
||||
using GeradoresService.DAL;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Primus.ProfitabilityModel.WebAPI.Filters;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
});
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
|
||||
options.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
|
||||
Version = "v1",
|
||||
Title = "FactoryId",
|
||||
Contact = new OpenApiContact
|
||||
{
|
||||
Name = "Marco Santos"
|
||||
},
|
||||
});
|
||||
|
||||
// Registra o filtro de esquema que aplica descri<72><69>es para todos os enums
|
||||
options.SchemaFilter<EnumDescriptionFilter>();
|
||||
});
|
||||
|
||||
|
||||
var connectionString = builder.Configuration.GetConnectionString("GeradoresConnection");
|
||||
|
||||
Reference in New Issue
Block a user