Skip to content

Commit 648962c

Browse files
committed
feat(*): implement fetching relationships
1 parent 7961841 commit 648962c

File tree

7 files changed

+23
-7
lines changed

7 files changed

+23
-7
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
5252
var data = new DocumentData
5353
{
5454
Type = contextEntity.EntityName,
55-
Id = entity.Id.ToString(),
56-
Attributes = new Dictionary<string, object>(),
57-
Relationships = new Dictionary<string, Dictionary<string, object>>()
55+
Id = entity.Id.ToString()
5856
};
5957

58+
if(_jsonApiContext.IsRelationshipData)
59+
return data;
60+
61+
data.Attributes = new Dictionary<string, object>();
62+
data.Relationships = new Dictionary<string, Dictionary<string, object>>();
63+
6064
contextEntity.Attributes.ForEach(attr =>
6165
{
6266
data.Attributes.Add(attr.PublicAttributeName, attr.GetValue(entity));

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public virtual async Task<IActionResult> GetAsync(TId id)
7070
return Ok(entity);
7171
}
7272

73+
[HttpGet("{id}/relationships/{relationshipName}")]
74+
public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string relationshipName)
75+
{
76+
_jsonApiContext.IsRelationshipData = true;
77+
78+
return await GetRelationshipAsync(id, relationshipName);
79+
}
80+
7381
[HttpGet("{id}/{relationshipName}")]
7482
public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName)
7583
{

src/JsonApiDotNetCore/Extensions/ServiceProviderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void AddJsonApiInternals<TContext>(this IServiceCollection service
3131
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
3232

3333
services.AddSingleton<IContextGraph>(contextGraph);
34-
services.AddSingleton<IJsonApiContext,JsonApiContext>();
34+
services.AddScoped<IJsonApiContext,JsonApiContext>();
3535
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
3636

3737
services.AddScoped<JsonApiRouteHandler>();

src/JsonApiDotNetCore/Serialization/JsonApiSerializer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ public static string Serialize(object entity, IJsonApiContext jsonApiContext)
1818
private static string _serializeDocuments(object entity, IJsonApiContext jsonApiContext)
1919
{
2020
var documentBuilder = new DocumentBuilder(jsonApiContext);
21-
2221
var entities = entity as IEnumerable<IIdentifiable>;
2322
var documents = documentBuilder.Build(entities);
2423
return JsonConvert.SerializeObject(documents);
2524
}
2625

27-
private static string _serializeDocument(object entity, IJsonApiContext jsonApiContext)
26+
private static string _serializeDocument(object entity, IJsonApiContext jsonApiContext)
2827
{
2928
var documentBuilder = new DocumentBuilder(jsonApiContext);
3029
var identifiableEntity = entity as IIdentifiable;
3130
var document = documentBuilder.Build(identifiableEntity);
32-
return JsonConvert.SerializeObject(document);
31+
return JsonConvert.SerializeObject(document, new JsonSerializerSettings {
32+
NullValueHandling = NullValueHandling.Ignore
33+
});
3334
}
3435
}
3536
}

src/JsonApiDotNetCore/Services/IJsonApiContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public interface IJsonApiContext
1010
ContextEntity RequestEntity { get; set; }
1111
string BasePath { get; set; }
1212
IQueryCollection Query { get; set; }
13+
bool IsRelationshipData { get; set; }
1314
}
1415
}

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public JsonApiContext(
1919
public ContextEntity RequestEntity { get; set; }
2020
public string BasePath { get; set; }
2121
public IQueryCollection Query { get; set; }
22+
public bool IsRelationshipData { get; set; }
2223

2324
public IJsonApiContext ApplyContext<T>()
2425
{

src/JsonApiDotNetCoreExample/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [x] Add support for authorization/context-scoping middleware
99
- [x] Filtering
1010
- [x] Sorting
11+
- [x] Fetching relationships
1112
- [ ] Creating relationships
1213
- [ ] Include Entities
1314
- [ ] BadRequest should be 422 in POST

0 commit comments

Comments
 (0)