Skip to content

Commit 0f99794

Browse files
committed
feat(fetch): get by id operation
1 parent ee5b34a commit 0f99794

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ public static void AddJsonApiInternals(
100100
services.AddScoped<IDbContextResolver, DbContextResolver>();
101101
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
102102
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
103-
103+
104104
services.AddScoped(typeof(ICreateService<>), typeof(EntityResourceService<>));
105105
services.AddScoped(typeof(ICreateService<,>), typeof(EntityResourceService<,>));
106106

107107
services.AddScoped(typeof(IGetAllService<>), typeof(EntityResourceService<>));
108108
services.AddScoped(typeof(IGetAllService<,>), typeof(EntityResourceService<,>));
109109

110+
services.AddScoped(typeof(IGetByIdService<>), typeof(EntityResourceService<>));
111+
services.AddScoped(typeof(IGetByIdService<,>), typeof(EntityResourceService<,>));
112+
110113
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
111114
services.AddScoped(typeof(IResourceService<,>), typeof(EntityResourceService<,>));
112115
services.AddSingleton<JsonApiOptions>(jsonApiOptions);

src/JsonApiDotNetCore/Services/Operations/Processors/GetOpProcessor.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using JsonApiDotNetCore.Models;
66
using JsonApiDotNetCore.Models.Operations;
77
using JsonApiDotNetCore.Serialization;
8-
using JsonApiDotNetCore.Services;
98

109
namespace JsonApiDotNetCore.Services.Operations.Processors
1110
{
@@ -21,32 +20,36 @@ public class GetOpProcessor<T> : GetOpProcessor<T, int>
2120
where T : class, IIdentifiable<int>
2221
{
2322
public GetOpProcessor(
24-
IGetAllService<T, int> service,
23+
IGetAllService<T, int> getAll,
24+
IGetByIdService<T, int> getById,
2525
IJsonApiDeSerializer deSerializer,
2626
IDocumentBuilder documentBuilder,
2727
IContextGraph contextGraph,
2828
IJsonApiContext jsonApiContext
29-
) : base(service, deSerializer, documentBuilder, contextGraph, jsonApiContext)
29+
) : base(getAll, getById, deSerializer, documentBuilder, contextGraph, jsonApiContext)
3030
{ }
3131
}
3232

3333
public class GetOpProcessor<T, TId> : IGetOpProcessor<T, TId>
3434
where T : class, IIdentifiable<TId>
3535
{
36-
private readonly IGetAllService<T, TId> _service;
36+
private readonly IGetAllService<T, TId> _getAll;
37+
private readonly IGetByIdService<T, TId> _getById;
3738
private readonly IJsonApiDeSerializer _deSerializer;
3839
private readonly IDocumentBuilder _documentBuilder;
3940
private readonly IContextGraph _contextGraph;
4041
private readonly IJsonApiContext _jsonApiContext;
4142

4243
public GetOpProcessor(
43-
IGetAllService<T, TId> service,
44+
IGetAllService<T, TId> getAll,
45+
IGetByIdService<T, TId> getById,
4446
IJsonApiDeSerializer deSerializer,
4547
IDocumentBuilder documentBuilder,
4648
IContextGraph contextGraph,
4749
IJsonApiContext jsonApiContext)
4850
{
49-
_service = service;
51+
_getAll = getAll;
52+
_getById = getById;
5053
_deSerializer = deSerializer;
5154
_documentBuilder = documentBuilder;
5255
_contextGraph = contextGraph;
@@ -55,25 +58,43 @@ public GetOpProcessor(
5558

5659
public async Task<Operation> ProcessAsync(Operation operation)
5760
{
58-
var result = await _service.GetAsync();
59-
6061
var operationResult = new Operation
6162
{
62-
Op = OperationCode.add
63+
Op = OperationCode.get
6364
};
6465

66+
operationResult.Data = string.IsNullOrWhiteSpace(operation.Ref.Id?.ToString())
67+
? await GetAllAsync(operation)
68+
: await GetByIdAsync(operation);
69+
70+
return operationResult;
71+
}
72+
73+
private async Task<object> GetAllAsync(Operation operation)
74+
{
75+
var result = await _getAll.GetAsync();
76+
6577
var operations = new List<DocumentData>();
6678
foreach (var resource in result)
6779
{
6880
var doc = _documentBuilder.GetData(
69-
_contextGraph.GetContextEntity(operation.GetResourceTypeName()),
70-
resource);
81+
_contextGraph.GetContextEntity(operation.GetResourceTypeName()),
82+
resource);
7183
operations.Add(doc);
7284
}
7385

74-
operationResult.Data = operations;
86+
return operations;
87+
}
7588

76-
return operationResult;
89+
private async Task<object> GetByIdAsync(Operation operation)
90+
{
91+
var id = TypeHelper.ConvertType<TId>(operation.Ref.Id);
92+
var result = await _getById.GetAsync(id);
93+
var doc = _documentBuilder.GetData(
94+
_contextGraph.GetContextEntity(operation.GetResourceTypeName()),
95+
result);
96+
97+
return doc;
7798
}
7899
}
79100
}

test/OperationsExampleTests/Get/GetTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Net;
34
using System.Threading.Tasks;
45
using JsonApiDotNetCore.Models.Operations;
@@ -43,7 +44,35 @@ public async Task Can_Get_Articles()
4344
Assert.NotNull(result.data);
4445
Assert.Equal(HttpStatusCode.OK, result.response.StatusCode);
4546
Assert.Equal(1, result.data.Operations.Count);
46-
Assert.Equal(articles.Count, result.data.Operations[0].DataList.Count);
47+
Assert.Equal(articles.Count, result.data.Operations.Single().DataList.Count);
48+
}
49+
50+
[Fact]
51+
public async Task Can_Get_Article_By_Id()
52+
{
53+
// arrange
54+
var context = _fixture.GetService<AppDbContext>();
55+
var article = await context.Articles.LastAsync();
56+
57+
var content = new
58+
{
59+
operations = new[] {
60+
new Dictionary<string, object> {
61+
{ "op", "get"},
62+
{ "ref", new { type = "articles", id = article.StringId } }
63+
}
64+
}
65+
};
66+
67+
// act
68+
var result = await _fixture.PatchAsync<OperationsDocument>("api/bulk", content);
69+
70+
// assert
71+
Assert.NotNull(result.response);
72+
Assert.NotNull(result.data);
73+
Assert.Equal(HttpStatusCode.OK, result.response.StatusCode);
74+
Assert.Equal(1, result.data.Operations.Count);
75+
Assert.Equal(article.Id.ToString(), result.data.Operations.Single().DataObject.Id);
4776
}
4877
}
4978
}

0 commit comments

Comments
 (0)