1+ using Redis . OM ;
2+ using Redis . OM . UpdatingDocuments . Models ;
3+
4+ const string CONNECTION_URI = "redis://localhost:6379" ;
5+
6+ // create test data
7+ var coffee = new Product ( )
8+ {
9+ Name = "coffee" ,
10+ Description = "a hot drink made from the roasted and ground coffee beans" ,
11+ Price = 2.00 ,
12+ DateAdded = DateTime . UtcNow ,
13+ InStock = false
14+ } ;
15+
16+ // connect and create index
17+ var provider = new RedisConnectionProvider ( CONNECTION_URI ) ;
18+ var connection = provider . Connection ;
19+ var products = provider . RedisCollection < Product > ( ) ;
20+ connection . CreateIndex ( typeof ( Product ) ) ;
21+
22+ // insert data and save Id for querying later
23+ var productId = await products . InsertAsync ( coffee ) ;
24+
25+ // update using IRedisCollection.Update
26+ coffee . Description = "Pure developer fuel" ;
27+ products . Update ( coffee ) ;
28+
29+ // update using IRedisCollection.UpdateAsync
30+ coffee . Price = 2.50 ;
31+ await products . UpdateAsync ( coffee ) ;
32+
33+ // update using IRedisCollection.Save
34+ var updatedCoffee = await products . FindByIdAsync ( productId ) ;
35+ if ( updatedCoffee != null )
36+ {
37+ updatedCoffee . InStock = true ;
38+ }
39+ coffee . InStock = true ;
40+ products . Save ( ) ;
41+
42+ // update using IRedisCollection.SaveAsync
43+ updatedCoffee = await products . FindByIdAsync ( productId ) ;
44+ if ( updatedCoffee != null )
45+ {
46+ updatedCoffee . DateAdded = DateTime . UtcNow . AddDays ( - 7 ) ;
47+ }
48+ coffee . DateAdded = DateTime . UtcNow . AddDays ( - 7 ) ;
49+ await products . SaveAsync ( ) ;
50+
51+ // query and validate results
52+ updatedCoffee = await products . FindByIdAsync ( productId ) ;
53+
54+ Console . WriteLine ( $ "{ nameof ( Product . Name ) } - Expected: { coffee . Name } Actual: { updatedCoffee ? . Name } ") ;
55+ Console . WriteLine ( $ "{ nameof ( Product . Description ) } - Expected: { coffee . Description } Actual: { updatedCoffee ? . Description } ") ;
56+ Console . WriteLine ( $ "{ nameof ( Product . Price ) } - Expected: { coffee . Price } Actual: { updatedCoffee ? . Price } ") ;
57+ Console . WriteLine ( $ "{ nameof ( Product . DateAdded ) } - Expected: { coffee . DateAdded . ToUniversalTime ( ) } Actual: { updatedCoffee ? . DateAdded . ToUniversalTime ( ) } ") ;
58+ Console . WriteLine ( $ "{ nameof ( Product . InStock ) } - Expected: { coffee . InStock } Actual: { updatedCoffee ? . InStock } ") ;
0 commit comments