55
66namespace BridgingIT . DevKit . Examples . GettingStarted . Modules . CoreModule . Domain . Model ;
77
8+ using System . Net . Sockets ;
89using BridgingIT . DevKit . Domain ;
910
1011/// <summary>
@@ -184,10 +185,10 @@ public Result<Customer> ChangeStatus(CustomerStatus status)
184185 /// <param name="country">The country name (required).</param>
185186 /// <param name="isPrimary">Indicates if this should be the primary address.</param>
186187 /// <returns>The current <see cref="Customer"/> instance for chaining.</returns>
187- public Result < Customer > AddAddress ( string name , string line1 , string line2 , string postalCode , string city , string country , bool isPrimary = false )
188+ public Result < Customer > AddAddress ( string name , string line1 , string line2 , string postalCode , string city , string country )
188189 {
189190 return this . Change ( )
190- . Add ( e => this . addresses , Address . Create ( name , line1 , line2 , postalCode , city , country , isPrimary ) )
191+ . Add ( e => this . addresses , Address . Create ( name , line1 , line2 , postalCode , city , country ) )
191192 . Register ( e => new CustomerUpdatedDomainEvent ( e ) )
192193 . Apply ( ) ;
193194 }
@@ -196,34 +197,23 @@ public Result<Customer> AddAddress(string name, string line1, string line2, stri
196197 /// Removes an address from the customer's address collection.
197198 /// Registers a <see cref="CustomerUpdatedDomainEvent"/>.
198199 /// </summary>
199- /// <param name="addressId ">The ID of the address to remove.</param>
200+ /// <param name="id ">The ID of the address to remove.</param>
200201 /// <returns>The current <see cref="Customer"/> instance for chaining.</returns>
201- public Result < Customer > RemoveAddress ( AddressId addressId )
202+ public Result < Customer > RemoveAddress ( AddressId id )
202203 {
203- var address = this . addresses . FirstOrDefault ( a => a . Id == addressId ) ;
204- if ( address == null )
205- {
206- return Result < Customer > . Failure ( $ "Address with ID { addressId } not found") ;
207- }
208-
209- this . addresses . Remove ( address ) ;
210-
211204 return this . Change ( )
205+ //.When(_ => id != null)
206+ . RemoveById ( e => this . addresses , id , errorMessage : "Address with specified ID not found" )
212207 . Register ( e => new CustomerUpdatedDomainEvent ( e ) )
213208 . Apply ( ) ;
214209 }
215210
216- public Result < Customer > SetPrimaryAddress ( AddressId addressId )
211+ public Result < Customer > SetPrimaryAddress ( AddressId id )
217212 {
218213 return this . Change ( )
219- . Ensure ( _ => this . addresses . Any ( a => a . Id == addressId ) , "Address with ID {addressId} not found" )
220- . Execute ( e =>
221- {
222- foreach ( var addr in this . addresses )
223- {
224- addr . SetPrimary ( addr . Id == addressId ) ;
225- }
226- } )
214+ //.When(_ => id != null)
215+ . Ensure ( _ => this . addresses . Any ( a => a . Id == id ) , "Address with specified ID not found" )
216+ . Set ( e => e . addresses , a => a . SetPrimary ( a . Id == id ) )
227217 . Register ( e => new CustomerUpdatedDomainEvent ( e ) )
228218 . Apply ( ) ;
229219 }
@@ -232,33 +222,57 @@ public Result<Customer> SetPrimaryAddress(AddressId addressId)
232222 /// Updates an existing address with new values.
233223 /// Registers a <see cref="CustomerUpdatedDomainEvent"/>.
234224 /// </summary>
235- /// <param name="addressId ">The ID of the address to update.</param>
225+ /// <param name="id ">The ID of the address to update.</param>
236226 /// <param name="name">The new name/label.</param>
237227 /// <param name="line1">The new first line.</param>
238228 /// <param name="line2">The new second line.</param>
239229 /// <param name="postalCode">The new postal code.</param>
240230 /// <param name="city">The new city.</param>
241231 /// <param name="country">The new country.</param>
242232 /// <returns>The current <see cref="Customer"/> instance for chaining.</returns>
243- public Result < Customer > ChangeAddress ( AddressId addressId , string name , string line1 , string line2 , string postalCode , string city , string country )
233+ public Result < Customer > ChangeAddress ( AddressId id , string name , string line1 , string line2 , string postalCode , string city , string country )
244234 {
245- var address = this . addresses . FirstOrDefault ( a => a . Id == addressId ) ;
235+ var address = this . addresses . FirstOrDefault ( a => a . Id == id ) ;
246236 if ( address == null )
247237 {
248- return Result < Customer > . Failure ( $ "Address with ID { addressId } not found") ;
238+ return Result < Customer > . Failure ( "Address with specified ID not found" ) ;
249239 }
250240
251241 return address . Change ( )
252- . Ensure ( _ => address != null , "Address with ID {addressId} not found" )
253- //.Ensure(_ => this.addresses.Any(a => a.Id == addressId), "Address with ID {addressId} not found")
242+ . Ensure ( _ => address != null , "Address with specified ID not found" )
243+ //.Ensure(_ => this.addresses.Any(a => a.Id == addressId), "Address with specified ID not found")
254244 . Set ( e => e . ChangeName ( name ) )
255245 . Set ( e => e . ChangeLine1 ( line1 ) )
256246 . Set ( e => e . ChangeLine2 ( line2 ) )
257247 . Set ( e => e . ChangePostalCode ( postalCode ) )
258248 . Set ( e => e . ChangeCity ( city ) )
259249 . Set ( e => e . ChangeCountry ( country ) )
260250 . Register ( e => new CustomerUpdatedDomainEvent ( this ) )
261- . Apply ( ) ;
251+ . Apply ( ) . Wrap ( this ) ;
252+
253+ // return customer.Change()
254+ // .Register(e => new CustomerUpdatedDomainEvent(this))
255+ // .Select(e => e.addresses.FirstOrDefault(a => a.Id == id), "Address with specified ID not found") // change to address context
256+ // .Set(a => a.ChangeName(name))
257+ // .Set(a => a.ChangeLine1(line1))
258+ // .Set(a => a.ChangeLine2(line2))
259+ // .Set(a => a.ChangePostalCode(postalCode))
260+ // .Set(a => a.ChangeCity(city))
261+ // .Set(a => a.ChangeCountry(country))
262+ // .Apply();
263+
264+ // return customer.Change()
265+ // .Ensure(_ => this.addresses.Any(a => a.Id == id), "Address with specified ID not found")
266+ // .Select(e => e.addresses.FirstOrDefault(a => a.Id == id) // change to address context
267+ // .Change()
268+ // .Set(a => a.ChangeName(name))
269+ // .Set(a => a.ChangeLine1(line1))
270+ // .Set(a => a.ChangeLine2(line2))
271+ // .Set(a => a.ChangePostalCode(postalCode))
272+ // .Set(a => a.ChangeCity(city))
273+ // .Set(a => a.ChangeCountry(country)).Apply())
274+ // .Register(e => new CustomerUpdatedDomainEvent(this))
275+ // .Apply();
262276
263277 // var nameResult = address.ChangeName(name);
264278 // if (nameResult.IsFailure)
@@ -296,8 +310,8 @@ public Result<Customer> ChangeAddress(AddressId addressId, string name, string l
296310 // return countryResult.Unwrap();
297311 // }
298312
299- // return this.Change()
300- // .Register(e => new CustomerUpdatedDomainEvent(e))
301- // .Apply();
302- }
303- }
313+ // return this.Change()
314+ // .Register(e => new CustomerUpdatedDomainEvent(e))
315+ // .Apply();
316+ }
317+ }
0 commit comments