Skip to content

Commit a3b1fbc

Browse files
authored
fix: send flattened data when upserting (#72)
1 parent 5782b69 commit a3b1fbc

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

src/Collections.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<ResponseBase> UpsertManyAsync(string collectionName, IEnumerab
2727
var body = new Dictionary<string, object>
2828
{
2929
{
30-
"data", new Dictionary<string, object> { { collectionName, data } }
30+
"data", new Dictionary<string, IEnumerable<object>> { { collectionName, data.Select(x => x.Flatten()) } }
3131
},
3232
};
3333
var request = _client.BuildAppRequest("collections/", HttpMethod.Post);
@@ -41,7 +41,7 @@ public async Task<ResponseBase> UpsertManyAsync(string collectionName, IEnumerab
4141
throw StreamException.FromResponse(response);
4242
}
4343

44-
public async Task<GetCollectionResponseObject> SelectAsync(string collectionName, string id)
44+
public async Task<CollectionObject> SelectAsync(string collectionName, string id)
4545
{
4646
var result = await SelectManyAsync(collectionName, new[] { id });
4747
return result.Response.Data.FirstOrDefault();
@@ -75,7 +75,7 @@ public async Task DeleteManyAsync(string collectionName, IEnumerable<string> ids
7575
public async Task<CollectionObject> AddAsync(string collectionName, Dictionary<string, object> data, string id = null, string userId = null)
7676
{
7777
var collectionObject = new CollectionObject(id) { UserId = userId };
78-
data.ForEach(x => collectionObject.Data.SetData(x.Key, x.Value));
78+
data.ForEach(x => collectionObject.SetData(x.Key, x.Value));
7979

8080
var request = _client.BuildAppRequest($"collections/{collectionName}/", HttpMethod.Post);
8181
request.SetJsonBody(StreamJsonConverter.SerializeObject(collectionObject));

src/ICollections.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface ICollections
3131

3232
/// <summary>Returns a collection object.</summary>
3333
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/collections_introduction/?language=csharp</remarks>
34-
Task<GetCollectionResponseObject> SelectAsync(string collectionName, string id);
34+
Task<CollectionObject> SelectAsync(string collectionName, string id);
3535

3636
/// <summary>Returns multiple collection objects.</summary>
3737
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/collections_introduction/?language=csharp</remarks>

src/Models/CollectionObject.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
23
using Stream.Utils;
34
using System;
45
using System.Collections.Generic;
@@ -31,21 +32,30 @@ public class CollectionObject
3132
/// Gets a custom data value parsed into <typeparamref name="T"/>.
3233
/// </summary>
3334
public T GetData<T>(string name) => Data.GetData<T>(name);
34-
}
3535

36-
public class GetCollectionResponseObject
37-
{
38-
public string Id { get; set; }
39-
public string Collection { get; set; }
40-
public string ForeignId { get; set; }
41-
public DateTime CreatedAt { get; set; }
42-
public DateTime UpdatedAt { get; set; }
43-
public CollectionObject Data { get; set; }
36+
internal JObject Flatten()
37+
{
38+
var flat = new JObject();
39+
40+
if (!string.IsNullOrWhiteSpace(Id))
41+
flat["id"] = Id;
42+
43+
if (!string.IsNullOrEmpty(UserId))
44+
flat["user_id"] = UserId;
45+
46+
if (Data?.GetAllData()?.Count > 0)
47+
{
48+
foreach (var kvp in Data.GetAllData())
49+
flat[kvp.Key] = kvp.Value;
50+
}
51+
52+
return flat;
53+
}
4454
}
4555

4656
public class GetCollectionResponse
4757
{
48-
public List<GetCollectionResponseObject> Data { get; set; }
58+
public List<CollectionObject> Data { get; set; }
4959
}
5060

5161
public class GetCollectionResponseWrap : ResponseBase

src/Models/CustomDataBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public abstract class CustomDataBase
1010
[JsonExtensionData]
1111
protected virtual Dictionary<string, JToken> Data { get; set; } = new Dictionary<string, JToken>();
1212

13+
/// <summary>
14+
/// Returns all custom data
15+
/// </summary>
16+
public Dictionary<string, JToken> GetAllData() => Data;
17+
1318
/// <summary>
1419
/// Gets a custom data value parsed into <typeparamref name="T"/>
1520
/// </summary>

tests/ActivityTests/EnrichedActivityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public async Task TestEnrich_Collection()
128128
Assert.NotNull(act.Actor);
129129
Assert.AreEqual("actor-1", act.Actor.Id);
130130
Assert.AreEqual(c.Id, act.Object.Id);
131-
var dataJobject = act.Object.GetData<Dictionary<string, object>>("data")["data"] as JObject;
131+
var dataJobject = act.Object.GetData<Dictionary<string, object>>("data");
132132
Assert.AreEqual("testing_value", dataJobject["field"].ToString());
133133
}
134134

tests/CollectionTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task TestCollectionsCRUD()
4242

4343
Assert.ThrowsAsync<StreamException>(async () =>
4444
{
45-
var o = await Client.Collections.AddAsync("col_test_crud", colData, collectionObject.Id);
45+
await Client.Collections.AddAsync("col_test_crud", colData, collectionObject.Id);
4646
});
4747

4848
// GET
@@ -163,11 +163,9 @@ public async Task TestCollectionsSelectMany()
163163

164164
await Client.Collections.UpsertManyAsync("people", data);
165165

166-
var results = (await Client.Collections.SelectManyAsync("people", new[] { id1, id2 })).Response.Data;
166+
var results = await Client.Collections.SelectManyAsync("people", new[] { id1, id2 });
167167

168-
Assert.NotNull(results);
169-
Assert.AreEqual(data.Count, results.CountOrFallback());
170-
results.ForEach(r =>
168+
results.Response.Data.ForEach(r =>
171169
{
172170
var found = data.First(x => x.Id == r.Id);
173171
var key = r.Id.Equals(id1) ? "hobbies" : "vacation";
@@ -176,7 +174,7 @@ public async Task TestCollectionsSelectMany()
176174
}
177175

178176
[Test]
179-
public void TestCollectionsUpsert()
177+
public async Task TestCollectionsUpsert()
180178
{
181179
var data = new CollectionObject(System.Guid.NewGuid().ToString());
182180
data.SetData("hobbies", new List<string> { "eating", "coding" });
@@ -185,10 +183,13 @@ public void TestCollectionsUpsert()
185183
{
186184
await Client.Collections.UpsertAsync("people", data);
187185
});
186+
187+
var result = await Client.Collections.GetAsync("people", data.Id);
188+
Assert.AreEqual(data.GetData<List<string>>("hobbies"), result.GetData<List<string>>("hobbies"));
188189
}
189190

190191
[Test]
191-
public void TestCollectionsUpsertMany()
192+
public async Task TestCollectionsUpsertMany()
192193
{
193194
var data1 = new CollectionObject(System.Guid.NewGuid().ToString());
194195
data1.SetData("hobbies", new List<string> { "eating", "coding" });
@@ -201,6 +202,14 @@ public void TestCollectionsUpsertMany()
201202
{
202203
await Client.Collections.UpsertManyAsync("people", data);
203204
});
205+
206+
var result = await Client.Collections.SelectManyAsync("people", new[] { data1.Id, data2.Id });
207+
result.Response.Data.ForEach(r =>
208+
{
209+
var found = data.First(x => x.Id == r.Id);
210+
var key = r.Id.Equals(data1.Id) ? "hobbies" : "vacation";
211+
Assert.AreEqual(found.GetData<List<string>>(key), r.GetData<List<string>>(key));
212+
});
204213
}
205214
}
206215
}

0 commit comments

Comments
 (0)