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,58 @@
using Microsoft.AspNetCore.Mvc;
using ProximaContracts.Application.Contracts.Services;
using ProximaContracts.Domain.Contracts.DTOs.Request;
namespace ProximaContracts.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ContractsController(IContractService service) : ControllerBase
{
private readonly IContractService _service = service;
[HttpGet("GetAll")]
public async Task<IActionResult> GetContracts()
{
var response = await _service.GetContracts();
return response.Any() ? Ok(response) : NotFound();
}
[HttpGet("GetById")]
public async Task<IActionResult> GetContractsById([FromQuery] ContractByIdRequestDto dto)
{
var response = await _service.GetContractById(dto);
return response != null ? Ok(response) : NotFound();
}
[HttpPost("CreateContract")]
public async Task<IActionResult> CreateContract([FromBody] CreateContractRequestDto dto)
{
var result = await _service.CreateContract(dto);
if(result.IsCreated)
{
return Ok(result);
}
else
{
return BadRequest(result);
}
}
[HttpPut("UpdateContract")]
public async Task<IActionResult> UpdateContract([FromBody] UpdateContractRequestDto dto)
{
var result = await _service.UpdateContract(dto);
if (result.IsUpdated)
{
return Ok(result);
}
else
{
return BadRequest(result);
}
}
}
}

View File

@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Mvc;
using ProximaContracts.Application.Rates.Services;
namespace ProximaContracts.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RatesController(IRateService service) : ControllerBase
{
private readonly IRateService _service = service;
[HttpGet("GetAllRates")]
public async Task<IActionResult> GetAllRates()
{
return Ok(await _service.GetRates());
}
}
}

View File

@@ -0,0 +1,69 @@
using ProximaContracts.Shared.Exceptions.Repositories.Contract;
using ProximaContracts.Shared.Exceptions.Repositories.Rates;
using System.Diagnostics.Contracts;
using System.Net;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace ProximaContracts.API.Middleware
{
public sealed class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
public ExceptionHandlingMiddleware(
RequestDelegate next,
ILogger<ExceptionHandlingMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
_logger.LogError(ex, "Unhandled exception");
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext ctx, Exception ex)
{
(HttpStatusCode code, string title) mapping = ex switch
{
GetContractByIdException => (HttpStatusCode.InternalServerError, "Error getting contract by Id."),
GetContractsException => (HttpStatusCode.InternalServerError, "Error getting all contracts."),
CreateContractException => (HttpStatusCode.InternalServerError, "Error creating contract."),
CreateContractRateNotFoundException => (HttpStatusCode.BadRequest, "Error creating contract =>Rate not found."),
UpdateContractException => (HttpStatusCode.InternalServerError, "Error updating contract."),
CheckIfRateIdExistsException =>(HttpStatusCode.InternalServerError, "Error checking rates."),
GetAllRatesException => (HttpStatusCode.InternalServerError, "Error getting all rates."),
GetAllRates404Exception =>(HttpStatusCode.NotFound, "No Rates found."),
_ => (HttpStatusCode.InternalServerError, $"Internal server error.")
};
var problem = new
{
type = $"https://httpstatuses.com/{(int)mapping.code}",
title = mapping.title,
status = (int)mapping.code,
detail = ex.Message,
instance = ctx.Request.Path
};
string json = JsonSerializer.Serialize(problem);
ctx.Response.ContentType = "application/problem+json";
ctx.Response.StatusCode = (int)mapping.code;
return ctx.Response.WriteAsync(json);
}
}
}

View File

