Skip to content

Commit b8d2c9b

Browse files
GwFreak01GwFreak01
authored andcommitted
feat(poll): add update poll option endpoint
- Add UpdatePollOptionResponse class - Add UpdatePollOptionRequestData with UpdatePollOptionRequest builder class - Add updateOption() static factory method - Add PUT /polls/{poll_id}/options service method - Add unit test for poll option update
1 parent 432e256 commit b8d2c9b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,58 @@ protected Call<CreatePollOptionResponse> generateCall(Client client) throws Stre
479479
}
480480
}
481481

482+
/** Response for updating a poll option. */
483+
@Data
484+
@NoArgsConstructor
485+
@EqualsAndHashCode(callSuper = true)
486+
public static class UpdatePollOptionResponse extends StreamResponseObject {
487+
@NotNull
488+
@JsonProperty("poll_option")
489+
private PollOption pollOption;
490+
}
491+
492+
/** Request data for updating a poll option. */
493+
@Builder(
494+
builderClassName = "UpdatePollOptionRequest",
495+
builderMethodName = "",
496+
buildMethodName = "internalBuild")
497+
@Getter
498+
@EqualsAndHashCode
499+
public static class UpdatePollOptionRequestData {
500+
@Nullable
501+
@JsonProperty("id")
502+
private String id;
503+
504+
@Nullable
505+
@JsonProperty("text")
506+
private String text;
507+
508+
@Nullable
509+
@JsonProperty("position")
510+
private Integer position;
511+
512+
@Nullable
513+
@JsonProperty("user_id")
514+
private String userId;
515+
516+
@Nullable
517+
@JsonProperty("user")
518+
private UserRequestObject user;
519+
520+
public static class UpdatePollOptionRequest extends StreamRequest<UpdatePollOptionResponse> {
521+
@NotNull private String pollId;
522+
523+
private UpdatePollOptionRequest(@NotNull String pollId) {
524+
this.pollId = pollId;
525+
}
526+
527+
@Override
528+
protected Call<UpdatePollOptionResponse> generateCall(Client client) throws StreamException {
529+
return client.create(PollService.class).updateOption(this.pollId, this.internalBuild());
530+
}
531+
}
532+
}
533+
482534
/** Request data for creating a poll. */
483535
@Builder(
484536
builderClassName = "CreatePollRequest",
@@ -607,4 +659,16 @@ public static CreatePollOptionRequestData.CreatePollOptionRequest createOption(
607659
@NotNull String pollId) {
608660
return new CreatePollOptionRequestData.CreatePollOptionRequest(pollId);
609661
}
662+
663+
/**
664+
* Updates a poll option.
665+
*
666+
* @param pollId the poll ID
667+
* @return the created request
668+
*/
669+
@NotNull
670+
public static UpdatePollOptionRequestData.UpdatePollOptionRequest updateOption(
671+
@NotNull String pollId) {
672+
return new UpdatePollOptionRequestData.UpdatePollOptionRequest(pollId);
673+
}
610674
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io.getstream.chat.java.models.Poll.CreatePollResponse;
77
import io.getstream.chat.java.models.Poll.GetPollResponse;
88
import io.getstream.chat.java.models.Poll.PartialUpdatePollRequestData;
9+
import io.getstream.chat.java.models.Poll.UpdatePollOptionRequestData;
10+
import io.getstream.chat.java.models.Poll.UpdatePollOptionResponse;
911
import io.getstream.chat.java.models.Poll.UpdatePollRequestData;
1012
import io.getstream.chat.java.models.Poll.UpdatePollResponse;
1113
import io.getstream.chat.java.models.framework.StreamResponseObject;
@@ -85,4 +87,15 @@ Call<StreamResponseObject> delete(
8587
@POST("polls/{poll_id}/options")
8688
Call<CreatePollOptionResponse> createOption(
8789
@NotNull @Path("poll_id") String pollId, @NotNull @Body CreatePollOptionRequestData request);
90+
91+
/**
92+
* Updates a poll option.
93+
*
94+
* @param pollId The poll ID
95+
* @param request The poll option update request data
96+
* @return A response with the updated poll option
97+
*/
98+
@PUT("polls/{poll_id}/options")
99+
Call<UpdatePollOptionResponse> updateOption(
100+
@NotNull @Path("poll_id") String pollId, @NotNull @Body UpdatePollOptionRequestData request);
88101
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.getstream.chat.java.models.Poll.CreatePollResponse;
66
import io.getstream.chat.java.models.Poll.GetPollResponse;
77
import io.getstream.chat.java.models.Poll.PollOptionRequestObject;
8+
import io.getstream.chat.java.models.Poll.UpdatePollOptionResponse;
89
import io.getstream.chat.java.models.Poll.UpdatePollResponse;
910
import io.getstream.chat.java.models.Poll.VotingVisibility;
1011
import io.getstream.chat.java.models.framework.StreamResponseObject;
@@ -317,4 +318,36 @@ void whenCreatingPollOption_thenNoException() {
317318
Assertions.assertNotNull(optionResponse.getPollOption().getId());
318319
Assertions.assertNotNull(optionResponse.getPollOption().getText());
319320
}
321+
322+
@DisplayName("Can update a poll option")
323+
@Test
324+
void whenUpdatingPollOption_thenNoException() {
325+
// Create a poll first
326+
CreatePollResponse createResponse =
327+
Assertions.assertDoesNotThrow(
328+
() ->
329+
Poll.create()
330+
.name("Poll for option update " + UUID.randomUUID())
331+
.userId(testUserRequestObject.getId())
332+
.option(PollOptionRequestObject.builder().text("Original Option").build())
333+
.request());
334+
335+
String pollId = createResponse.getPoll().getId();
336+
String optionId = createResponse.getPoll().getOptions().get(0).getId();
337+
338+
// Update the option
339+
String newText = "Updated Option " + UUID.randomUUID();
340+
UpdatePollOptionResponse updateResponse =
341+
Assertions.assertDoesNotThrow(
342+
() ->
343+
Poll.updateOption(pollId)
344+
.id(optionId)
345+
.text(newText)
346+
.userId(testUserRequestObject.getId())
347+
.request());
348+
349+
Assertions.assertNotNull(updateResponse);
350+
Assertions.assertNotNull(updateResponse.getPollOption());
351+
Assertions.assertEquals(newText, updateResponse.getPollOption().getText());
352+
}
320353
}

0 commit comments

Comments
 (0)