Skip to content

Commit 6131834

Browse files
committed
Add support for partial update
1 parent 6763f08 commit 6131834

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Assets/Plugins/StreamChat/Core/StatefulModels/IStreamPoll.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ public interface IStreamPoll : IStreamStatefulModel
172172
/// <param name="updateRequest">Update request with poll fields to change</param>
173173
Task UpdateAsync(StreamUpdatePollRequest updateRequest);
174174

175+
/// <summary>
176+
/// Update poll in a partial mode. You can selectively set and unset fields of the poll
177+
/// If you want to completely overwrite the poll use the <see cref="UpdateAsync"/>
178+
/// </summary>
179+
/// <param name="setFields">Fields to set with new values</param>
180+
/// <param name="unsetFields">Fields to unset (remove)</param>
181+
Task UpdatePartialAsync(IDictionary<string, object> setFields = null, IEnumerable<string> unsetFields = null);
182+
175183
/// <summary>
176184
/// Close this poll
177185
/// </summary>

Assets/Plugins/StreamChat/Core/StatefulModels/StreamPoll.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,33 @@ public async Task UpdateAsync(StreamUpdatePollRequest updateRequest)
121121
this.TryUpdateFromDto(response.Poll, Cache);
122122
}
123123

124+
public async Task UpdatePartialAsync(IDictionary<string, object> setFields = null, IEnumerable<string> unsetFields = null)
125+
{
126+
if (setFields == null && unsetFields == null)
127+
{
128+
throw new ArgumentNullException($"{nameof(setFields)} and {nameof(unsetFields)} cannot be both null");
129+
}
130+
131+
if (unsetFields != null && !unsetFields.Any())
132+
{
133+
throw new ArgumentException($"{nameof(unsetFields)} cannot be empty");
134+
}
135+
136+
if (setFields != null && !setFields.Any())
137+
{
138+
throw new ArgumentException($"{nameof(setFields)} cannot be empty");
139+
}
140+
141+
var response = await LowLevelClient.InternalPollsApi.UpdatePollPartialAsync(Id,
142+
new UpdatePollPartialRequestInternalDTO
143+
{
144+
Set = setFields?.ToDictionary(p => p.Key, p => p.Value),
145+
Unset = unsetFields?.ToList(),
146+
});
147+
148+
this.TryUpdateFromDto(response.Poll, Cache);
149+
}
150+
124151
public async Task CloseAsync()
125152
{
126153
var request = new UpdatePollPartialRequestInternalDTO

Assets/Plugins/StreamChat/Tests/StatefulClient/PollsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,46 @@ private async Task When_updating_poll_expect_poll_updated_Async()
266266
Assert.AreEqual("Updated Description", poll.Description);
267267
}
268268

269+
[UnityTest]
270+
public IEnumerator When_updating_poll_partially_expect_poll_updated()
271+
=> ConnectAndExecute(When_updating_poll_partially_expect_poll_updated_Async);
272+
273+
private async Task When_updating_poll_partially_expect_poll_updated_Async()
274+
{
275+
var pollId = CreateUniquePollId();
276+
277+
// Create a poll
278+
var createPollRequest = new StreamCreatePollRequest
279+
{
280+
Id = pollId,
281+
Name = "Original Name",
282+
Description = "Original Description",
283+
MaxVotesAllowed = 1,
284+
Options = new List<StreamPollOptionRequest>
285+
{
286+
new StreamPollOptionRequest { Text = "Option 1" }
287+
}
288+
};
289+
290+
var poll = await Client.Polls.CreatePollAsync(createPollRequest);
291+
Assert.NotNull(poll);
292+
Assert.AreEqual("Original Name", poll.Name);
293+
Assert.AreEqual("Original Description", poll.Description);
294+
Assert.AreEqual(1, poll.MaxVotesAllowed);
295+
296+
// Partially update the poll - set specific fields
297+
await poll.UpdatePartialAsync(
298+
setFields: new Dictionary<string, object>
299+
{
300+
{ "name", "Partially Updated Name" },
301+
{ "max_votes_allowed", 3 }
302+
});
303+
304+
Assert.AreEqual("Partially Updated Name", poll.Name);
305+
Assert.AreEqual(3, poll.MaxVotesAllowed);
306+
Assert.AreEqual("Original Description", poll.Description); // Should remain unchanged
307+
}
308+
269309
[UnityTest]
270310
public IEnumerator When_closing_poll_expect_poll_closed()
271311
=> ConnectAndExecute(When_closing_poll_expect_poll_closed_Async);

0 commit comments

Comments
 (0)