prueba tecnica
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
public static class ContractFS
|
||||
{
|
||||
public static string GetContractById = "SELECT * FROM public.get_contract_by_id(@p_contract_id);";
|
||||
public static string GetContracts = "SELECT * FROM public.get_contracts();";
|
||||
public static string CreateContract = "SELECT * FROM public.create_contract(@p_contractor_id_number, @p_contractor_name, @p_contractor_surname, @p_contract_init_date, @p_rate_id);";
|
||||
public static string UpdateContract = """
|
||||
SELECT * FROM public.update_contract(@p_contract_id, @p_rate_id, @p_contractor_id_number,
|
||||
@p_contractor_name, @p_contractor_surname, @p_contract_init_date)
|
||||
""";
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using NpgsqlTypes;
|
||||
using ProximaContracts.Domain.Contracts.DTOs.Request;
|
||||
using ProximaContracts.Domain.Contracts.Entities;
|
||||
using ProximaContracts.Shared.Exceptions.Repositories.Contract;
|
||||
|
||||
namespace ProximaContracts.Infrastructure.Rpositories.Contracts
|
||||
{
|
||||
public class ContractRepository(IConfiguration config, ILogger<ContractRepository> logger, IMapper mapper) : IContractRepository
|
||||
{
|
||||
private readonly string _connStr = config.GetConnectionString("PostgreSQL")!;
|
||||
private readonly ILogger<ContractRepository> _log = logger;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
|
||||
public async Task<ContractByIdEntity?> GetContractById(ContractByIdRequestDto dto)
|
||||
{
|
||||
await using var conn = new NpgsqlConnection(_connStr);
|
||||
await conn.OpenAsync();
|
||||
try
|
||||
{
|
||||
await using var cmd = new NpgsqlCommand(ContractFS.GetContractById, conn);
|
||||
cmd.Parameters.AddWithValue("p_contract_id", NpgsqlDbType.Integer, dto.Id);
|
||||
|
||||
await using NpgsqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||
if (!await reader.ReadAsync()) return null;
|
||||
|
||||
return _mapper.Map<ContractByIdEntity>(reader);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex.Message);
|
||||
throw new GetContractByIdException(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await conn.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GetContractsEntity>> GetContracts()
|
||||
{
|
||||
await using var conn = new NpgsqlConnection(_connStr);
|
||||
await conn.OpenAsync();
|
||||
try
|
||||
{
|
||||
await using var cmd = new NpgsqlCommand(ContractFS.GetContracts, conn);
|
||||
|
||||
await using NpgsqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||
var results = new List<GetContractsEntity>();
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
results.Add(_mapper.Map<GetContractsEntity>(reader));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex.Message);
|
||||
throw new GetContractsException(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await conn.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int?> CreateContract(CreateContractRequestDto dto)
|
||||
{
|
||||
await using var conn = new NpgsqlConnection(_connStr);
|
||||
await conn.OpenAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await using var cmd = new NpgsqlCommand(ContractFS.CreateContract, conn);
|
||||
cmd.Parameters.AddWithValue("p_contractor_id_number", NpgsqlDbType.Varchar, dto.ContractorIdNumber);
|
||||
cmd.Parameters.AddWithValue("p_contractor_name", NpgsqlDbType.Varchar, dto.ContractorName);
|
||||
cmd.Parameters.AddWithValue("p_contractor_surname", NpgsqlDbType.Varchar, dto.ContractorSurname);
|
||||
cmd.Parameters.AddWithValue("p_contract_init_date", NpgsqlDbType.Timestamp, DateTime.Now);
|
||||
cmd.Parameters.AddWithValue("p_rate_id", NpgsqlDbType.Integer, dto.RateId);
|
||||
|
||||
await using NpgsqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||
if (!await reader.ReadAsync()) return null;
|
||||
|
||||
var result = reader.GetInt32(reader.GetOrdinal("create_contract"));
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex.Message);
|
||||
throw new CreateContractException(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await conn.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int?> UpdateContract(UpdateContractRequestDto dto)
|
||||
{
|
||||
await using var conn = new NpgsqlConnection(_connStr);
|
||||
await conn.OpenAsync();
|
||||
try
|
||||
{
|
||||
await using var cmd = new NpgsqlCommand(ContractFS.UpdateContract, conn);
|
||||
cmd.Parameters.AddWithValue("p_contract_id", NpgsqlDbType.Integer, dto.ContractId);
|
||||
cmd.Parameters.AddWithValue("p_rate_id", NpgsqlDbType.Integer, dto.RateId);
|
||||
|
||||
cmd.Parameters.AddWithValue("p_contractor_id_number", NpgsqlDbType.Varchar, dto.ContractorIdNumber != null ? dto.ContractorIdNumber : DBNull.Value);
|
||||
|
||||
cmd.Parameters.AddWithValue("p_contractor_name", NpgsqlDbType.Varchar, dto.ContractorName != null ? dto.ContractorName : DBNull.Value);
|
||||
cmd.Parameters.AddWithValue("p_contractor_surname", NpgsqlDbType.Varchar, dto.ContractorSurname != null ? dto.ContractorSurname : DBNull.Value);
|
||||
cmd.Parameters.AddWithValue("p_contract_init_date", NpgsqlDbType.Timestamp, dto.ContractInitDate != null ? dto.ContractInitDate : DBNull.Value);
|
||||
|
||||
await using NpgsqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||
if (!await reader.ReadAsync()) return null;
|
||||
|
||||
var result = reader.GetInt32(reader.GetOrdinal("update_contract"));
|
||||
return result;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
_log.LogError(ex.Message);
|
||||
throw new UpdateContractException(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await conn.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using ProximaContracts.Domain.Contracts.DTOs.Request;
|
||||
using ProximaContracts.Domain.Contracts.Entities;
|
||||
|
||||
namespace ProximaContracts.Infrastructure.Rpositories.Contracts
|
||||
{
|
||||
public interface IContractRepository
|
||||
{
|
||||
Task<ContractByIdEntity?> GetContractById(ContractByIdRequestDto dto);
|
||||
Task<IEnumerable<GetContractsEntity>> GetContracts();
|
||||
Task<int?> CreateContract(CreateContractRequestDto dto);
|
||||
Task<int?> UpdateContract(UpdateContractRequestDto dto);
|
||||
}
|
||||
}
|
||||
@@ -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