6
6
using System . Linq ;
7
7
using System . Threading . Tasks ;
8
8
9
- namespace cartservice . cartstore
9
+ namespace cartservice . cartstore ;
10
+
11
+ internal class LocalCartStore : ICartStore
10
12
{
11
- internal class LocalCartStore : ICartStore
12
- {
13
- // Maps between user and their cart
14
- private ConcurrentDictionary < string , Oteldemo . Cart > userCartItems = new ConcurrentDictionary < string , Oteldemo . Cart > ( ) ;
15
- private readonly Oteldemo . Cart emptyCart = new Oteldemo . Cart ( ) ;
13
+ // Maps between user and their cart
14
+ private readonly ConcurrentDictionary < string , Oteldemo . Cart > _userCartItems = new ( ) ;
15
+ private readonly Oteldemo . Cart _emptyCart = new ( ) ;
16
16
17
- public Task InitializeAsync ( )
18
- {
19
- Console . WriteLine ( "Local Cart Store was initialized" ) ;
17
+ public Task InitializeAsync ( )
18
+ {
19
+ Console . WriteLine ( "Local Cart Store was initialized" ) ;
20
20
21
- return Task . CompletedTask ;
22
- }
21
+ return Task . CompletedTask ;
22
+ }
23
23
24
- public Task AddItemAsync ( string userId , string productId , int quantity )
24
+ public Task AddItemAsync ( string userId , string productId , int quantity )
25
+ {
26
+ Console . WriteLine ( $ "AddItemAsync called with userId={ userId } , productId={ productId } , quantity={ quantity } ") ;
27
+ var newCart = new Oteldemo . Cart
25
28
{
26
- Console . WriteLine ( $ "AddItemAsync called with userId={ userId } , productId={ productId } , quantity={ quantity } ") ;
27
- var newCart = new Oteldemo . Cart
28
- {
29
- UserId = userId ,
30
- Items = { new Oteldemo . CartItem { ProductId = productId , Quantity = quantity } }
31
- } ;
32
- userCartItems . AddOrUpdate ( userId , newCart ,
33
- ( k , exVal ) =>
29
+ UserId = userId ,
30
+ Items = { new Oteldemo . CartItem { ProductId = productId , Quantity = quantity } }
31
+ } ;
32
+ _userCartItems . AddOrUpdate ( userId , newCart ,
33
+ ( _ , exVal ) =>
34
34
{
35
35
// If the item exists, we update its quantity
36
36
var existingItem = exVal . Items . SingleOrDefault ( item => item . ProductId == productId ) ;
@@ -46,35 +46,32 @@ public Task AddItemAsync(string userId, string productId, int quantity)
46
46
return exVal ;
47
47
} ) ;
48
48
49
- return Task . CompletedTask ;
50
- }
49
+ return Task . CompletedTask ;
50
+ }
51
51
52
- public Task EmptyCartAsync ( string userId )
53
- {
54
- var eventTags = new ActivityTagsCollection ( ) ;
55
- eventTags . Add ( "userId" , userId ) ;
56
- Activity . Current ? . AddEvent ( new ActivityEvent ( "EmptyCartAsync called." , default , eventTags ) ) ;
52
+ public Task EmptyCartAsync ( string userId )
53
+ {
54
+ var eventTags = new ActivityTagsCollection { { "userId" , userId } } ;
55
+ Activity . Current ? . AddEvent ( new ActivityEvent ( "EmptyCartAsync called." , default , eventTags ) ) ;
57
56
58
- userCartItems [ userId ] = new Oteldemo . Cart ( ) ;
59
- return Task . CompletedTask ;
60
- }
57
+ _userCartItems [ userId ] = new Oteldemo . Cart ( ) ;
58
+ return Task . CompletedTask ;
59
+ }
61
60
62
- public Task < Oteldemo . Cart > GetCartAsync ( string userId )
61
+ public Task < Oteldemo . Cart > GetCartAsync ( string userId )
62
+ {
63
+ Console . WriteLine ( $ "GetCartAsync called with userId={ userId } ") ;
64
+ if ( ! _userCartItems . TryGetValue ( userId , out var cart ) )
63
65
{
64
- Console . WriteLine ( $ "GetCartAsync called with userId={ userId } ") ;
65
- Oteldemo . Cart cart = null ;
66
- if ( ! userCartItems . TryGetValue ( userId , out cart ) )
67
- {
68
- Console . WriteLine ( $ "No carts for user { userId } ") ;
69
- return Task . FromResult ( emptyCart ) ;
70
- }
71
-
72
- return Task . FromResult ( cart ) ;
66
+ Console . WriteLine ( $ "No carts for user { userId } ") ;
67
+ return Task . FromResult ( _emptyCart ) ;
73
68
}
74
69
75
- public bool Ping ( )
76
- {
77
- return true ;
78
- }
70
+ return Task . FromResult ( cart ) ;
71
+ }
72
+
73
+ public bool Ping ( )
74
+ {
75
+ return true ;
79
76
}
80
77
}
0 commit comments