Skip to content

Commit 84d105e

Browse files
committed
refactor(links): add poco Links class
1 parent ba67d24 commit 84d105e

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
7878

7979
contextEntity.Relationships.ForEach(r => {
8080
var relationshipData = new RelationshipData {
81-
Links = new Dictionary<string, string> {
82-
{"self", linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName)},
83-
{"related", linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName)}
81+
Links = new Links {
82+
Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName),
83+
Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName)
8484
}
8585
};
8686
data.Relationships.Add(r.RelationshipName.Dasherize(), relationshipData);

src/JsonApiDotNetCore/Models/Links.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Newtonsoft.Json;
2+
3+
namespace JsonApiDotNetCore.Models
4+
{
5+
public class Links
6+
{
7+
[JsonProperty("self")]
8+
public string Self { get; set; }
9+
10+
[JsonProperty("related")]
11+
public string Related { get; set; }
12+
}
13+
}

src/JsonApiDotNetCore/Models/RelationshipData.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
45

56
namespace JsonApiDotNetCore.Models
67
{
78
public class RelationshipData
89
{
910
[JsonProperty("links")]
10-
public Dictionary<string, string> Links { get; set; }
11+
public Links Links { get; set; }
1112

1213
[JsonProperty("data")]
1314
public object ExposedData {
@@ -18,7 +19,12 @@ public object ExposedData {
1819
}
1920
set {
2021
if(value is IEnumerable)
21-
ManyData = (List<Dictionary<string, string>>)value;
22+
if(value is JObject)
23+
SingleData = ((JObject)value).ToObject<Dictionary<string, string>>();
24+
else if(value is JArray)
25+
ManyData = ((JArray)value).ToObject<List<Dictionary<string, string>>>();
26+
else
27+
ManyData = (List<Dictionary<string, string>>)value;
2228
else
2329
SingleData = (Dictionary<string, string>)value;
2430
}

src/JsonApiDotNetCore/Serialization/JsonApiSerializer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ private static string _serializeDocuments(object entity, IJsonApiContext jsonApi
2020
var documentBuilder = new DocumentBuilder(jsonApiContext);
2121
var entities = entity as IEnumerable<IIdentifiable>;
2222
var documents = documentBuilder.Build(entities);
23-
return JsonConvert.SerializeObject(documents);
23+
return _serialize(documents);
2424
}
2525

2626
private static string _serializeDocument(object entity, IJsonApiContext jsonApiContext)
2727
{
2828
var documentBuilder = new DocumentBuilder(jsonApiContext);
2929
var identifiableEntity = entity as IIdentifiable;
3030
var document = documentBuilder.Build(identifiableEntity);
31-
return JsonConvert.SerializeObject(document, new JsonSerializerSettings {
31+
return _serialize(document);
32+
}
33+
34+
private static string _serialize(object obj)
35+
{
36+
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings {
3237
NullValueHandling = NullValueHandling.Ignore
3338
});
3439
}

src/JsonApiDotNetCoreExample/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [x] Sorting
1111
- [x] Fetching relationships
1212
- [x] Creating relationships
13+
- [x] Refactor relationships links to use an POCO that contains data and links objects
1314
- [ ] Include Entities
1415
- [ ] BadRequest should be 422 in POST
1516
- [ ] Add integration tests to example project
@@ -23,7 +24,7 @@
2324
- [ ] Dasherized route support
2425
- [ ] Sorting/Filtering should be handled by the repository so that it is not dependeny on EF ?
2526
- [ ] Rename ContextEntity ??
26-
- [x] Refactor relationships links to use an POCO that contains data and links objects
27+
2728

2829
## Usage
2930

0 commit comments

Comments
 (0)