Skip to content

Commit 97c4b33

Browse files
GwFreak01GwFreak01
authored andcommitted
feat(poll): add partial update poll endpoint
- Add PartialUpdatePollRequestData with PartialUpdatePollRequest builder class - Add partialUpdate() static factory method - Add PATCH /polls/{poll_id} service method - Add unit tests for partial update with set and unset
1 parent 6d942e7 commit 97c4b33

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

src/main/java/io/getstream/chat/java/models/Poll.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,46 @@ protected Call<UpdatePollResponse> generateCall(Client client) throws StreamExce
367367
}
368368
}
369369

370+
/** Request data for partially updating a poll. */
371+
@Builder(
372+
builderClassName = "PartialUpdatePollRequest",
373+
builderMethodName = "",
374+
buildMethodName = "internalBuild")
375+
@Getter
376+
@EqualsAndHashCode
377+
public static class PartialUpdatePollRequestData {
378+
@Singular
379+
@Nullable
380+
@JsonProperty("set")
381+
private Map<String, Object> setValues;
382+
383+
@Singular
384+
@Nullable
385+
@JsonProperty("unset")
386+
private List<String> unsetValues;
387+
388+
@Nullable
389+
@JsonProperty("user_id")
390+
private String userId;
391+
392+
@Nullable
393+
@JsonProperty("user")
394+
private UserRequestObject user;
395+
396+
public static class PartialUpdatePollRequest extends StreamRequest<UpdatePollResponse> {
397+
@NotNull private String pollId;
398+
399+
private PartialUpdatePollRequest(@NotNull String pollId) {
400+
this.pollId = pollId;
401+
}
402+
403+
@Override
404+
protected Call<UpdatePollResponse> generateCall(Client client) throws StreamException {
405+
return client.create(PollService.class).partialUpdate(this.pollId, this.internalBuild());
406+
}
407+
}
408+
}
409+
370410
/** Request data for creating a poll. */
371411
@Builder(
372412
builderClassName = "CreatePollRequest",
@@ -460,4 +500,16 @@ public static GetPollRequest get(@NotNull String pollId) {
460500
public static UpdatePollRequestData.UpdatePollRequest update() {
461501
return new UpdatePollRequestData.UpdatePollRequest();
462502
}
503+
504+
/**
505+
* Partially updates a poll.
506+
*
507+
* @param pollId the poll ID
508+
* @return the created request
509+
*/
510+
@NotNull
511+
public static PartialUpdatePollRequestData.PartialUpdatePollRequest partialUpdate(
512+
@NotNull String pollId) {
513+
return new PartialUpdatePollRequestData.PartialUpdatePollRequest(pollId);
514+
}
463515
}

src/main/java/io/getstream/chat/java/services/PollService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import io.getstream.chat.java.models.Poll.CreatePollRequestData;
44
import io.getstream.chat.java.models.Poll.CreatePollResponse;
55
import io.getstream.chat.java.models.Poll.GetPollResponse;
6+
import io.getstream.chat.java.models.Poll.PartialUpdatePollRequestData;
67
import io.getstream.chat.java.models.Poll.UpdatePollRequestData;
78
import io.getstream.chat.java.models.Poll.UpdatePollResponse;
89
import org.jetbrains.annotations.NotNull;
910
import org.jetbrains.annotations.Nullable;
1011
import retrofit2.Call;
1112
import retrofit2.http.Body;
1213
import retrofit2.http.GET;
14+
import retrofit2.http.PATCH;
1315
import retrofit2.http.POST;
1416
import retrofit2.http.PUT;
1517
import retrofit2.http.Path;
@@ -46,4 +48,15 @@ Call<GetPollResponse> get(
4648
*/
4749
@PUT("polls")
4850
Call<UpdatePollResponse> update(@NotNull @Body UpdatePollRequestData request);
51+
52+
/**
53+
* Partially updates a poll.
54+
*
55+
* @param pollId The poll ID
56+
* @param request The partial update request data
57+
* @return A response with the updated poll
58+
*/
59+
@PATCH("polls/{poll_id}")
60+
Call<UpdatePollResponse> partialUpdate(
61+
@NotNull @Path("poll_id") String pollId, @NotNull @Body PartialUpdatePollRequestData request);
4962
}

src/test/java/io/getstream/chat/java/PollTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,64 @@ void whenClosingPoll_thenNoException() {
201201
Assertions.assertNotNull(updateResponse);
202202
Assertions.assertTrue(updateResponse.getPoll().getIsClosed());
203203
}
204+
205+
@DisplayName("Can partially update a poll with set")
206+
@Test
207+
void whenPartiallyUpdatingPollWithSet_thenNoException() {
208+
// Create a poll first
209+
CreatePollResponse createResponse =
210+
Assertions.assertDoesNotThrow(
211+
() ->
212+
Poll.create()
213+
.name("Original Partial " + UUID.randomUUID())
214+
.description("Original description")
215+
.userId(testUserRequestObject.getId())
216+
.option(PollOptionRequestObject.builder().text("A").build())
217+
.request());
218+
219+
String pollId = createResponse.getPoll().getId();
220+
221+
// Partial update the poll
222+
UpdatePollResponse updateResponse =
223+
Assertions.assertDoesNotThrow(
224+
() ->
225+
Poll.partialUpdate(pollId)
226+
.setValue("name", "Partial Updated Name")
227+
.userId(testUserRequestObject.getId())
228+
.request());
229+
230+
Assertions.assertNotNull(updateResponse);
231+
Assertions.assertEquals("Partial Updated Name", updateResponse.getPoll().getName());
232+
// Description should remain unchanged
233+
Assertions.assertEquals("Original description", updateResponse.getPoll().getDescription());
234+
}
235+
236+
@DisplayName("Can partially update a poll with unset")
237+
@Test
238+
void whenPartiallyUpdatingPollWithUnset_thenNoException() {
239+
// Create a poll first
240+
CreatePollResponse createResponse =
241+
Assertions.assertDoesNotThrow(
242+
() ->
243+
Poll.create()
244+
.name("Poll with description " + UUID.randomUUID())
245+
.description("To be removed")
246+
.userId(testUserRequestObject.getId())
247+
.option(PollOptionRequestObject.builder().text("A").build())
248+
.request());
249+
250+
String pollId = createResponse.getPoll().getId();
251+
252+
// Partial update to unset description
253+
UpdatePollResponse updateResponse =
254+
Assertions.assertDoesNotThrow(
255+
() ->
256+
Poll.partialUpdate(pollId)
257+
.unsetValue("description")
258+
.userId(testUserRequestObject.getId())
259+
.request());
260+
261+
Assertions.assertNotNull(updateResponse);
262+
Assertions.assertNull(updateResponse.getPoll().getDescription());
263+
}
204264
}

0 commit comments

Comments
 (0)