Skip to content

Commit fc7f08c

Browse files
authored
Add IRedisCollection Update and Save examples (#236)
1 parent 0ee1d09 commit fc7f08c

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Redis.OM.UpdatingDocuments.Models;
2+
3+
using Redis.OM.Modeling;
4+
5+
[Document(IndexName = "products-idx", StorageType = StorageType.Json)]
6+
public class Product
7+
{
8+
[RedisIdField]
9+
[Indexed]
10+
public Ulid Id { get; set; }
11+
12+
[Indexed]
13+
public string? Name { get; set; }
14+
15+
public string? Description { get; set; }
16+
17+
[Indexed(Sortable = true)]
18+
public double Price { get; set; }
19+
20+
public DateTime DateAdded { get; set; }
21+
22+
[Indexed]
23+
public bool InStock { get; set; }
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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}");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Redis.OM" Version="0.3.1" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)