prueba tecnica
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
51
backend/ProximaContracts/ProximaContracts.API/Program.cs
Normal file
51
backend/ProximaContracts/ProximaContracts.API/Program.cs
Normal 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();
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,2 @@
|
||||
@ProximaContracts.API_HostAddress = http://localhost:5009
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user