@@ -52,99 +52,133 @@ public EstateDomainService(Func<IAggregateService> aggregateService,
5252 #endregion
5353
5454 #region Methods
55+
56+ public async Task < Result > CreateEstate ( EstateCommands . CreateEstateCommand command ,
57+ CancellationToken cancellationToken ) {
58+ try {
59+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < EstateAggregate > ( command . RequestDto . EstateId , ct ) , command . RequestDto . EstateId , cancellationToken , false ) ;
60+ if ( estateResult . IsFailed )
61+ return ResultHelpers . CreateFailure ( estateResult ) ;
5562
56- private async Task < Result > ApplyUpdates ( Action < EstateAggregate > action , Guid estateId , CancellationToken cancellationToken , Boolean isNotFoundError = true )
57- {
58- try
59- {
60- Result < EstateAggregate > getLatestVersionResult = await this . AggregateService . GetLatest < EstateAggregate > ( estateId , cancellationToken ) ;
61- Result < EstateAggregate > estateAggregateResult =
62- DomainServiceHelper . HandleGetAggregateResult ( getLatestVersionResult , estateId , isNotFoundError ) ;
63- if ( estateAggregateResult . IsFailed )
64- return ResultHelpers . CreateFailure ( estateAggregateResult ) ;
65-
66- EstateAggregate estateAggregate = estateAggregateResult . Data ;
63+ EstateAggregate estateAggregate = estateResult . Data ;
6764
68- action ( estateAggregate ) ;
65+ estateAggregate . Create ( command . RequestDto . EstateName ) ;
66+ estateAggregate . GenerateReference ( ) ;
6967
7068 Result saveResult = await this . AggregateService . Save ( estateAggregate , cancellationToken ) ;
7169 if ( saveResult . IsFailed )
7270 return ResultHelpers . CreateFailure ( saveResult ) ;
7371
7472 return Result . Success ( ) ;
7573 }
76- catch ( Exception ex )
77- {
74+ catch ( Exception ex ) {
7875 return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
7976 }
8077 }
8178
82-
83- public async Task < Result > CreateEstate ( EstateCommands . CreateEstateCommand command , CancellationToken cancellationToken )
79+ public async Task < Result > AddOperatorToEstate ( EstateCommands . AddOperatorToEstateCommand command , CancellationToken cancellationToken )
8480 {
85- Result result = await ApplyUpdates ( ( estateAggregate ) => {
86- estateAggregate . Create ( command . RequestDto . EstateName ) ;
87- estateAggregate . GenerateReference ( ) ;
88- } , command . RequestDto . EstateId , cancellationToken , false ) ;
81+ try
82+ {
83+ Result < OperatorAggregate > operatorResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . Get < OperatorAggregate > ( command . RequestDto . OperatorId , ct ) , command . RequestDto . OperatorId , cancellationToken ) ;
84+ if ( operatorResult . IsFailed )
85+ return ResultHelpers . CreateFailure ( operatorResult ) ;
8986
90- return result ;
91- }
87+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < EstateAggregate > ( command . EstateId , ct ) , command . EstateId , cancellationToken ) ;
88+ if ( estateResult . IsFailed )
89+ return ResultHelpers . CreateFailure ( estateResult ) ;
90+
91+ EstateAggregate estateAggregate = estateResult . Data ;
9292
93- public async Task < Result > AddOperatorToEstate ( EstateCommands . AddOperatorToEstateCommand command , CancellationToken cancellationToken )
94- {
95- Result result = await ApplyUpdates ( ( estateAggregate ) => {
9693 estateAggregate . AddOperator ( command . RequestDto . OperatorId ) ;
97- } , command . EstateId , cancellationToken ) ;
9894
99- return result ;
95+ Result saveResult = await this . AggregateService . Save ( estateAggregate , cancellationToken ) ;
96+ if ( saveResult . IsFailed )
97+ return ResultHelpers . CreateFailure ( saveResult ) ;
98+
99+ return Result . Success ( ) ;
100+ }
101+ catch ( Exception ex )
102+ {
103+ return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
104+ }
100105 }
101106
102107 public async Task < Result > CreateEstateUser ( EstateCommands . CreateEstateUserCommand command , CancellationToken cancellationToken )
103108 {
104- CreateUserRequest createUserRequest = new CreateUserRequest
109+ try
105110 {
106- EmailAddress = command . RequestDto . EmailAddress ,
107- FamilyName = command . RequestDto . FamilyName ,
108- GivenName = command . RequestDto . GivenName ,
109- MiddleName = command . RequestDto . MiddleName ,
110- Password = command . RequestDto . Password ,
111- PhoneNumber = "123456" , // Is this really needed :|
112- Roles = new List < String > ( ) ,
113- Claims = new Dictionary < String , String > ( )
114- } ;
115-
116- // Check if role has been overridden
117- String estateRoleName = Environment . GetEnvironmentVariable ( "EstateRoleName" ) ;
118- createUserRequest . Roles . Add ( String . IsNullOrEmpty ( estateRoleName ) ? "Estate" : estateRoleName ) ;
119- createUserRequest . Claims . Add ( "estateId" , command . EstateId . ToString ( ) ) ;
120-
121- Result createUserResult = await this . SecurityServiceClient . CreateUser ( createUserRequest , cancellationToken ) ;
122- if ( createUserResult . IsFailed )
123- return ResultHelpers . CreateFailure ( createUserResult ) ;
124-
125- Result < List < UserDetails > > userDetailsResult = await this . SecurityServiceClient . GetUsers ( createUserRequest . EmailAddress , cancellationToken ) ;
126- if ( userDetailsResult . IsFailed )
127- return ResultHelpers . CreateFailure ( userDetailsResult ) ;
128-
129- UserDetails user = userDetailsResult . Data . SingleOrDefault ( ) ;
130- if ( user == null )
131- return Result . Failure ( $ "Unable to get user details for username { createUserRequest . EmailAddress } ") ;
132-
133- Result result = await ApplyUpdates ( ( estateAggregate ) => {
134- // Add the user to the aggregate
111+ CreateUserRequest createUserRequest = new CreateUserRequest
112+ {
113+ EmailAddress = command . RequestDto . EmailAddress ,
114+ FamilyName = command . RequestDto . FamilyName ,
115+ GivenName = command . RequestDto . GivenName ,
116+ MiddleName = command . RequestDto . MiddleName ,
117+ Password = command . RequestDto . Password ,
118+ PhoneNumber = "123456" , // Is this really needed :|
119+ Roles = new List < String > ( ) ,
120+ Claims = new Dictionary < String , String > ( )
121+ } ;
122+
123+ // Check if role has been overridden
124+ String estateRoleName = Environment . GetEnvironmentVariable ( "EstateRoleName" ) ;
125+ createUserRequest . Roles . Add ( String . IsNullOrEmpty ( estateRoleName ) ? "Estate" : estateRoleName ) ;
126+ createUserRequest . Claims . Add ( "estateId" , command . EstateId . ToString ( ) ) ;
127+
128+ Result createUserResult = await this . SecurityServiceClient . CreateUser ( createUserRequest , cancellationToken ) ;
129+ if ( createUserResult . IsFailed )
130+ return ResultHelpers . CreateFailure ( createUserResult ) ;
131+
132+ Result < List < UserDetails > > userDetailsResult = await this . SecurityServiceClient . GetUsers ( createUserRequest . EmailAddress , cancellationToken ) ;
133+ if ( userDetailsResult . IsFailed )
134+ return ResultHelpers . CreateFailure ( userDetailsResult ) ;
135+
136+ UserDetails user = userDetailsResult . Data . SingleOrDefault ( ) ;
137+ if ( user == null )
138+ return Result . Failure ( $ "Unable to get user details for username { createUserRequest . EmailAddress } ") ;
139+
140+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < EstateAggregate > ( command . EstateId , ct ) , command . EstateId , cancellationToken ) ;
141+ if ( estateResult . IsFailed )
142+ return ResultHelpers . CreateFailure ( estateResult ) ;
143+
144+ EstateAggregate estateAggregate = estateResult . Data ;
145+
135146 estateAggregate . AddSecurityUser ( user . UserId , command . RequestDto . EmailAddress ) ;
136- } , command . EstateId , cancellationToken ) ;
137147
138- return result ;
148+ Result saveResult = await this . AggregateService . Save ( estateAggregate , cancellationToken ) ;
149+ if ( saveResult . IsFailed )
150+ return ResultHelpers . CreateFailure ( saveResult ) ;
151+
152+ return Result . Success ( ) ;
153+ }
154+ catch ( Exception ex )
155+ {
156+ return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
157+ }
139158 }
140159
141160 public async Task < Result > RemoveOperatorFromEstate ( EstateCommands . RemoveOperatorFromEstateCommand command , CancellationToken cancellationToken )
142161 {
143- Result result = await ApplyUpdates ( ( estateAggregate ) => {
162+ try
163+ {
164+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < EstateAggregate > ( command . EstateId , ct ) , command . EstateId , cancellationToken ) ;
165+ if ( estateResult . IsFailed )
166+ return ResultHelpers . CreateFailure ( estateResult ) ;
167+
168+ EstateAggregate estateAggregate = estateResult . Data ;
169+
144170 estateAggregate . RemoveOperator ( command . OperatorId ) ;
145- } , command . EstateId , cancellationToken ) ;
146171
147- return result ;
172+ Result saveResult = await this . AggregateService . Save ( estateAggregate , cancellationToken ) ;
173+ if ( saveResult . IsFailed )
174+ return ResultHelpers . CreateFailure ( saveResult ) ;
175+
176+ return Result . Success ( ) ;
177+ }
178+ catch ( Exception ex )
179+ {
180+ return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
181+ }
148182 }
149183
150184 #endregion
0 commit comments