1- using System ;
1+ using System . Linq ;
22using System . Threading ;
33using System . Threading . Tasks ;
4+ using IdentityServer4 . EntityFramework . Entities ;
45using IdentityServer4 . EntityFramework . Mappers ;
56using Jp . Domain . Commands . Client ;
67using Jp . Domain . Core . Bus ;
78using Jp . Domain . Core . Notifications ;
89using Jp . Domain . Events . Client ;
910using Jp . Domain . Interfaces ;
10- using Jp . Domain . Models ;
1111using MediatR ;
1212
1313namespace Jp . Domain . CommandHandlers
1414{
1515 public class ClientCommandHandler : CommandHandler ,
1616 IRequestHandler < RegisterClientCommand > ,
17- IRequestHandler < UpdateClientCommand >
17+ IRequestHandler < UpdateClientCommand > ,
18+ IRequestHandler < RemoveSecretCommand > ,
19+ IRequestHandler < SaveClientSecretCommand >
1820 {
1921 private readonly IClientRepository _clientRepository ;
22+ private readonly IClientSecretRepository _clientSecretRepository ;
2023
2124 public ClientCommandHandler (
2225 IUnitOfWork uow ,
2326 IMediatorHandler bus ,
2427 INotificationHandler < DomainNotification > notifications ,
25- IClientRepository clientRepository ) : base ( uow , bus , notifications )
28+ IClientRepository clientRepository ,
29+ IClientSecretRepository clientSecretRepository ) : base ( uow , bus , notifications )
2630 {
2731 _clientRepository = clientRepository ;
32+ _clientSecretRepository = clientSecretRepository ;
2833 }
2934
3035
@@ -51,11 +56,11 @@ public async Task Handle(UpdateClientCommand request, CancellationToken cancella
5156 if ( ! request . IsValid ( ) )
5257 {
5358 NotifyValidationErrors ( request ) ;
54- return ;
59+ return ;
5560 }
5661
5762 // Businness logic here
58- var savedClient = await _clientRepository . GetByUniqueName ( request . Client . ClientId ) ;
63+ var savedClient = await _clientRepository . GetClient ( request . Client . ClientId ) ;
5964 if ( savedClient == null )
6065 {
6166 await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
@@ -72,5 +77,68 @@ public async Task Handle(UpdateClientCommand request, CancellationToken cancella
7277 }
7378
7479 }
80+
81+ public async Task Handle ( RemoveSecretCommand request , CancellationToken cancellationToken )
82+ {
83+ if ( ! request . IsValid ( ) )
84+ {
85+ NotifyValidationErrors ( request ) ;
86+ return ;
87+ }
88+
89+ var savedClient = await _clientRepository . GetClient ( request . ClientId ) ;
90+ if ( savedClient == null )
91+ {
92+ await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
93+ return ;
94+ }
95+
96+ if ( savedClient . ClientSecrets . All ( f => f . Id != request . Id ) )
97+ {
98+ await Bus . RaiseEvent ( new DomainNotification ( "2" , "Invalid secret" ) ) ;
99+ return ;
100+ }
101+
102+ _clientSecretRepository . Remove ( request . Id ) ;
103+
104+ if ( Commit ( ) )
105+ {
106+ await Bus . RaiseEvent ( new ClientSecretRemovedEvent ( request . Id , request . ClientId ) ) ;
107+ }
108+ }
109+
110+ public async Task Handle ( SaveClientSecretCommand request , CancellationToken cancellationToken )
111+ {
112+ if ( ! request . IsValid ( ) )
113+ {
114+ NotifyValidationErrors ( request ) ;
115+ return ;
116+ }
117+
118+ var savedClient = await _clientRepository . GetByClientId ( request . ClientId ) ;
119+ if ( savedClient == null )
120+ {
121+ await Bus . RaiseEvent ( new DomainNotification ( "1" , "Client not found" ) ) ;
122+ return ;
123+ }
124+
125+ var secret = new ClientSecret
126+ {
127+ Client = savedClient ,
128+ Description = request . Description ,
129+ Expiration = request . Expiration ,
130+ Type = request . Type ,
131+ Value = request . GetValue ( )
132+ } ;
133+
134+ _clientSecretRepository . Add ( secret ) ;
135+
136+ if ( Commit ( ) )
137+ {
138+ await Bus . RaiseEvent ( new NewClientSecretEvent ( request . Id , request . ClientId , secret . Type , secret . Description ) ) ;
139+ }
140+ }
75141 }
142+
143+
76144}
0 commit comments