From 8ed080db2e91966b9d485925369447ea39c40015 Mon Sep 17 00:00:00 2001 From: Leandro Hernan Rojas Date: Thu, 3 Apr 2025 21:43:09 -0300 Subject: [PATCH] Add DocumentTypes in API --- Core/Interfaces/IDocumentTypeDom.cs | 15 +++++++ Core/Services/DocumentTypeService.cs | 43 +++++++++++++++++++ .../Interfaces/IPhSDocumentTypeRepository.cs | 10 +++++ .../Repositories/PhSDocumentTypeRepository.cs | 39 +++++++++++++++++ .../Sales/DocumentTypeController.cs | 40 +++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 Core/Interfaces/IDocumentTypeDom.cs create mode 100644 Core/Services/DocumentTypeService.cs create mode 100644 Models/Interfaces/IPhSDocumentTypeRepository.cs create mode 100644 Models/Repositories/PhSDocumentTypeRepository.cs create mode 100644 phronCare.API/Controllers/Sales/DocumentTypeController.cs diff --git a/Core/Interfaces/IDocumentTypeDom.cs b/Core/Interfaces/IDocumentTypeDom.cs new file mode 100644 index 0000000..075df8d --- /dev/null +++ b/Core/Interfaces/IDocumentTypeDom.cs @@ -0,0 +1,15 @@ +using Domain.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Core.Interfaces +{ + public interface IDocumentTypeDom + { + Task> GetAllAsync(); + Task GetByNameAsync(string name); + } +} diff --git a/Core/Services/DocumentTypeService.cs b/Core/Services/DocumentTypeService.cs new file mode 100644 index 0000000..2b7c464 --- /dev/null +++ b/Core/Services/DocumentTypeService.cs @@ -0,0 +1,43 @@ +using Core.Interfaces; +using Domain.Entities; +using Models.Interfaces; +using System.Reflection; + +namespace Core.Services +{ + public class DocumentTypeService : IDocumentTypeDom + { + private readonly IPhSDocumentTypeRepository _repository; + + public DocumentTypeService(IPhSDocumentTypeRepository repository) + { + _repository = repository ?? throw new ArgumentNullException(nameof(repository)); + } + + public async Task> GetAllAsync() + { + try + { + return await _repository.GetAllAsync(); + } + catch (Exception ex) + { + var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + throw new Exception($"{methodName} Message: {ex.Message}", ex); + } + } + + public async Task GetByNameAsync(string name) + { + try + { + return await _repository.GetByNameAsync(name); + } + catch (Exception ex) + { + var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + throw new Exception($"{methodName} Message: {ex.Message}", ex); + } + } + } +} diff --git a/Models/Interfaces/IPhSDocumentTypeRepository.cs b/Models/Interfaces/IPhSDocumentTypeRepository.cs new file mode 100644 index 0000000..5073fe6 --- /dev/null +++ b/Models/Interfaces/IPhSDocumentTypeRepository.cs @@ -0,0 +1,10 @@ +using Domain.Entities; + +namespace Models.Interfaces +{ + public interface IPhSDocumentTypeRepository + { + Task> GetAllAsync(); + Task GetByNameAsync(string name); + } +} diff --git a/Models/Repositories/PhSDocumentTypeRepository.cs b/Models/Repositories/PhSDocumentTypeRepository.cs new file mode 100644 index 0000000..c4d5e29 --- /dev/null +++ b/Models/Repositories/PhSDocumentTypeRepository.cs @@ -0,0 +1,39 @@ +using Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Models.Interfaces; +using Models.Models; + +namespace Models.Repositories +{ + public class PhSDocumentTypeRepository (PhronCareOperationsHubContext context): IPhSDocumentTypeRepository + { + #region Declaraciones y Constructor + private readonly PhronCareOperationsHubContext _context = context; + #endregion + #region Metodos de clase + public async Task> GetAllAsync() + { + var documentTypes = await _context.PhSDocumentTypes.ToListAsync(); + return documentTypes.Select(at => MapEntity(at)); + } + + public async Task GetByNameAsync(string name) + { + var documentType = await _context.PhSDocumentTypes.FirstOrDefaultAsync(a => a.Name.Contains(name)); + return documentType != null ? MapEntity(documentType) : null; + } + #endregion + #region Métodos Auxiliares + private static TDestination MapEntity(TSource source) where TDestination : new() + { + var destination = new TDestination(); + foreach (var propertyInfo in typeof(TSource).GetProperties()) + { + var value = propertyInfo.GetValue(source); + typeof(TDestination).GetProperty(propertyInfo.Name)?.SetValue(destination, value); + } + return destination; + } + #endregion + } +} diff --git a/phronCare.API/Controllers/Sales/DocumentTypeController.cs b/phronCare.API/Controllers/Sales/DocumentTypeController.cs new file mode 100644 index 0000000..a427e28 --- /dev/null +++ b/phronCare.API/Controllers/Sales/DocumentTypeController.cs @@ -0,0 +1,40 @@ +using Core.Interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace phronCare.API.Controllers.Sales +{ + [Route("api/[controller]")] + [ApiController] + public class DocumentTypeController : ControllerBase + { + private readonly IDocumentTypeDom _documentTypeService; + + public DocumentTypeController(IDocumentTypeDom documentTypeService) + { + _documentTypeService = documentTypeService ?? throw new ArgumentNullException(nameof(documentTypeService)); + } + [HttpGet("GetAll")] + public async Task GetAll() + { + try + { + var result = await _documentTypeService.GetAllAsync(); + return Ok(result); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + [HttpGet("GetByName/{name}")] + public async Task GetByName(string name) + { + var result = await _documentTypeService.GetByNameAsync(name); + if (result == null) + return NotFound($"No se encontró un tipo de cuenta con el nombre '{name}'."); + + return Ok(result); + } + + } +}