5
5
using JsonApiDotNetCore . Models ;
6
6
using JsonApiDotNetCore . Models . Operations ;
7
7
using JsonApiDotNetCore . Serialization ;
8
- using JsonApiDotNetCore . Services ;
9
8
10
9
namespace JsonApiDotNetCore . Services . Operations . Processors
11
10
{
@@ -21,32 +20,36 @@ public class GetOpProcessor<T> : GetOpProcessor<T, int>
21
20
where T : class , IIdentifiable < int >
22
21
{
23
22
public GetOpProcessor (
24
- IGetAllService < T , int > service ,
23
+ IGetAllService < T , int > getAll ,
24
+ IGetByIdService < T , int > getById ,
25
25
IJsonApiDeSerializer deSerializer ,
26
26
IDocumentBuilder documentBuilder ,
27
27
IContextGraph contextGraph ,
28
28
IJsonApiContext jsonApiContext
29
- ) : base ( service , deSerializer , documentBuilder , contextGraph , jsonApiContext )
29
+ ) : base ( getAll , getById , deSerializer , documentBuilder , contextGraph , jsonApiContext )
30
30
{ }
31
31
}
32
32
33
33
public class GetOpProcessor < T , TId > : IGetOpProcessor < T , TId >
34
34
where T : class , IIdentifiable < TId >
35
35
{
36
- private readonly IGetAllService < T , TId > _service ;
36
+ private readonly IGetAllService < T , TId > _getAll ;
37
+ private readonly IGetByIdService < T , TId > _getById ;
37
38
private readonly IJsonApiDeSerializer _deSerializer ;
38
39
private readonly IDocumentBuilder _documentBuilder ;
39
40
private readonly IContextGraph _contextGraph ;
40
41
private readonly IJsonApiContext _jsonApiContext ;
41
42
42
43
public GetOpProcessor (
43
- IGetAllService < T , TId > service ,
44
+ IGetAllService < T , TId > getAll ,
45
+ IGetByIdService < T , TId > getById ,
44
46
IJsonApiDeSerializer deSerializer ,
45
47
IDocumentBuilder documentBuilder ,
46
48
IContextGraph contextGraph ,
47
49
IJsonApiContext jsonApiContext )
48
50
{
49
- _service = service ;
51
+ _getAll = getAll ;
52
+ _getById = getById ;
50
53
_deSerializer = deSerializer ;
51
54
_documentBuilder = documentBuilder ;
52
55
_contextGraph = contextGraph ;
@@ -55,25 +58,43 @@ public GetOpProcessor(
55
58
56
59
public async Task < Operation > ProcessAsync ( Operation operation )
57
60
{
58
- var result = await _service . GetAsync ( ) ;
59
-
60
61
var operationResult = new Operation
61
62
{
62
- Op = OperationCode . add
63
+ Op = OperationCode . get
63
64
} ;
64
65
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
+
65
77
var operations = new List < DocumentData > ( ) ;
66
78
foreach ( var resource in result )
67
79
{
68
80
var doc = _documentBuilder . GetData (
69
- _contextGraph . GetContextEntity ( operation . GetResourceTypeName ( ) ) ,
70
- resource ) ;
81
+ _contextGraph . GetContextEntity ( operation . GetResourceTypeName ( ) ) ,
82
+ resource ) ;
71
83
operations . Add ( doc ) ;
72
84
}
73
85
74
- operationResult . Data = operations ;
86
+ return operations ;
87
+ }
75
88
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 ;
77
98
}
78
99
}
79
100
}
0 commit comments