Skip to content

Commit

Permalink
feat: Add CreateContentResultWithProblemDetails with overload "object…
Browse files Browse the repository at this point in the history
…? value"
  • Loading branch information
davidkallesen committed Mar 7, 2022
1 parent 2c5b778 commit 70d7739
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CodeDoc/Atc.Rest/Atc.Rest.Results.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/CodeDoc/Atc.Rest/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 30 additions & 1 deletion src/Atc.Rest/Results/ResultFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/Atc.Rest.Tests/Results/ResultFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 70d7739

Please sign in to comment.