prueba tecnica
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();";
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user