Skip to content

Commit 4afa439

Browse files
author
Bart Koelman
committed
Small tweaks
1 parent 3c05438 commit 4afa439

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/JsonApiDotNetCore/Serialization/Building/LinkBuilder.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public class LinkBuilder : ILinkBuilder
2323
private const string PageSizeParameterName = "page[size]";
2424
private const string PageNumberParameterName = "page[number]";
2525

26-
private static readonly string GetPrimaryControllerAction = WithoutAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetAsync));
27-
private static readonly string GetSecondaryControllerAction = WithoutAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetSecondaryAsync));
28-
private static readonly string GetRelationshipControllerAction = WithoutAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetRelationshipAsync));
26+
private static readonly string GetPrimaryControllerActionName = NoAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetAsync));
27+
private static readonly string GetSecondaryControllerActionName = NoAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetSecondaryAsync));
28+
private static readonly string GetRelationshipControllerActionName = NoAsyncSuffix(nameof(BaseJsonApiController<Identifiable>.GetRelationshipAsync));
2929

3030
private readonly IJsonApiOptions _options;
3131
private readonly IJsonApiRequest _request;
@@ -55,7 +55,7 @@ public LinkBuilder(IJsonApiOptions options, IJsonApiRequest request, IPagination
5555
_controllerResourceMapping = controllerResourceMapping;
5656
}
5757

58-
private static string WithoutAsyncSuffix(string actionName)
58+
private static string NoAsyncSuffix(string actionName)
5959
{
6060
return actionName.EndsWith("Async", StringComparison.Ordinal) ? actionName[..^"Async".Length] : actionName;
6161
}
@@ -185,6 +185,19 @@ private IList<PaginationElementQueryStringValueExpression> ParsePageSizeExpressi
185185
}
186186

187187
private string GetLinkForPagination(int pageOffset, string pageSizeValue)
188+
{
189+
string queryStringValue = GetQueryStringInPaginationLink(pageOffset, pageSizeValue);
190+
191+
var builder = new UriBuilder(_httpContextAccessor.HttpContext.Request.GetEncodedUrl())
192+
{
193+
Query = queryStringValue
194+
};
195+
196+
UriComponents components = _options.UseRelativeLinks ? UriComponents.PathAndQuery : UriComponents.AbsoluteUri;
197+
return builder.Uri.GetComponents(components, UriFormat.SafeUnescaped);
198+
}
199+
200+
private string GetQueryStringInPaginationLink(int pageOffset, string pageSizeValue)
188201
{
189202
IDictionary<string, string> parameters =
190203
_httpContextAccessor.HttpContext.Request.Query.ToDictionary(pair => pair.Key, pair => pair.Value.ToString());
@@ -208,15 +221,7 @@ private string GetLinkForPagination(int pageOffset, string pageSizeValue)
208221
}
209222

210223
string queryStringValue = QueryString.Create(parameters).Value;
211-
queryStringValue = DecodeSpecialCharacters(queryStringValue);
212-
213-
var builder = new UriBuilder(_httpContextAccessor.HttpContext.Request.GetEncodedUrl())
214-
{
215-
Query = queryStringValue
216-
};
217-
218-
UriComponents components = _options.UseRelativeLinks ? UriComponents.PathAndQuery : UriComponents.AbsoluteUri;
219-
return builder.Uri.GetComponents(components, UriFormat.SafeUnescaped);
224+
return DecodeSpecialCharacters(queryStringValue);
220225
}
221226

222227
private static string DecodeSpecialCharacters(string uri)
@@ -260,7 +265,7 @@ private string GetLinkForResourceSelf(ResourceContext resourceContext, string re
260265
string controllerName = _controllerResourceMapping.GetControllerNameForResourceType(resourceContext.ResourceType);
261266
IDictionary<string, object> routeValues = GetRouteValues(resourceId, null);
262267

263-
return RenderLinkForAction(controllerName, GetPrimaryControllerAction, routeValues);
268+
return RenderLinkForAction(controllerName, GetPrimaryControllerActionName, routeValues);
264269
}
265270

266271
/// <inheritdoc />
@@ -290,15 +295,15 @@ private string GetLinkForRelationshipSelf(string primaryId, RelationshipAttribut
290295
string controllerName = _controllerResourceMapping.GetControllerNameForResourceType(relationship.LeftType);
291296
IDictionary<string, object> routeValues = GetRouteValues(primaryId, relationship.PublicName);
292297

293-
return RenderLinkForAction(controllerName, GetRelationshipControllerAction, routeValues);
298+
return RenderLinkForAction(controllerName, GetRelationshipControllerActionName, routeValues);
294299
}
295300

296301
private string GetLinkForRelationshipRelated(string primaryId, RelationshipAttribute relationship)
297302
{
298303
string controllerName = _controllerResourceMapping.GetControllerNameForResourceType(relationship.LeftType);
299304
IDictionary<string, object> routeValues = GetRouteValues(primaryId, relationship.PublicName);
300305

301-
return RenderLinkForAction(controllerName, GetSecondaryControllerAction, routeValues);
306+
return RenderLinkForAction(controllerName, GetSecondaryControllerActionName, routeValues);
302307
}
303308

304309
private IDictionary<string, object> GetRouteValues(string primaryId, string relationshipName)

0 commit comments

Comments
 (0)