Add DocumentTypes in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 3m17s

This commit is contained in:
Leandro Hernan Rojas 2025-04-03 21:43:09 -03:00
parent e616318047
commit 8ed080db2e
5 changed files with 147 additions and 0 deletions

View File

@ -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<IEnumerable<EDocumentType>> GetAllAsync();
Task<EDocumentType?> GetByNameAsync(string name);
}
}

View File

@ -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<IEnumerable<EDocumentType>> 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<EDocumentType?> 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);
}
}
}
}

View File

@ -0,0 +1,10 @@
using Domain.Entities;
namespace Models.Interfaces
{
public interface IPhSDocumentTypeRepository
{
Task<IEnumerable<EDocumentType>> GetAllAsync();
Task<EDocumentType?> GetByNameAsync(string name);
}
}

View File

@ -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<IEnumerable<EDocumentType>> GetAllAsync()
{
var documentTypes = await _context.PhSDocumentTypes.ToListAsync();
return documentTypes.Select(at => MapEntity<PhSDocumentType, EDocumentType>(at));
}
public async Task<EDocumentType?> GetByNameAsync(string name)
{
var documentType = await _context.PhSDocumentTypes.FirstOrDefaultAsync(a => a.Name.Contains(name));
return documentType != null ? MapEntity<PhSDocumentType, EDocumentType>(documentType) : null;
}
#endregion
#region Métodos Auxiliares
private static TDestination MapEntity<TSource, TDestination>(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
}
}

View File

@ -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<IActionResult> GetAll()
{
try
{
var result = await _documentTypeService.GetAllAsync();
return Ok(result);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("GetByName/{name}")]
public async Task<IActionResult> 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);
}
}
}