Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<ResponseBase> UpsertManyAsync(string collectionName, IEnumerab
var body = new Dictionary<string, object>
{
{
"data", new Dictionary<string, object> { { collectionName, data } }
"data", new Dictionary<string, IEnumerable<object>> { { collectionName, data.Select(x => x.Flatten()) } }
},
};
var request = _client.BuildAppRequest("collections/", HttpMethod.Post);
Expand All @@ -41,7 +41,7 @@ public async Task<ResponseBase> UpsertManyAsync(string collectionName, IEnumerab
throw StreamException.FromResponse(response);
}

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

var request = _client.BuildAppRequest($"collections/{collectionName}/", HttpMethod.Post);
request.SetJsonBody(StreamJsonConverter.SerializeObject(collectionObject));
Expand Down
2 changes: 1 addition & 1 deletion src/ICollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface ICollections

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

/// <summary>Returns multiple collection objects.</summary>
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/collections_introduction/?language=csharp</remarks>
Expand Down
30 changes: 20 additions & 10 deletions src/Models/CollectionObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stream.Utils;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -31,21 +32,30 @@ public class CollectionObject
/// Gets a custom data value parsed into <typeparamref name="T"/>.
/// </summary>
public T GetData<T>(string name) => Data.GetData<T>(name);
}

public class GetCollectionResponseObject
{
public string Id { get; set; }
public string Collection { get; set; }
public string ForeignId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public CollectionObject Data { get; set; }
internal JObject Flatten()
{
var flat = new JObject();

if (!string.IsNullOrWhiteSpace(Id))
flat["id"] = Id;

if (!string.IsNullOrEmpty(UserId))
flat["user_id"] = UserId;

if (Data?.GetAllData()?.Count > 0)
{
foreach (var kvp in Data.GetAllData())
flat[kvp.Key] = kvp.Value;
}

return flat;
}
}

public class GetCollectionResponse
{
public List<GetCollectionResponseObject> Data { get; set; }
public List<CollectionObject> Data { get; set; }
}

public class GetCollectionResponseWrap : ResponseBase
Expand Down
5 changes: 5 additions & 0 deletions src/Models/CustomDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public abstract class CustomDataBase
[JsonExtensionData]
protected virtual Dictionary<string, JToken> Data { get; set; } = new Dictionary<string, JToken>();

/// <summary>
/// Returns all custom data
/// </summary>
public Dictionary<string, JToken> GetAllData() => Data;

/// <summary>
/// Gets a custom data value parsed into <typeparamref name="T"/>
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/ActivityTests/EnrichedActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public async Task TestEnrich_Collection()
Assert.NotNull(act.Actor);
Assert.AreEqual("actor-1", act.Actor.Id);
Assert.AreEqual(c.Id, act.Object.Id);
var dataJobject = act.Object.GetData<Dictionary<string, object>>("data")["data"] as JObject;
var dataJobject = act.Object.GetData<Dictionary<string, object>>("data");
Assert.AreEqual("testing_value", dataJobject["field"].ToString());
}

Expand Down
23 changes: 16 additions & 7 deletions tests/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task TestCollectionsCRUD()

Assert.ThrowsAsync<StreamException>(async () =>
{
var o = await Client.Collections.AddAsync("col_test_crud", colData, collectionObject.Id);
await Client.Collections.AddAsync("col_test_crud", colData, collectionObject.Id);
});

// GET
Expand Down Expand Up @@ -163,11 +163,9 @@ public async Task TestCollectionsSelectMany()

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

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

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

[Test]
public void TestCollectionsUpsert()
public async Task TestCollectionsUpsert()
{
var data = new CollectionObject(System.Guid.NewGuid().ToString());
data.SetData("hobbies", new List<string> { "eating", "coding" });
Expand All @@ -185,10 +183,13 @@ public void TestCollectionsUpsert()
{
await Client.Collections.UpsertAsync("people", data);
});

var result = await Client.Collections.GetAsync("people", data.Id);
Assert.AreEqual(data.GetData<List<string>>("hobbies"), result.GetData<List<string>>("hobbies"));
}

[Test]
public void TestCollectionsUpsertMany()
public async Task TestCollectionsUpsertMany()
{
var data1 = new CollectionObject(System.Guid.NewGuid().ToString());
data1.SetData("hobbies", new List<string> { "eating", "coding" });
Expand All @@ -201,6 +202,14 @@ public void TestCollectionsUpsertMany()
{
await Client.Collections.UpsertManyAsync("people", data);
});

var result = await Client.Collections.SelectManyAsync("people", new[] { data1.Id, data2.Id });
result.Response.Data.ForEach(r =>
{
var found = data.First(x => x.Id == r.Id);
var key = r.Id.Equals(data1.Id) ? "hobbies" : "vacation";
Assert.AreEqual(found.GetData<List<string>>(key), r.GetData<List<string>>(key));
});
}
}
}