Skip to content

Commit

Permalink
Configured ProblemDetals follow RFC 9457
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dębowski committed Dec 4, 2024
1 parent cd2f2e0 commit 6bbd86e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 51 deletions.
4 changes: 4 additions & 0 deletions src/hiPower.Core/DataCenterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)
public async Task<ErrorOr<DataCenter>> GetAsync (string id)
{
var result = await unit.DataCenterRepository.GetAsync(x => x.Id.Equals(id.ToUpperInvariant()), null);
if (result is null)
{
return Error.NotFound();
}
return result.Adapt<DataCenter> ();
}

Expand Down
16 changes: 12 additions & 4 deletions src/hiPower.Core/ServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)
public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync (string dataCenterId)
{
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(dataCenterId.ToUpper()), null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
.ToErrorOr();
if (result.Any ())
{
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
.ToErrorOr ();
}
return Error.NotFound();
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync ()
Expand All @@ -48,8 +52,12 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)
public async Task<ErrorOr<IEnumerable<HintItem>>> GetAvailableDataCentersAsync ()
{
var result = await unit.DataCenterRepository.GetAll(null, null, null);
return result.Adapt<IEnumerable<HintItem>>()
.ToErrorOr();
if (result.Any())
{
return result.Adapt<IEnumerable<HintItem>> ()
.ToErrorOr ();
}
return Error.NotFound ();
}

public async Task<ErrorOr<Dto.Manager.Server>> UpdateAsync (string id, Dto.Manager.Server server)
Expand Down
30 changes: 15 additions & 15 deletions src/hiPower.WebApi/Controllers/DataCentersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public async Task<IActionResult> Get ([FromRoute] string id)

if (result.IsError)
{
return BadRequest(new ProblemDetails () { Status = StatusCodes.Status400BadRequest });
if (result.FirstError.Type == ErrorType.NotFound)
{
return NotFound();
}
return BadRequest();
}
Thread.Sleep (1000);
return Ok (new ApiResult<DataCenter> (true, result.Value));
Expand All @@ -54,10 +58,7 @@ public async Task<IActionResult> GetServers ([FromRoute] string id)

if (result.IsError)
{
return BadRequest (new ProblemDetails
{
Status = StatusCodes.Status400BadRequest
});
return BadRequest ();
}

var response = new ApiResult<IEnumerable<Server>>(true, result.Value);
Expand All @@ -74,10 +75,7 @@ public async Task<IActionResult> Create ([FromBody] DataCenter location)

if (result.IsError)
{
return BadRequest (new ProblemDetails
{
Status = StatusCodes.Status400BadRequest
});
return BadRequest ();
}
var temp = result.Value;
var response = new ApiResult<DataCenter>(true, temp);
Expand All @@ -95,10 +93,11 @@ public async Task<IActionResult> Update ([FromRoute] string id, [FromBody] DataC
var result = await locationService.UpdateAsync(id, location);
if (result.IsError)
{
return BadRequest (new ProblemDetails
if (result.FirstError.Type == ErrorType.NotFound)
{
Status = StatusCodes.Status400BadRequest
});
return NotFound ();
}
return BadRequest ();
}

var response = new ApiResult<DataCenter>(true, result.Value);
Expand All @@ -115,10 +114,11 @@ public async Task<IActionResult> Delete ([FromRoute] string id)
var result = await locationService.DeleteAsync (id);
if (result.IsError)
{
return BadRequest (new ProblemDetails ()
if (result.FirstError.Type == ErrorType.NotFound)
{
Status = StatusCodes.Status400BadRequest
});
return NotFound () ;
}
return BadRequest ();
}
var response = new ApiResult<bool>(true, result.Value);
return Ok (response);
Expand Down
52 changes: 24 additions & 28 deletions src/hiPower.WebApi/Controllers/ServersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using hiPower.Abstracts;
using ErrorOr;
using hiPower.Abstracts;
using Microsoft.AspNetCore.Mvc;

namespace hiPower.WebApi.Controllers
Expand All @@ -13,14 +14,15 @@ public class ServersController(IServerService serverService) : ControllerBase
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<IEnumerable<Server>>))]
public async Task<IActionResult> GetAll ([FromRoute] string dataCenterId)
{
var response = await serverService.GetAllAsync (dataCenterId);
var result = await serverService.GetAllAsync (dataCenterId);
bool notFound = result.IsError && result.FirstError.Type == ErrorType.NotFound;

if (response.IsError)
if (notFound)
{
return BadRequest ();
return NotFound ();
}

return Ok (new ApiResult<IEnumerable<Server>>(!response.IsError, response.Value));
return Ok (new ApiResult<IEnumerable<Server>>(!result.IsError, result.Value));
}


