prueba tecnica

This commit is contained in:
Alejandro
2025-06-15 18:29:25 +02:00
parent 9758ee0bc6
commit d97e55a83f
127 changed files with 6488 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
using ProximaContracts.Domain.Rates.Entities;
namespace ProximaContracts.Infrastructure.Rpositories.Rates
{
public interface IRateRepository
{
Task<bool> CheckIfExists(int Id);
Task<IEnumerable<RateEntity>> GetRates();
}
}

View File

@@ -0,0 +1,5 @@
public static class RateFS
{
public static string CheckIfExists = "SELECT public.check_rate_exists(@p_id);";
public static string GetRates = "SELECT * FROM public.get_rates();";
}

View File

@@ -0,0 +1,75 @@
using AutoMapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Npgsql;
using NpgsqlTypes;
using ProximaContracts.Domain.Rates.Entities;
using ProximaContracts.Shared.Exceptions.Repositories.Rates;
namespace ProximaContracts.Infrastructure.Rpositories.Rates
{
public class RateRepository(IConfiguration config, ILogger<RateRepository> logger, IMapper mapper) : IRateRepository
{
private readonly string _connStr = config.GetConnectionString("PostgreSQL")!;
private readonly ILogger<RateRepository> _log = logger;
private readonly IMapper _mapper = mapper;
public async Task<bool> CheckIfExists(int Id)
{
await using var conn = new NpgsqlConnection(_connStr);
await conn.OpenAsync();
try
{
await using var cmd = new NpgsqlCommand(RateFS.CheckIfExists, conn);
cmd.Parameters.AddWithValue("p_id", NpgsqlDbType.Integer, Id);
var result = await cmd.ExecuteScalarAsync();
return Convert.ToInt32(result) == 1;
}
catch (Exception ex)
{
_log.LogError(ex.Message);
throw new CheckIfRateIdExistsException(ex.Message);
}
finally
{
await conn.CloseAsync();
}
}
public async Task<IEnumerable<RateEntity>> GetRates()
{
await using var conn = new NpgsqlConnection(_connStr);
await conn.OpenAsync();
try
{
await using var cmd = new NpgsqlCommand(RateFS.GetRates, conn);
await using NpgsqlDataReader reader = await cmd.ExecuteReaderAsync();
var results = new List<RateEntity>();
while (await reader.ReadAsync())
{
results.Add(_mapper.Map<RateEntity>(reader));
}
await conn.CloseAsync();
return results;
}
catch (Exception ex)
{
_log.LogError(ex.Message);
throw new GetAllRatesException(ex.Message);
}
finally
{
await conn.CloseAsync();
}
}
}
}