1
1
using System ;
2
+ using System . Text . Json . Serialization ;
2
3
using System . Threading ;
3
4
using System . Threading . Tasks ;
4
5
using Warehouse . Core . Commands ;
6
+ using Warehouse . Core . Primitives ;
5
7
using Warehouse . Products . Primitives ;
6
8
7
9
namespace Warehouse . Products . RegisteringProduct
8
10
{
9
- internal class HandleRegisterProduct : ICommandHandler < RegisterProduct >
11
+ internal class HandleRegisterProduct : ICommandHandler < RegisterProduct >
10
12
{
11
13
private readonly Func < Product , CancellationToken , ValueTask > addProduct ;
12
14
private readonly Func < SKU , CancellationToken , ValueTask < bool > > productWithSKUExists ;
@@ -20,47 +22,51 @@ Func<SKU, CancellationToken, ValueTask<bool>> productWithSKUExists
20
22
this . productWithSKUExists = productWithSKUExists ;
21
23
}
22
24
23
- public async ValueTask Handle ( RegisterProduct command , CancellationToken ct )
25
+ public async ValueTask < CommandResult > Handle ( RegisterProduct command , CancellationToken ct )
24
26
{
27
+ var productId = Guid . NewGuid ( ) ;
28
+ var ( skuValue , name , description ) = command ;
29
+
30
+ var sku = SKU . Create ( skuValue ) ;
31
+
25
32
var product = new Product (
26
- command . ProductId ,
27
- command . SKU ,
28
- command . Name ,
29
- command . Description
33
+ productId ,
34
+ sku ,
35
+ name ,
36
+ description
30
37
) ;
31
38
32
- if ( await productWithSKUExists ( command . SKU , ct ) )
39
+ if ( await productWithSKUExists ( sku , ct ) )
33
40
throw new InvalidOperationException (
34
- $ "Product with SKU `{ command . SKU } already exists.") ;
41
+ $ "Product with SKU `{ command . Sku } already exists.") ;
35
42
36
43
await addProduct ( product , ct ) ;
44
+
45
+ return CommandResult . Of ( productId ) ;
37
46
}
38
47
}
39
48
40
49
public record RegisterProduct
41
50
{
42
- public Guid ProductId { get ; }
43
-
44
- public SKU SKU { get ; }
51
+ public string Sku { get ; }
45
52
46
53
public string Name { get ; }
47
54
48
55
public string ? Description { get ; }
49
56
50
- private RegisterProduct ( Guid productId , SKU sku , string name , string ? description )
57
+ [ JsonConstructor ]
58
+ public RegisterProduct ( string ? sku , string ? name , string ? description )
51
59
{
52
- ProductId = productId ;
53
- SKU = sku ;
54
- Name = name ;
60
+ Sku = sku . AssertNotEmpty ( ) ;
61
+ Name = name . AssertNotEmpty ( ) ;
55
62
Description = description ;
56
63
}
57
64
58
- public static RegisterProduct Create ( Guid ? id , string ? sku , string ? name , string ? description )
65
+ public void Deconstruct ( out string sku , out string name , out string ? description )
59
66
{
60
- if ( ! id . HasValue ) throw new ArgumentNullException ( nameof ( id ) ) ;
61
- if ( name == null ) throw new ArgumentNullException ( nameof ( name ) ) ;
62
-
63
- return new RegisterProduct ( id . Value , SKU . Create ( sku ) , name , description ) ;
67
+ sku = Sku ;
68
+ name = Name ;
69
+ description = Description ;
64
70
}
65
71
}
66
72
}
0 commit comments