Skip to content

Commit ca46835

Browse files
committed
Create public accessor for ApiResponseBuilder
1 parent 341b531 commit ca46835

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/Initium/Response/ApiResponseBuilder.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
namespace Initium.Response;
77

88
/// <summary>
9-
/// Provides a fluent builder for creating <see cref="ApiResponse"/> objects.
9+
/// Provides a fluent builder for creating <see cref="Response.ApiResponse"/> objects.
1010
/// </summary>
1111
[SuppressMessage("ReSharper", "UnusedMember.Global")]
1212
internal class ApiResponseBuilder
1313
{
14-
private readonly ApiResponse _apiResponse;
14+
public ApiResponse ApiResponse { get; set; }
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="ApiResponseBuilder"/> class.
1818
/// </summary>
19-
private ApiResponseBuilder() => _apiResponse = new ApiResponse();
19+
private ApiResponseBuilder() => ApiResponse = new ApiResponse();
2020

2121
/// <summary>
2222
/// Creates a new instance of the <see cref="ApiResponseBuilder"/>.
@@ -31,7 +31,7 @@ internal class ApiResponseBuilder
3131
/// <returns>A new instance of the <see cref="ApiResponseBuilder"/> initialized with context details.</returns>
3232
public static ApiResponseBuilder CreateFromContext(HttpContext context) => new()
3333
{
34-
_apiResponse =
34+
ApiResponse =
3535
{
3636
RequestDetails = new RequestDetails
3737
{
@@ -50,7 +50,7 @@ internal class ApiResponseBuilder
5050
/// <returns>The current instance of the <see cref="ApiResponseBuilder"/>.</returns>
5151
public ApiResponseBuilder WithMessage(string message)
5252
{
53-
_apiResponse.Message = message;
53+
ApiResponse.Message = message;
5454
return this;
5555
}
5656

@@ -61,7 +61,13 @@ public ApiResponseBuilder WithMessage(string message)
6161
/// <returns>The current instance of the <see cref="ApiResponseBuilder"/>.</returns>
6262
public ApiResponseBuilder WithStatusCode(HttpStatusCode statusCode)
6363
{
64-
_apiResponse.StatusCode = (int)statusCode;
64+
ApiResponse.StatusCode = (int)statusCode;
65+
return this;
66+
}
67+
68+
public ApiResponseBuilder WithData(object data)
69+
{
70+
ApiResponse.Data = data;
6571
return this;
6672
}
6773

@@ -72,7 +78,7 @@ public ApiResponseBuilder WithStatusCode(HttpStatusCode statusCode)
7278
/// <returns>The current instance of the <see cref="ApiResponseBuilder"/>.</returns>
7379
public ApiResponseBuilder WithErrors(params ApiError[] errors)
7480
{
75-
_apiResponse.Errors = errors.ToList();
81+
ApiResponse.Errors = errors.ToList();
7682
return this;
7783
}
7884

@@ -88,8 +94,8 @@ public ApiResponseBuilder WithCustomHeader(string headerName, string headerValue
8894
if (string.IsNullOrWhiteSpace(headerName))
8995
throw new ArgumentException("Header name cannot be null or empty.", nameof(headerName));
9096

91-
_apiResponse.CustomHeaders ??= new Dictionary<string, string>();
92-
_apiResponse.CustomHeaders[headerName] = headerValue;
97+
ApiResponse.CustomHeaders ??= new Dictionary<string, string>();
98+
ApiResponse.CustomHeaders[headerName] = headerValue;
9399
return this;
94100
}
95101

@@ -101,25 +107,25 @@ public ApiResponseBuilder WithCustomHeader(string headerName, string headerValue
101107
/// <exception cref="ArgumentException">Thrown when the <paramref name="headers"/> dictionary is null or empty.</exception>
102108
public ApiResponseBuilder WithCustomHeaders(Dictionary<string, string> headers)
103109
{
104-
_apiResponse.CustomHeaders ??= new Dictionary<string, string>();
110+
ApiResponse.CustomHeaders ??= new Dictionary<string, string>();
105111
foreach (var header in headers)
106-
_apiResponse.CustomHeaders[header.Key] = header.Value;
112+
ApiResponse.CustomHeaders[header.Key] = header.Value;
107113

108114
return this;
109115
}
110116

111117
/// <summary>
112-
/// Builds and returns the constructed <see cref="ApiResponse"/>.
118+
/// Builds and returns the constructed <see cref="Response.ApiResponse"/>.
113119
/// </summary>
114-
/// <returns>The constructed <see cref="ApiResponse"/>.</returns>
115-
public ApiResponse Build() => _apiResponse;
120+
/// <returns>The constructed <see cref="Response.ApiResponse"/>.</returns>
121+
public ApiResponse Build() => ApiResponse;
116122

117123
/// <summary>
118-
/// Builds and returns the constructed <see cref="ApiResponse"/> as a <see cref="JsonResult"/>.
124+
/// Builds and returns the constructed <see cref="Response.ApiResponse"/> as a <see cref="JsonResult"/>.
119125
/// </summary>
120-
/// <returns>A <see cref="JsonResult"/> containing the constructed <see cref="ApiResponse"/>.</returns>
121-
public JsonResult BuildAsJsonResult() => new(_apiResponse)
126+
/// <returns>A <see cref="JsonResult"/> containing the constructed <see cref="Response.ApiResponse"/>.</returns>
127+
public JsonResult BuildAsJsonResult() => new(ApiResponse)
122128
{
123-
StatusCode = _apiResponse.StatusCode
129+
StatusCode = ApiResponse.StatusCode
124130
};
125131
}

0 commit comments

Comments
 (0)