diff --git a/DoliMiddlewareApi/Program.cs b/DoliMiddlewareApi/Program.cs new file mode 100644 index 0000000..e97329c --- /dev/null +++ b/DoliMiddlewareApi/Program.cs @@ -0,0 +1,72 @@ +using DoliMiddlewareApi.Exceptions; +using DoliMiddlewareApi.Services; +using Microsoft.AspNetCore.Diagnostics; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers() + .AddJsonOptions(options => + { + // Serializar enums como strings en vez de números + options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); + + // Usar camelCase para propiedades (id en vez de Id) + options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase; + }); +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + + +// Typed Client - Inyecta HttpClient directamente en DolibarrApiClient +builder.Services.AddHttpClient(client => +{ + client.BaseAddress = new Uri(builder.Configuration["Dolibarr:ApiUrl"]!); + client.DefaultRequestHeaders.Add("DOLAPIKEY", builder.Configuration["Dolibarr:ApiKey"]!); +}); + +var app = builder.Build(); + +// Global exception handler +app.UseExceptionHandler(errorApp => +{ + errorApp.Run(async context => + { + var exceptionHandler = context.Features.Get(); + var exception = exceptionHandler?.Error; + + var (statusCode, message) = exception switch + { + NotFoundException notFound => (StatusCodes.Status404NotFound, notFound.Message), + UnauthorizedException unauthorized => (StatusCodes.Status401Unauthorized, unauthorized.Message), + ForbiddenException forbidden => (StatusCodes.Status403Forbidden, forbidden.Message), + BadRequestException badRequest => (StatusCodes.Status400BadRequest, badRequest.Message), + ApiException apiEx => (StatusCodes.Status500InternalServerError, apiEx.Message), + _ => (StatusCodes.Status500InternalServerError, "An unexpected error occurred") + }; + + context.Response.StatusCode = statusCode; + context.Response.ContentType = "application/json"; + + await context.Response.WriteAsJsonAsync(new + { + error = message, + statusCode + }); + }); +}); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); \ No newline at end of file diff --git a/DoliMiddlewareApi/Properties/launchSettings.json b/DoliMiddlewareApi/Properties/launchSettings.json new file mode 100644 index 0000000..c2c9f7a --- /dev/null +++ b/DoliMiddlewareApi/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5269", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7010;http://localhost:5269", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/DoliMiddlewareApi/appsettings.Development.json b/DoliMiddlewareApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/DoliMiddlewareApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/DoliMiddlewareApi/appsettings.json b/DoliMiddlewareApi/appsettings.json new file mode 100644 index 0000000..2917f4e --- /dev/null +++ b/DoliMiddlewareApi/appsettings.json @@ -0,0 +1,13 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Dolibarr": { + "ApiUrl": "http://localhost/api/index.php/", + "ApiKey": "3004128e4cfbf5d0d9805d2e9cd8c79d015d0a25" + } +} \ No newline at end of file