From ab80901280be333a91a3f156c752cd840d46b2f0 Mon Sep 17 00:00:00 2001 From: javiermengual Date: Wed, 31 Dec 2025 15:15:00 +0100 Subject: [PATCH] refactor: sacar los metodos de cada DTO a su propio servicio porque si no se me hara un god object , y lo mantendre simple sin mucha abstraccion. --- .../Controllers/InvoicesController.cs | 10 ++-- DoliMiddlewareApi/Program.cs | 4 ++ .../{ => Clients}/DolibarrApiClient.cs | 53 +++++++------------ DoliMiddlewareApi/Services/InvoiceService.cs | 39 ++++++++++++++ 4 files changed, 68 insertions(+), 38 deletions(-) rename DoliMiddlewareApi/Services/{ => Clients}/DolibarrApiClient.cs (61%) create mode 100644 DoliMiddlewareApi/Services/InvoiceService.cs diff --git a/DoliMiddlewareApi/Controllers/InvoicesController.cs b/DoliMiddlewareApi/Controllers/InvoicesController.cs index aee9662..4d6e3d6 100644 --- a/DoliMiddlewareApi/Controllers/InvoicesController.cs +++ b/DoliMiddlewareApi/Controllers/InvoicesController.cs @@ -9,11 +9,11 @@ namespace DoliMiddlewareApi.Controllers; [Route("api/[controller]")] public class InvoicesController : ControllerBase { - private readonly DolibarrApiClient _dolibarrClient; + private readonly InvoiceService _invoiceService; - public InvoicesController(DolibarrApiClient dolibarrClient) + public InvoicesController(InvoiceService invoiceService) { - _dolibarrClient = dolibarrClient; + _invoiceService = invoiceService; } [HttpGet] @@ -24,7 +24,7 @@ public class InvoicesController : ControllerBase [FromQuery] [Range(1, int.MaxValue)] int page = 1, [FromQuery] string? status = null) { - var invoices = await _dolibarrClient.GetInvoicesAsync(limit, page, status); + var invoices = await _invoiceService.GetInvoicesAsync(limit, page, status); return Ok(invoices); } @@ -34,7 +34,7 @@ public class InvoicesController : ControllerBase [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] public async Task> GetInvoice(int id) { - var invoice = await _dolibarrClient.GetInvoiceAsync(id); + var invoice = await _invoiceService.GetInvoiceAsync(id); return Ok(invoice); } } \ No newline at end of file diff --git a/DoliMiddlewareApi/Program.cs b/DoliMiddlewareApi/Program.cs index e122669..e43ad64 100644 --- a/DoliMiddlewareApi/Program.cs +++ b/DoliMiddlewareApi/Program.cs @@ -1,5 +1,6 @@ using DoliMiddlewareApi.Exceptions; using DoliMiddlewareApi.Services; +using DoliMiddlewareApi.Services.Clients; using Microsoft.AspNetCore.Diagnostics; var builder = WebApplication.CreateBuilder(args); @@ -27,6 +28,9 @@ builder.Services.AddHttpClient(client => client.DefaultRequestHeaders.Add("DOLAPIKEY", builder.Configuration["Dolibarr:ApiKey"]!); }); +// Registrar servicios de negocio +builder.Services.AddScoped(); + var app = builder.Build(); // Global exception handler diff --git a/DoliMiddlewareApi/Services/DolibarrApiClient.cs b/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs similarity index 61% rename from DoliMiddlewareApi/Services/DolibarrApiClient.cs rename to DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs index f2c549f..198e598 100644 --- a/DoliMiddlewareApi/Services/DolibarrApiClient.cs +++ b/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs @@ -1,12 +1,7 @@ -using System.Globalization; using System.Net; -using System.Net.Http.Json; -using DoliMiddlewareApi.Dtos; -using DoliMiddlewareApi.Dtos.Dolibarr; using DoliMiddlewareApi.Exceptions; -using DoliMiddlewareApi.Mappers; -namespace DoliMiddlewareApi.Services; +namespace DoliMiddlewareApi.Services.Clients; public class DolibarrApiClient { @@ -16,33 +11,10 @@ public class DolibarrApiClient { _httpClient = httpClient; } - - // ===== FACTURAS ===== - public async Task GetInvoiceAsync(int id) - { - var data = await GetResourceAsync($"invoices/{id}"); - return InvoiceMapper.MapToInvoiceDetailDto(data); - } - - public async Task> GetInvoicesAsync( - int limit = 50, - int page = 1, - string? status = null) - { - // empieza por 1 para el frontend - var endpoint = $"invoices?limit={limit}&page={page - 1}"; - - if (!string.IsNullOrEmpty(status)) - { - endpoint += $"&status={status}"; - } - - var dataList = await GetCollectionAsync(endpoint); - return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList(); - } - + + // ===== MÉTODOS GENÉRICOS ===== - private async Task GetResourceAsync(string endpoint) where T : class + public async Task GetResourceAsync(string endpoint) where T : class { var response = await _httpClient.GetAsync(endpoint); await EnsureSuccessOrThrowAsync(response, endpoint); @@ -51,7 +23,7 @@ public class DolibarrApiClient ?? throw new ApiException($"Failed to deserialize response from Dolibarr for endpoint '{endpoint}'"); } - private async Task> GetCollectionAsync(string endpoint) where T : class + public async Task> GetCollectionAsync(string endpoint) where T : class { var response = await _httpClient.GetAsync(endpoint); await EnsureSuccessOrThrowAsync(response, endpoint); @@ -61,6 +33,21 @@ public class DolibarrApiClient $"Failed to deserialize list response from Dolibarr for endpoint '{endpoint}'"); } + + + + + + + + + + + + + + + private async Task EnsureSuccessOrThrowAsync(HttpResponseMessage response, string endpoint) { if (response.IsSuccessStatusCode) return; diff --git a/DoliMiddlewareApi/Services/InvoiceService.cs b/DoliMiddlewareApi/Services/InvoiceService.cs new file mode 100644 index 0000000..de534e0 --- /dev/null +++ b/DoliMiddlewareApi/Services/InvoiceService.cs @@ -0,0 +1,39 @@ +using DoliMiddlewareApi.Dtos; +using DoliMiddlewareApi.Dtos.Dolibarr; +using DoliMiddlewareApi.Mappers; +using DoliMiddlewareApi.Services.Clients; + +namespace DoliMiddlewareApi.Services; + +public class InvoiceService +{ + private readonly DolibarrApiClient _apiClient; + + public InvoiceService(DolibarrApiClient apiClient) + { + _apiClient = apiClient; + } + + public async Task GetInvoiceAsync(int id) + { + var data = await _apiClient.GetResourceAsync($"invoices/{id}"); + return InvoiceMapper.MapToInvoiceDetailDto(data); + } + + public async Task> GetInvoicesAsync( + int limit = 50, + int page = 1, + string? status = null) + { + // empieza por 1 para el frontend + var endpoint = $"invoices?limit={limit}&page={page - 1}"; + + if (!string.IsNullOrEmpty(status)) + { + endpoint += $"&status={status}"; + } + + var dataList = await _apiClient.GetCollectionAsync(endpoint); + return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList(); + } +} \ No newline at end of file