1313namespace Jp . Domain . CommandHandlers
1414{
1515 public class ApiResourceCommandHandler : CommandHandler ,
16- IRequestHandler < RegisterApiResourceCommand > ,
17- IRequestHandler < UpdateApiResourceCommand > ,
18- IRequestHandler < RemoveApiResourceCommand > ,
19- IRequestHandler < RemoveApiSecretCommand > ,
20- IRequestHandler < SaveApiSecretCommand > ,
21- IRequestHandler < RemoveApiScopeCommand > ,
22- IRequestHandler < SaveApiScopeCommand >
16+ IRequestHandler < RegisterApiResourceCommand , bool > ,
17+ IRequestHandler < UpdateApiResourceCommand , bool > ,
18+ IRequestHandler < RemoveApiResourceCommand , bool > ,
19+ IRequestHandler < RemoveApiSecretCommand , bool > ,
20+ IRequestHandler < SaveApiSecretCommand , bool > ,
21+ IRequestHandler < RemoveApiScopeCommand , bool > ,
22+ IRequestHandler < SaveApiScopeCommand , bool >
2323 {
2424 private readonly IApiResourceRepository _apiResourceRepository ;
2525 private readonly IApiSecretRepository _apiSecretRepository ;
@@ -39,19 +39,19 @@ public ApiResourceCommandHandler(
3939 }
4040
4141
42- public async Task Handle ( RegisterApiResourceCommand request , CancellationToken cancellationToken )
42+ public async Task < bool > Handle ( RegisterApiResourceCommand request , CancellationToken cancellationToken )
4343 {
4444 if ( ! request . IsValid ( ) )
4545 {
4646 NotifyValidationErrors ( request ) ;
47- return ;
47+ return false ;
4848 }
4949
5050 var savedClient = await _apiResourceRepository . GetResource ( request . Resource . Name ) ;
5151 if ( savedClient != null )
5252 {
5353 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Resource already exists" ) ) ;
54- return ;
54+ return false ;
5555 }
5656
5757 var api = request . Resource . ToEntity ( ) ;
@@ -60,25 +60,25 @@ public async Task Handle(RegisterApiResourceCommand request, CancellationToken c
6060 if ( Commit ( ) )
6161 {
6262 await Bus . RaiseEvent ( new ApiResourceRegisteredEvent ( request . Resource . Name ) ) ;
63+ return true ;
6364 }
64-
65-
65+ return false ;
6666 }
6767
6868
69- public async Task Handle ( UpdateApiResourceCommand request , CancellationToken cancellationToken )
69+ public async Task < bool > Handle ( UpdateApiResourceCommand request , CancellationToken cancellationToken )
7070 {
7171 if ( ! request . IsValid ( ) )
7272 {
7373 NotifyValidationErrors ( request ) ;
74- return ;
74+ return false ;
7575 }
7676
7777 var savedClient = await _apiResourceRepository . GetResource ( request . Resource . Name ) ;
7878 if ( savedClient == null )
7979 {
8080 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Resource not found" ) ) ;
81- return ;
81+ return false ;
8282 }
8383
8484 var irs = request . Resource . ToEntity ( ) ;
@@ -88,74 +88,80 @@ public async Task Handle(UpdateApiResourceCommand request, CancellationToken can
8888 if ( Commit ( ) )
8989 {
9090 await Bus . RaiseEvent ( new ApiResourceUpdatedEvent ( request . Resource ) ) ;
91+ return true ;
9192 }
93+ return false ;
9294 }
9395
94- public async Task Handle ( RemoveApiResourceCommand request , CancellationToken cancellationToken )
96+ public async Task < bool > Handle ( RemoveApiResourceCommand request , CancellationToken cancellationToken )
9597 {
9698 if ( ! request . IsValid ( ) )
9799 {
98100 NotifyValidationErrors ( request ) ;
99- return ;
101+ return false ;
100102 }
101103
102104 var savedClient = await _apiResourceRepository . GetResource ( request . Resource . Name ) ;
103105 if ( savedClient == null )
104106 {
105107 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Resource not found" ) ) ;
106- return ;
108+ return false ;
107109 }
108110
109111 _apiResourceRepository . Remove ( savedClient . Id ) ;
110112
111113 if ( Commit ( ) )
112114 {
113115 await Bus . RaiseEvent ( new ApiResourceRemovedEvent ( request . Resource . Name ) ) ;
116+ return true ;
114117 }
118+ return false ;
115119 }
116120
117- public async Task Handle ( RemoveApiSecretCommand request , CancellationToken cancellationToken )
121+ public async Task < bool > Handle ( RemoveApiSecretCommand request , CancellationToken cancellationToken )
118122 {
119123 if ( ! request . IsValid ( ) )
120124 {
121125 NotifyValidationErrors ( request ) ;
122- return ;
126+ return false ;
123127 }
124128
125129 var savedClient = await _apiResourceRepository . GetResource ( request . ResourceName ) ;
126130 if ( savedClient == null )
127131 {
128132 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
129- return ;
133+ return false ;
130134 }
131135
132136 if ( savedClient . Secrets . All ( f => f . Id != request . Id ) )
133137 {
134138 await Bus . RaiseEvent ( new DomainNotification ( "2" , "Invalid secret" ) ) ;
135- return ;
139+ return false ;
136140 }
137141
138142 _apiSecretRepository . Remove ( request . Id ) ;
139143
140144 if ( Commit ( ) )
141145 {
142146 await Bus . RaiseEvent ( new ApiSecretRemovedEvent ( request . Id , request . ResourceName ) ) ;
147+ return true ;
143148 }
149+ return false ;
144150 }
145151
146- public async Task Handle ( SaveApiSecretCommand request , CancellationToken cancellationToken )
152+ public async Task < bool > Handle ( SaveApiSecretCommand request , CancellationToken cancellationToken )
147153 {
148154 if ( ! request . IsValid ( ) )
149155 {
150156 NotifyValidationErrors ( request ) ;
151- return ;
157+ return false ;
152158 }
153159
154160 var savedClient = await _apiResourceRepository . GetByName ( request . ResourceName ) ;
155161 if ( savedClient == null )
156162 {
157163 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
158- return ;
164+ return false ;
159165 }
160166
161167 var secret = new ApiSecret
@@ -171,28 +177,30 @@ public async Task Handle(SaveApiSecretCommand request, CancellationToken cancell
171177 if ( Commit ( ) )
172178 {
173179 await Bus . RaiseEvent ( new ApiSecretSavedEvent ( request . Id , request . ResourceName ) ) ;
180+ return true ;
174181 }
182+ return false ;
175183 }
176184
177- public async Task Handle ( RemoveApiScopeCommand request , CancellationToken cancellationToken )
185+ public async Task < bool > Handle ( RemoveApiScopeCommand request , CancellationToken cancellationToken )
178186 {
179187 if ( ! request . IsValid ( ) )
180188 {
181189 NotifyValidationErrors ( request ) ;
182- return ;
190+ return false ;
183191 }
184192
185193 var savedClient = await _apiResourceRepository . GetResource ( request . ResourceName ) ;
186194 if ( savedClient == null )
187195 {
188196 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
189- return ;
197+ return false ;
190198 }
191199
192200 if ( savedClient . Scopes . All ( f => f . Id != request . Id ) )
193201 {
194202 await Bus . RaiseEvent ( new DomainNotification ( "3" , "Invalid scope" ) ) ;
195- return ;
203+ return false ;
196204 }
197205
198206 var scopeToremove = savedClient . Scopes . First ( f => f . Id == request . Id ) ;
@@ -201,22 +209,24 @@ public async Task Handle(RemoveApiScopeCommand request, CancellationToken cancel
201209 if ( Commit ( ) )
202210 {
203211 await Bus . RaiseEvent ( new ApiScopeRemovedEvent ( request . Id , request . ResourceName , scopeToremove . Name ) ) ;
212+ return true ;
204213 }
214+ return false ;
205215 }
206216
207- public async Task Handle ( SaveApiScopeCommand request , CancellationToken cancellationToken )
217+ public async Task < bool > Handle ( SaveApiScopeCommand request , CancellationToken cancellationToken )
208218 {
209219 if ( ! request . IsValid ( ) )
210220 {
211221 NotifyValidationErrors ( request ) ;
212- return ;
222+ return false ;
213223 }
214224
215225 var savedClient = await _apiResourceRepository . GetByName ( request . ResourceName ) ;
216226 if ( savedClient == null )
217227 {
218228 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
219- return ;
229+ return false ;
220230 }
221231
222232 var secret = new ApiScope ( )
@@ -236,7 +246,9 @@ public async Task Handle(SaveApiScopeCommand request, CancellationToken cancella
236246 if ( Commit ( ) )
237247 {
238248 await Bus . RaiseEvent ( new ApiSecretSavedEvent ( request . Id , request . ResourceName ) ) ;
249+ return true ;
239250 }
251+ return false ;
240252 }
241253 }
242254}
0 commit comments