Expand All @@ -31,31 +33,27 @@ public async Task<IActionResult> GetAll ([FromRoute] string dataCenterId)
[ProducesResponseType (StatusCodes.Status404NotFound, Type = typeof (ProblemDetails))]
public async Task<IActionResult> Get ([FromRoute] string id)
{
bool canProcced = Guid.TryParse(id, out Guid correctValue);
var result = await serverService.GetAsync (id);

if (canProcced)
{

var result = await serverService.GetAsync (correctValue.ToString().ToUpper());

if (result.IsError)
{
return NotFound ();
}
bool notFound = result.IsError && result.FirstError.Type == ErrorType.NotFound;

return Ok (new ApiResult<Server> (!result.IsError, result.Value));
if (notFound)
{
return NotFound ();
}
return BadRequest ();

return Ok (new ApiResult<Server> (!result.IsError, result.Value));
}

[HttpGet("datacenters")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<IEnumerable<HintItem>>))]
public async Task<IActionResult> HintDataCenters()
{
var result = await serverService.GetAvailableDataCentersAsync ();
if (result.IsError)
bool notFound = result.IsError && result.FirstError.Type == ErrorType.NotFound;
if (notFound)
{
return BadRequest ();
return NotFound();
}
return Ok (new ApiResult<IEnumerable<HintItem>>(true, result.Value));
}
Expand All @@ -64,13 +62,10 @@ public async Task<IActionResult> HintDataCenters()
[VallidatModel]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ApiResult<Server>))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
public async Task<IActionResult> Create ([FromBody] Server server)
{
var result = await serverService.CreateAsync(server);
if (result.IsError)
{
return BadRequest ();
}
return Created (string.Empty, new ApiResult<Server> (!result.IsError, result.Value));
}

Expand All @@ -82,9 +77,10 @@ public async Task<IActionResult> Create ([FromBody] Server server)
public async Task<IActionResult> Update ([FromRoute] string id, [FromBody] Server server)
{
var result = await serverService.UpdateAsync(id, server);
if (result.IsError)
bool notFound = result.IsError && result.FirstError.Type == ErrorType.NotFound;
if (notFound)
{
return BadRequest ();
return NotFound ();
}
return Ok (new ApiResult<Server> (!result.IsError, result.Value));
}
Expand All @@ -96,10 +92,10 @@ public async Task<IActionResult> Update ([FromRoute] string id, [FromBody] Serve
public async Task<IActionResult> Deleted ([FromQuery] string id)
{
var result = await serverService.DeleteAsync(id);

if(result.IsError)
bool notFound = result.IsError && result.FirstError.Type == ErrorType.NotFound;
if (notFound)
{
return BadRequest ();
return NotFound ();
}
return Ok ();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json.Serialization;
using System.Diagnostics;
using System.Text.Json.Serialization;
using hiPower.WebApi.Middlewares;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Formatters;

namespace hiPower.WebApi.Extensions.DependencyInjection;
Expand Down Expand Up @@ -32,13 +34,15 @@ public static IServiceCollection ConfigureWebHostServices(this IServiceCollectio

public static IServiceCollection ConfigureProblemDetails(this IServiceCollection services)
{

services.AddProblemDetails (problem =>
{
problem.CustomizeProblemDetails = context =>
{
context.ProblemDetails.Title = "Error";
context.ProblemDetails.Instance = "hiPower Web Api";
context.ProblemDetails.Instance = $"{context.HttpContext.Request.Method} {context.HttpContext.Request.Path}";
context.ProblemDetails.Extensions.TryAdd("requestId", context.HttpContext.TraceIdentifier);

Activity? activity = context.HttpContext.Features.Get<IHttpActivityFeature?>()?.Activity;
context.ProblemDetails.Extensions.TryAdd ("traceId", activity?.Id);
};
});

Expand Down
1 change: 1 addition & 0 deletions src/hiPower.WebApi/hiPower.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<Using Include="ErrorOr"/>
<Using Include="System.Net.Mime" />
<Using Include="hiPower.Dto" />
<Using Include="hiPower.Dto.Manager" />
Expand Down

0 comments on commit 6bbd86e

Please sign in to comment.