@@ -0,0 +1,51 @@
using ProximaContracts.API.Middleware;
using ProximaContracts.Application;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy("Loopback", p =>
p.SetIsOriginAllowed(o => new Uri(o).IsLoopback)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
options.AddPolicy("ProdDomains", p =>
p.WithOrigins(builder.Configuration
.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>())
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
builder.Services.AddApplicationDependencies();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseMiddleware<ExceptionHandlingMiddleware>();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors(app.Environment.IsDevelopment() ? "Loopback" : "ProdDomains");
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:27614",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5009",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProximaContracts.Application\ProximaContracts.Application.csproj" />
<ProjectReference Include="..\ProximaContracts.Shared\ProximaContracts.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,2 @@
@ProximaContracts.API_HostAddress = http://localhost:5009

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,17 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"PostgreSQL": "Host=localhost;Port=5432;Database=db.ProximaContracts;Username=proxima_user;Password=Proxima_Password"
},
"Cors": {
"AllowedOrigins": [
"https://quediaempiezo.asarmientotest.es"
]
},
"AllowedHosts": "*"
}

View File

@@ -0,0 +1,56 @@
using AutoMapper;
using ProximaContracts.Application.Rates.Services;
using ProximaContracts.Domain.Contracts.DTOs.Request;
using ProximaContracts.Domain.Contracts.DTOs.Response;
using ProximaContracts.Infrastructure.Rpositories.Contracts;
using ProximaContracts.Shared.Exceptions.Repositories.Contract;
namespace ProximaContracts.Application.Contracts.Services
{
public class ContractService(IContractRepository repository, IMapper mapper, IRateService rateService) : IContractService
{
private readonly IContractRepository _repository = repository;
private readonly IMapper _mapper = mapper;
private readonly IRateService _rateService = rateService;
public async Task<ContractByIdResponseDto> GetContractById(ContractByIdRequestDto dto)
{
var result = await _repository.GetContractById(dto);
return _mapper.Map< ContractByIdResponseDto>(result);
}
public async Task<IEnumerable<GetContractsResponseDto>> GetContracts()
{
var result = await _repository.GetContracts();
return _mapper.Map<List<GetContractsResponseDto>>(result);
}
public async Task<CreateContractResponseDto> CreateContract(CreateContractRequestDto dto)
{
var rateExists = await _rateService.CheckIfExists(dto.RateId);
if (!rateExists)
{
throw new CreateContractRateNotFoundException($"No rate found with id: {dto.RateId}, Contract can't be created.");
}
var result = await _repository.CreateContract(dto);
return new CreateContractResponseDto()
{
IsCreated = result.HasValue ? true : false,
Message = result.HasValue ? "Created" : "Error creating contract",
NewContractId = result.HasValue ? result.Value : 0
};
}
public async Task<UpdateContractResponseDto> UpdateContract(UpdateContractRequestDto dto)
{
var result = await _repository.UpdateContract(dto);
return new UpdateContractResponseDto()
{
IsUpdated = result.HasValue ? true : false,
Message = result.HasValue ? "Updated" : "Error updating contract",
ContractId = result.HasValue ? result.Value : 0
};
}
}
}

View File

@@ -0,0 +1,14 @@
using ProximaContracts.Domain.Contracts.DTOs.Request;
using ProximaContracts.Domain.Contracts.DTOs.Response;
namespace ProximaContracts.Application.Contracts.Services
{
public interface IContractService
{
Task<ContractByIdResponseDto> GetContractById(ContractByIdRequestDto dto);
Task<IEnumerable<GetContractsResponseDto>> GetContracts();
Task<CreateContractResponseDto> CreateContract(CreateContractRequestDto dto);
Task<UpdateContractResponseDto> UpdateContract(UpdateContractRequestDto dto);
}
}

View File

