diff --git a/src/hiPower.Core/DataCenterService.cs b/src/hiPower.Core/DataCenterService.cs index a10eb6e..c73cbc0 100644 --- a/src/hiPower.Core/DataCenterService.cs +++ b/src/hiPower.Core/DataCenterService.cs @@ -29,6 +29,10 @@ public async Task> DeleteAsync (string id) public async Task> 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 (); } diff --git a/src/hiPower.Core/ServerService.cs b/src/hiPower.Core/ServerService.cs index 8506442..34d7358 100644 --- a/src/hiPower.Core/ServerService.cs +++ b/src/hiPower.Core/ServerService.cs @@ -23,8 +23,12 @@ public async Task> DeleteAsync (string id) public async Task>> GetAllAsync (string dataCenterId) { var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(dataCenterId.ToUpper()), null, null); - return result.Adapt> () - .ToErrorOr(); + if (result.Any ()) + { + return result.Adapt> () + .ToErrorOr (); + } + return Error.NotFound(); } public async Task>> GetAllAsync () @@ -48,8 +52,12 @@ public async Task> DeleteAsync (string id) public async Task>> GetAvailableDataCentersAsync () { var result = await unit.DataCenterRepository.GetAll(null, null, null); - return result.Adapt>() - .ToErrorOr(); + if (result.Any()) + { + return result.Adapt> () + .ToErrorOr (); + } + return Error.NotFound (); } public async Task> UpdateAsync (string id, Dto.Manager.Server server) diff --git a/src/hiPower.WebApi/Controllers/DataCentersController.cs b/src/hiPower.WebApi/Controllers/DataCentersController.cs index be8884e..e97e3d7 100644 --- a/src/hiPower.WebApi/Controllers/DataCentersController.cs +++ b/src/hiPower.WebApi/Controllers/DataCentersController.cs @@ -37,7 +37,11 @@ public async Task 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 (true, result.Value)); @@ -54,10 +58,7 @@ public async Task GetServers ([FromRoute] string id) if (result.IsError) { - return BadRequest (new ProblemDetails - { - Status = StatusCodes.Status400BadRequest - }); + return BadRequest (); } var response = new ApiResult>(true, result.Value); @@ -74,10 +75,7 @@ public async Task Create ([FromBody] DataCenter location) if (result.IsError) { - return BadRequest (new ProblemDetails - { - Status = StatusCodes.Status400BadRequest - }); + return BadRequest (); } var temp = result.Value; var response = new ApiResult(true, temp); @@ -95,10 +93,11 @@ public async Task 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(true, result.Value); @@ -115,10 +114,11 @@ public async Task 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(true, result.Value); return Ok (response); diff --git a/src/hiPower.WebApi/Controllers/ServersController.cs b/src/hiPower.WebApi/Controllers/ServersController.cs index 52be7ff..6648e47 100644 --- a/src/hiPower.WebApi/Controllers/ServersController.cs +++ b/src/hiPower.WebApi/Controllers/ServersController.cs @@ -1,4 +1,5 @@ -using hiPower.Abstracts; +using ErrorOr; +using hiPower.Abstracts; using Microsoft.AspNetCore.Mvc; namespace hiPower.WebApi.Controllers @@ -13,14 +14,15 @@ public class ServersController(IServerService serverService) : ControllerBase [ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult>))] public async Task 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>(!response.IsError, response.Value)); + return Ok (new ApiResult>(!result.IsError, result.Value)); } @@ -31,21 +33,16 @@ public async Task GetAll ([FromRoute] string dataCenterId) [ProducesResponseType (StatusCodes.Status404NotFound, Type = typeof (ProblemDetails))] public async Task 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 (!result.IsError, result.Value)); + if (notFound) + { + return NotFound (); } - return BadRequest (); + + return Ok (new ApiResult (!result.IsError, result.Value)); } [HttpGet("datacenters")] @@ -53,9 +50,10 @@ public async Task Get ([FromRoute] string id) public async Task 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>(true, result.Value)); } @@ -64,13 +62,10 @@ public async Task HintDataCenters() [VallidatModel] [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ApiResult))] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProblemDetails))] + [ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))] public async Task Create ([FromBody] Server server) { var result = await serverService.CreateAsync(server); - if (result.IsError) - { - return BadRequest (); - } return Created (string.Empty, new ApiResult (!result.IsError, result.Value)); } @@ -82,9 +77,10 @@ public async Task Create ([FromBody] Server server) public async Task 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 (!result.IsError, result.Value)); } @@ -96,10 +92,10 @@ public async Task Update ([FromRoute] string id, [FromBody] Serve public async Task 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 (); } diff --git a/src/hiPower.WebApi/Extensions/DependencyInjection/WebHostServiceExtension.cs b/src/hiPower.WebApi/Extensions/DependencyInjection/WebHostServiceExtension.cs index ed1ddcf..f21f3bc 100644 --- a/src/hiPower.WebApi/Extensions/DependencyInjection/WebHostServiceExtension.cs +++ b/src/hiPower.WebApi/Extensions/DependencyInjection/WebHostServiceExtension.cs @@ -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; @@ -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()?.Activity; + context.ProblemDetails.Extensions.TryAdd ("traceId", activity?.Id); }; }); diff --git a/src/hiPower.WebApi/hiPower.WebApi.csproj b/src/hiPower.WebApi/hiPower.WebApi.csproj index d4207ba..e083e43 100644 --- a/src/hiPower.WebApi/hiPower.WebApi.csproj +++ b/src/hiPower.WebApi/hiPower.WebApi.csproj @@ -12,6 +12,7 @@ +