From 70d773988f4e3480d14bc489c0e65caa72d2039b Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Mon, 7 Mar 2022 17:41:08 +0100 Subject: [PATCH] feat: Add CreateContentResultWithProblemDetails with overload "object? value" --- docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md | 4 +++ docs/CodeDoc/Atc.Rest/IndexExtended.md | 1 + src/Atc.Rest/Results/ResultFactory.cs | 31 ++++++++++++++++++- .../Results/ResultFactoryTests.cs | 19 ++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md b/docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md index f75a3f70..7c996f5d 100644 --- a/docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md +++ b/docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md @@ -82,6 +82,10 @@ ResultBase. >```csharp >ContentResult CreateContentResultWithProblemDetails(HttpStatusCode statusCode, string message, string contentType = application/json) >``` +#### CreateContentResultWithProblemDetails +>```csharp +>ContentResult CreateContentResultWithProblemDetails(HttpStatusCode statusCode, object value, string contentType = application/json) +>``` #### CreateContentResultWithValidationProblemDetails >```csharp >ContentResult CreateContentResultWithValidationProblemDetails(HttpStatusCode statusCode, string message, string contentType = application/json) diff --git a/docs/CodeDoc/Atc.Rest/IndexExtended.md b/docs/CodeDoc/Atc.Rest/IndexExtended.md index 951f1f2a..77c4c3a7 100644 --- a/docs/CodeDoc/Atc.Rest/IndexExtended.md +++ b/docs/CodeDoc/Atc.Rest/IndexExtended.md @@ -107,6 +107,7 @@ - [ResultFactory](Atc.Rest.Results.md#resultfactory) - Static Methods - CreateContentResult(HttpStatusCode statusCode, string message, string contentType = application/json) + - CreateContentResultWithProblemDetails(HttpStatusCode statusCode, object value, string contentType = application/json) - CreateContentResultWithProblemDetails(HttpStatusCode statusCode, string message, string contentType = application/json) - CreateContentResultWithValidationProblemDetails(HttpStatusCode statusCode, Dictionary<string, string[]> errors, string message, string contentType = application/json) - CreateContentResultWithValidationProblemDetails(HttpStatusCode statusCode, string message, string contentType = application/json) diff --git a/src/Atc.Rest/Results/ResultFactory.cs b/src/Atc.Rest/Results/ResultFactory.cs index 9d0b3088..5cec1d79 100644 --- a/src/Atc.Rest/Results/ResultFactory.cs +++ b/src/Atc.Rest/Results/ResultFactory.cs @@ -2,7 +2,9 @@ namespace Atc.Rest.Results; public static class ResultFactory { - public static ProblemDetails CreateProblemDetails(HttpStatusCode statusCode, string? message) + public static ProblemDetails CreateProblemDetails( + HttpStatusCode statusCode, + string? message) => new ProblemDetails { Status = (int)statusCode, @@ -30,6 +32,33 @@ public static ContentResult CreateContentResultWithProblemDetails( Content = JsonSerializer.Serialize(CreateProblemDetails(statusCode, message)), }; + public static ContentResult CreateContentResultWithProblemDetails( + HttpStatusCode statusCode, + object? value, + string contentType = MediaTypeNames.Application.Json) + { + var result = new ContentResult + { + ContentType = contentType, + StatusCode = (int)statusCode, + }; + + if (value is null) + { + return result; + } + + var message = Helpers.SimpleTypeHelper.IsSimpleType(value.GetType().BeautifyTypeName()) + ? value.ToString() + : JsonSerializer.Serialize(value); + + var problemDetails = CreateProblemDetails(statusCode, message); + + result.Content = JsonSerializer.Serialize(problemDetails); + + return result; + } + public static ContentResult CreateContentResultWithValidationProblemDetails( HttpStatusCode statusCode, string? message, diff --git a/test/Atc.Rest.Tests/Results/ResultFactoryTests.cs b/test/Atc.Rest.Tests/Results/ResultFactoryTests.cs index 01150112..cd77248e 100644 --- a/test/Atc.Rest.Tests/Results/ResultFactoryTests.cs +++ b/test/Atc.Rest.Tests/Results/ResultFactoryTests.cs @@ -57,6 +57,25 @@ public void CreateContentResultWithProblemDetails_Message(HttpStatusCode statusC } } + [Theory] + [InlineData(HttpStatusCode.Conflict, null)] + [InlineData(HttpStatusCode.Conflict, "Hallo World")] + public void CreateContentResultWithProblemDetails_Value(HttpStatusCode statusCode, object? value) + { + // Act + var actual = ResultFactory.CreateContentResultWithProblemDetails(statusCode, value); + + // Assert + Assert.NotNull(actual); + Assert.Equal((int)statusCode, actual.StatusCode); + if (value is not null) + { + Assert.NotNull(actual.Content); + Assert.Equal(MediaTypeNames.Application.Json, actual.ContentType); + Assert.Equal($"{{\"status\":{(int)statusCode},\"detail\":\"{value}\"}}", actual.Content); + } + } + [Theory] [InlineData(HttpStatusCode.OK, null)] [InlineData(HttpStatusCode.OK, "Hallo World")]