@@ -0,0 +1,37 @@
using Microsoft.Extensions.DependencyInjection;
using ProximaContracts.Application.Contracts.Services;
using ProximaContracts.Application.Rates.Services;
using ProximaContracts.Domain.Contracts.Mappings;
using ProximaContracts.Infrastructure.Rpositories.Contracts;
using ProximaContracts.Infrastructure.Rpositories.Rates;
namespace ProximaContracts.Application
{
public static class IoCConfiguration
{
public static IServiceCollection AddApplicationDependencies(this IServiceCollection services)
{
AddServices(services);
AddRepositories(services);
AddAutommaperProfiles(services);
return services;
}
private static void AddServices(IServiceCollection services)
{
services.AddScoped<IContractService, ContractService>();
services.AddScoped<IRateService, RateService>();
}
private static void AddRepositories(IServiceCollection services)
{
services.AddScoped<IContractRepository, ContractRepository>();
services.AddScoped<IRateRepository, RateRepository>();
}
private static void AddAutommaperProfiles(IServiceCollection services)
{
services.AddAutoMapper(typeof(ContractProfile).Assembly);
services.AddAutoMapper(typeof(RateProfile).Assembly);
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProximaContracts.Domain\ProximaContracts.Domain.csproj" />
<ProjectReference Include="..\ProximaContracts.Infrastructure\ProximaContracts.Infrastructure.csproj" />
<ProjectReference Include="..\ProximaContracts.Shared\ProximaContracts.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,10 @@
using ProximaContracts.Domain.Rates.DTOs.Responses;
namespace ProximaContracts.Application.Rates.Services
{
public interface IRateService
{
Task<bool> CheckIfExists(int id);
Task<IEnumerable<GetAllRatesDto>> GetRates();
}
}

View File

@@ -0,0 +1,29 @@
using AutoMapper;
using ProximaContracts.Domain.Rates.DTOs.Responses;
using ProximaContracts.Infrastructure.Rpositories.Rates;
using ProximaContracts.Shared.Exceptions.Repositories.Rates;
namespace ProximaContracts.Application.Rates.Services
{
public class RateService(IRateRepository repository, IMapper mapper) : IRateService
{
private readonly IRateRepository _repository = repository;
private readonly IMapper _mapper = mapper;
public async Task<bool> CheckIfExists(int id)
{
return await _repository.CheckIfExists(id);
}
public async Task<IEnumerable<GetAllRatesDto>> GetRates()
{
var response = await _repository.GetRates();
if (!response.Any())
{
throw new GetAllRates404Exception("No Rates found");
}
return _mapper.Map<List<GetAllRatesDto>>(response);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Domain.Contracts.DTOs.Request
{
public class ContractByIdRequestDto
{
public int Id { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations;
namespace ProximaContracts.Domain.Contracts.DTOs.Request
{
public class CreateContractRequestDto
{
[Required]
[MaxLength(20)]
public string ContractorIdNumber { get; set; } = null!;
[Required]
[MaxLength(50)]
public string ContractorName { get; set; } = null!;
[Required]
[MaxLength(100)]
public string ContractorSurname { get; set; } = null!;
[Required]
[Range(1, int.MaxValue, ErrorMessage = "RateId must be greater than 0.")]
public int RateId { get; set; }
}
}

View File

@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace ProximaContracts.Domain.Contracts.DTOs.Request
{
public class UpdateContractRequestDto
{
[Required]
public int ContractId { get; set; }
[Required]
public int RateId { get; set; }
public string? ContractorIdNumber { get; set; }
public string? ContractorName { get; set; }
public string? ContractorSurname { get; set; }
public DateTime? ContractInitDate { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace ProximaContracts.Domain.Contracts.DTOs.Response
{
public class ContractByIdResponseDto
{
public int Id { get; set; }
public string ContractorIdNumber { get; set; }
public string ContractorName { get; set; }
public string ContractorSurname { get; set; }
public DateTime ContractInitDate { get; set; }
public int RateId { get; set; }
public string RateName { get; set; }
public decimal RatePrice { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace ProximaContracts.Domain.Contracts.DTOs.Response
{
public class CreateContractResponseDto
{
public bool IsCreated { get; set; }
public int NewContractId { get; set; }
public string Message { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
namespace ProximaContracts.Domain.Contracts.DTOs.Response
{
public class GetContractsResponseDto
{
public int Id { get; set; }
public string ContractorName { get; set; }
public string ContractorSurname { get; set; }
public DateTime ContractInitDate { get; set; }
public string RateName { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace ProximaContracts.Domain.Contracts.DTOs.Response
{
public class UpdateContractResponseDto
{
public bool IsUpdated { get; set; }
public int ContractId { get; set; }
public string Message { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace ProximaContracts.Domain.Contracts.Entities
{
public class ContractByIdEntity
{
public int Id { get; set; }
public string ContractorIdNumber { get; set; }
public string ContractorName { get; set; }
public string ContractorSurname { get; set; }
public DateTime ContractInitDate { get; set; }
public int RateId { get; set; }
public string RateName { get; set; }
public decimal RatePrice { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
namespace ProximaContracts.Domain.Contracts.Entities
{
public class GetContractsEntity
{
public int Id { get; set; }
public string ContractorName { get; set; }
public string ContractorSurname { get; set; }
public DateTime ContractInitDate { get; set; }
public string RateName { get; set; }
}
}

View File

@@ -0,0 +1,40 @@
using AutoMapper;
using Npgsql;
using ProximaContracts.Domain.Contracts.DTOs.Response;
using ProximaContracts.Domain.Contracts.Entities;
namespace ProximaContracts.Domain.Contracts.Mappings
{
public class ContractProfile : Profile
{
public ContractProfile()
{
#region Contract By ID
CreateMap<NpgsqlDataReader, ContractByIdEntity>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.GetInt32(s.GetOrdinal("Id"))))
.ForMember(d => d.ContractorIdNumber, o => o.MapFrom(s => s.GetString(s.GetOrdinal("ContractorIdNumber"))))
.ForMember(d => d.ContractorName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("ContractorName"))))
.ForMember(d => d.ContractorSurname, o => o.MapFrom(s => s.GetString(s.GetOrdinal("ContractorSurname"))))
.ForMember(d => d.ContractInitDate, o => o.MapFrom(s => s.GetDateTime(s.GetOrdinal("ContractInitDate"))))
.ForMember(d => d.RateId, o => o.MapFrom(s => s.GetInt32(s.GetOrdinal("RateId"))))
.ForMember(d => d.RateName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("RateName"))))
.ForMember(d => d.RatePrice, o => o.MapFrom(s => s.GetFieldValue<decimal>(s.GetOrdinal("RatePrice"))))
;
CreateMap<ContractByIdEntity, ContractByIdResponseDto>();
#endregion ContractByID
#region Contracts All
CreateMap<NpgsqlDataReader, GetContractsEntity>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.GetInt32(s.GetOrdinal("Id"))))
.ForMember(d => d.ContractorName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("ContractorName"))))
.ForMember(d => d.ContractorSurname, o => o.MapFrom(s => s.GetString(s.GetOrdinal("ContractorSurname"))))
.ForMember(d => d.ContractInitDate, o => o.MapFrom(s => s.GetDateTime(s.GetOrdinal("ContractInitDate"))))
.ForMember(d => d.RateName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("RateName"))))
;
CreateMap<GetContractsEntity, GetContractsResponseDto>();
#endregion Contracts All
}
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="Npgsql" Version="8.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProximaContracts.Shared\ProximaContracts.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
using System.Data.SqlTypes;
namespace ProximaContracts.Domain.Rates.DTOs.Responses
{
public class GetAllRatesDto
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System.Data.SqlTypes;
namespace ProximaContracts.Domain.Rates.Entities
{
public class RateEntity
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}

View File

@@ -0,0 +1,23 @@
using AutoMapper;
using Npgsql;
using ProximaContracts.Domain.Rates.DTOs.Responses;
using ProximaContracts.Domain.Rates.Entities;
namespace ProximaContracts.Domain.Contracts.Mappings
{
public class RateProfile : Profile
{
public RateProfile()
{
#region Rates All
CreateMap<NpgsqlDataReader, RateEntity>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.GetInt32(s.GetOrdinal("Id"))))
.ForMember(d => d.Name, o => o.MapFrom(s => s.GetString(s.GetOrdinal("Name"))))
.ForMember(d => d.Price, o => o.MapFrom(r => r.GetDecimal(r.GetOrdinal("Price"))))
;
CreateMap<RateEntity, GetAllRatesDto>();
#endregion Rates All
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.6" />
<PackageReference Include="Npgsql" Version="8.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProximaContracts.Domain\ProximaContracts.Domain.csproj" />
<ProjectReference Include="..\ProximaContracts.Shared\ProximaContracts.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -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)
""";
}

View File

@@ -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();
}
}
}
}

View File

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

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();
}
}
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Contract
{
public sealed class CreateContractException : Exception
{
public CreateContractException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Contract
{
public sealed class CreateContractRateNotFoundException : Exception
{
public CreateContractRateNotFoundException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Contract
{
public sealed class GetContractByIdException : Exception
{
public GetContractByIdException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,8 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Contract
{
public sealed class GetContractsException : Exception
{
public GetContractsException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Contract
{
public sealed class UpdateContractException : Exception
{
public UpdateContractException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Rates
{
public sealed class CheckIfRateIdExistsException : Exception
{
public CheckIfRateIdExistsException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Rates
{
public sealed class GetAllRates404Exception : Exception
{
public GetAllRates404Exception(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,7 @@
namespace ProximaContracts.Shared.Exceptions.Repositories.Rates
{
public sealed class GetAllRatesException : Exception
{
public GetAllRatesException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,49 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36127.28 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaContracts.API", "ProximaContracts.API\ProximaContracts.API.csproj", "{2FCFBFDD-95E3-4D27-8DB5-37B59321EF11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaContracts.Application", "ProximaContracts.Application\ProximaContracts.Application.csproj", "{0B573051-EAD0-48D4-ABDF-A4E0A6247EFF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaContracts.Domain", "ProximaContracts.Domain\ProximaContracts.Domain.csproj", "{67CA7F33-0D6F-46A0-A7C9-58929E153C2A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaContracts.Infrastructure", "ProximaContracts.Infrastructure\ProximaContracts.Infrastructure.csproj", "{4F8BB408-0111-4DCF-B9CD-EC1D9D7B3EE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaContracts.Shared", "ProximaContracts.Shared\ProximaContracts.Shared.csproj", "{726F71DA-CADE-2203-4F7D-F88480F80337}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2FCFBFDD-95E3-4D27-8DB5-37B59321EF11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FCFBFDD-95E3-4D27-8DB5-37B59321EF11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FCFBFDD-95E3-4D27-8DB5-37B59321EF11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FCFBFDD-95E3-4D27-8DB5-37B59321EF11}.Release|Any CPU.Build.0 = Release|Any CPU
{0B573051-EAD0-48D4-ABDF-A4E0A6247EFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B573051-EAD0-48D4-ABDF-A4E0A6247EFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B573051-EAD0-48D4-ABDF-A4E0A6247EFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B573051-EAD0-48D4-ABDF-A4E0A6247EFF}.Release|Any CPU.Build.0 = Release|Any CPU
{67CA7F33-0D6F-46A0-A7C9-58929E153C2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67CA7F33-0D6F-46A0-A7C9-58929E153C2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67CA7F33-0D6F-46A0-A7C9-58929E153C2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67CA7F33-0D6F-46A0-A7C9-58929E153C2A}.Release|Any CPU.Build.0 = Release|Any CPU
{4F8BB408-0111-4DCF-B9CD-EC1D9D7B3EE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F8BB408-0111-4DCF-B9CD-EC1D9D7B3EE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F8BB408-0111-4DCF-B9CD-EC1D9D7B3EE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F8BB408-0111-4DCF-B9CD-EC1D9D7B3EE4}.Release|Any CPU.Build.0 = Release|Any CPU
{726F71DA-CADE-2203-4F7D-F88480F80337}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{726F71DA-CADE-2203-4F7D-F88480F80337}.Debug|Any CPU.Build.0 = Debug|Any CPU
{726F71DA-CADE-2203-4F7D-F88480F80337}.Release|Any CPU.ActiveCfg = Release|Any CPU
{726F71DA-CADE-2203-4F7D-F88480F80337}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F9B629D6-70F9-456E-802E-0A7C534C00A8}
EndGlobalSection
EndGlobal