66using System . Linq ;
77using System . Threading . Tasks ;
88
9- namespace cartservice . cartstore
9+ namespace cartservice . cartstore ;
10+
11+ internal class LocalCartStore : ICartStore
1012{
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 ( ) ;
1616
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" ) ;
2020
21- return Task . CompletedTask ;
22- }
21+ return Task . CompletedTask ;
22+ }
2323
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
2528 {
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 ) =>
3434 {
3535 // If the item exists, we update its quantity
3636 var existingItem = exVal . Items . SingleOrDefault ( item => item . ProductId == productId ) ;
@@ -46,35 +46,32 @@ public Task AddItemAsync(string userId, string productId, int quantity)
4646 return exVal ;
4747 } ) ;
4848
49- return Task . CompletedTask ;
50- }
49+ return Task . CompletedTask ;
50+ }
5151
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 ) ) ;
5756
58- userCartItems [ userId ] = new Oteldemo . Cart ( ) ;
59- return Task . CompletedTask ;
60- }
57+ _userCartItems [ userId ] = new Oteldemo . Cart ( ) ;
58+ return Task . CompletedTask ;
59+ }
6160
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 ) )
6365 {
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 ) ;
7368 }
7469
75- public bool Ping ( )
76- {
77- return true ;
78- }
70+ return Task . FromResult ( cart ) ;
71+ }
72+
73+ public bool Ping ( )
74+ {
75+ return true ;
7976 }
8077}
0 commit comments