forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultLinkConventions.cs
More file actions
102 lines (91 loc) · 4.69 KB
/
Copy pathDefaultLinkConventions.cs
File metadata and controls
102 lines (91 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using JSONAPI.Core;
namespace JSONAPI.Documents
{
/// <summary>
/// Implementation of ILinkConventions that adheres to JSON API recommendations for URL formatting.
/// </summary>
public class DefaultLinkConventions : ILinkConventions
{
public ILink GetRelationshipLink<TResource>(TResource relationshipOwner, IResourceTypeRegistry resourceTypeRegistry, ResourceTypeRelationship property, string baseUrl)
{
if (!property.SerializeRelationshipLink) return null;
var url = BuildRelationshipUrl(relationshipOwner, resourceTypeRegistry, property, baseUrl);
var metadata = GetMetadataForRelationshipLink(relationshipOwner, property);
return new Link(url, metadata);
}
public ILink GetRelatedResourceLink<TResource>(TResource relationshipOwner, IResourceTypeRegistry resourceTypeRegistry, ResourceTypeRelationship property, string baseUrl)
{
if (!property.SerializeRelatedResourceLink) return null;
var url = BuildRelatedResourceUrl(relationshipOwner, resourceTypeRegistry, property, baseUrl);
var metadata = GetMetadataForRelatedResourceLink(relationshipOwner, property);
return new Link(url, metadata);
}
private string GetSanitizedBaseUrl(string baseUrl)
{
while (baseUrl[baseUrl.Length - 1] == '/')
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
return baseUrl;
}
/// <summary>
/// Constructs a URL for the relationship belonging to the given resource
/// </summary>
/// <param name="relationshipOwner"></param>
/// <param name="resourceTypeRegistry"></param>
/// <param name="property"></param>
/// <param name="baseUrl"></param>
/// <returns></returns>
protected virtual string BuildRelationshipUrl(object relationshipOwner, IResourceTypeRegistry resourceTypeRegistry,
ResourceTypeRelationship property, string baseUrl)
{
var relationshipOwnerType = relationshipOwner.GetType();
var sanitizedBaseUrl = GetSanitizedBaseUrl(baseUrl);
var registration = resourceTypeRegistry.GetRegistrationForType(relationshipOwnerType);
var id = registration.GetIdForResource(relationshipOwner);
if (property.SelfLinkTemplate != null)
{
var replacedString = property.SelfLinkTemplate.Replace("{1}", id);
return String.Format("{0}/{1}", sanitizedBaseUrl, replacedString);
}
return String.Format("{0}/{1}/{2}/relationships/{3}", sanitizedBaseUrl, registration.ResourceTypeName, id, property.JsonKey);
}
/// <summary>
/// Gets a metadata object to serialize alongside the link URL for relationship links.
/// </summary>
/// <returns></returns>
protected virtual IMetadata GetMetadataForRelationshipLink<TResource>(TResource relationshipOwner, ResourceTypeRelationship property)
{
return null;
}
/// <summary>
/// Constructs a URL for the resource(s) on the other side of the given relationship, belonging to the given resource
/// </summary>
/// <param name="relationshipOwner"></param>
/// <param name="resourceTypeRegistry"></param>
/// <param name="property"></param>
/// <param name="baseUrl"></param>
/// <returns></returns>
protected virtual string BuildRelatedResourceUrl(object relationshipOwner, IResourceTypeRegistry resourceTypeRegistry,
ResourceTypeRelationship property, string baseUrl)
{
var relationshipOwnerType = relationshipOwner.GetType();
var sanitizedBaseUrl = GetSanitizedBaseUrl(baseUrl);
var registration = resourceTypeRegistry.GetRegistrationForType(relationshipOwnerType);
var id = registration.GetIdForResource(relationshipOwner);
if (property.RelatedResourceLinkTemplate != null)
{
var replacedString = property.RelatedResourceLinkTemplate.Replace("{1}", id);
return String.Format("{0}/{1}", sanitizedBaseUrl, replacedString);
}
return String.Format("{0}/{1}/{2}/{3}", sanitizedBaseUrl, registration.ResourceTypeName, id, property.JsonKey);
}
/// <summary>
/// Gets a metadata object to serialize alongside the link URL for related resource links.
/// </summary>
/// <returns></returns>
protected virtual IMetadata GetMetadataForRelatedResourceLink<TResource>(TResource relationshipOwner, ResourceTypeRelationship property)
{
return null;
}
}
}