Update API eliminacion de Generic
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 3m3s

This commit is contained in:
Leandro Hernan Rojas 2025-04-03 19:50:15 -03:00
parent 945da30636
commit 4e15bcfcc1
4 changed files with 18 additions and 50 deletions

View File

@ -1,8 +1,10 @@
using Domain.Entities; using Domain.Entities;
namespace Models.Interfaces namespace Models.Interfaces
{ {
public interface IPhSAccountTypeRepository : IGenericRepository<EAccountType> public interface IPhSAccountTypeRepository
{ {
Task<IEnumerable<EAccountType>> GetAllAsync();
Task<EAccountType?> GetByNameAsync(string name); Task<EAccountType?> GetByNameAsync(string name);
} }
} }

View File

@ -1,44 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Models.Interfaces;
using Models.Models;
namespace Models.Repositories
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly PhronCareOperationsHubContext _context;
private readonly DbSet<T> _dbSet;
public GenericRepository(PhronCareOperationsHubContext context)
{
_context = context;
_dbSet = _context.Set<T>();
}
public async Task<T?> GetByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public async Task<IEnumerable<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public async Task AddAsync(T entity)
{
await _dbSet.AddAsync(entity);
}
public void Update(T entity)
{
_dbSet.Update(entity);
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
}
}
}

View File

@ -5,12 +5,13 @@ using Models.Models;
namespace Models.Repositories namespace Models.Repositories
{ {
public class PhSAccountTypeRepository(PhronCareOperationsHubContext context) : IPhSAccountTypeRepository
public class PhSAccountTypeRepository(PhronCareOperationsHubContext context) : GenericRepository<EAccountType>(context), IPhSAccountTypeRepository
{ {
#region Declaraciones y Constructor
private readonly PhronCareOperationsHubContext _context = context; private readonly PhronCareOperationsHubContext _context = context;
#endregion
public new async Task<IEnumerable<EAccountType>> GetAllAsync() #region Metodos de clase
public async Task<IEnumerable<EAccountType>> GetAllAsync()
{ {
var accountTypes = await _context.PhSAccountTypes.ToListAsync(); var accountTypes = await _context.PhSAccountTypes.ToListAsync();
return accountTypes.Select(at => MapEntity<PhSAccountType, EAccountType>(at)); return accountTypes.Select(at => MapEntity<PhSAccountType, EAccountType>(at));
@ -21,6 +22,7 @@ namespace Models.Repositories
var accountType = await _context.PhSAccountTypes.FirstOrDefaultAsync(a => a.Name == name); var accountType = await _context.PhSAccountTypes.FirstOrDefaultAsync(a => a.Name == name);
return accountType != null ? MapEntity<PhSAccountType, EAccountType>(accountType) : null; return accountType != null ? MapEntity<PhSAccountType, EAccountType>(accountType) : null;
} }
#endregion
#region Métodos Auxiliares #region Métodos Auxiliares
private static TDestination MapEntity<TSource, TDestination>(TSource source) where TDestination : new() private static TDestination MapEntity<TSource, TDestination>(TSource source) where TDestination : new()
{ {

View File

@ -10,7 +10,6 @@ namespace phronCare.API.Controllers.Sales
{ {
private readonly IAccountTypeDom _accountTypeService; private readonly IAccountTypeDom _accountTypeService;
// Constructor que acepta ITicketDom como parámetro
public AccountTypeController(IAccountTypeDom accountTypeService) public AccountTypeController(IAccountTypeDom accountTypeService)
{ {
_accountTypeService = accountTypeService ?? throw new ArgumentNullException(nameof(accountTypeService)); _accountTypeService = accountTypeService ?? throw new ArgumentNullException(nameof(accountTypeService));
@ -28,5 +27,14 @@ namespace phronCare.API.Controllers.Sales
return BadRequest(ex.Message); return BadRequest(ex.Message);
} }
} }
[HttpGet("GetByName/{name}")]
public async Task<IActionResult> GetByName(string name)
{
var result = await _accountTypeService.GetByNameAsync(name);
if (result == null)
return NotFound($"No se encontró un tipo de cuenta con el nombre '{name}'.");
return Ok(result);
}
} }
} }