55using JsonApiDotNetCore . Models ;
66using JsonApiDotNetCore . Models . Operations ;
77using JsonApiDotNetCore . Serialization ;
8- using JsonApiDotNetCore . Services ;
98
109namespace 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}
0 commit comments