Skip to content

Commit 432e256

Browse files
GwFreak01GwFreak01
authored andcommitted
feat(poll): add create poll option endpoint
- Add CreatePollOptionResponse class - Add CreatePollOptionRequestData with CreatePollOptionRequest builder class - Add createOption() static factory method - Add POST /polls/{poll_id}/options service method - Add unit test for poll option creation
1 parent c1c7ef4 commit 432e256

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-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
@@ -427,6 +427,58 @@ protected Call<StreamResponseObject> generateCall(Client client) throws StreamEx
427427
}
428428
}
429429

430+
/** Response for creating a poll option. */
431+
@Data
432+
@NoArgsConstructor
433+
@EqualsAndHashCode(callSuper = true)
434+
public static class CreatePollOptionResponse extends StreamResponseObject {
435+
@NotNull
436+
@JsonProperty("poll_option")
437+
private PollOption pollOption;
438+
}
439+
440+
/** Request data for creating a poll option. */
441+
@Builder(
442+
builderClassName = "CreatePollOptionRequest",
443+
builderMethodName = "",
444+
buildMethodName = "internalBuild")
445+
@Getter
446+
@EqualsAndHashCode
447+
public static class CreatePollOptionRequestData {
448+
@Nullable
449+
@JsonProperty("id")
450+
private String id;
451+
452+
@Nullable
453+
@JsonProperty("text")
454+
private String text;
455+
456+
@Nullable
457+
@JsonProperty("position")
458+
private Integer position;
459+
460+
@Nullable
461+
@JsonProperty("user_id")
462+
private String userId;
463+
464+
@Nullable
465+
@JsonProperty("user")
466+
private UserRequestObject user;
467+
468+
public static class CreatePollOptionRequest extends StreamRequest<CreatePollOptionResponse> {
469+
@NotNull private String pollId;
470+
471+
private CreatePollOptionRequest(@NotNull String pollId) {
472+
this.pollId = pollId;
473+
}
474+
475+
@Override
476+
protected Call<CreatePollOptionResponse> generateCall(Client client) throws StreamException {
477+
return client.create(PollService.class).createOption(this.pollId, this.internalBuild());
478+
}
479+
}
480+
}
481+
430482
/** Request data for creating a poll. */
431483
@Builder(
432484
builderClassName = "CreatePollRequest",
@@ -543,4 +595,16 @@ public static PartialUpdatePollRequestData.PartialUpdatePollRequest partialUpdat
543595
public static DeletePollRequest delete(@NotNull String pollId) {
544596
return new DeletePollRequest(pollId);
545597
}
598+
599+
/**
600+
* Creates a poll option.
601+
*
602+
* @param pollId the poll ID
603+
* @return the created request
604+
*/
605+
@NotNull
606+
public static CreatePollOptionRequestData.CreatePollOptionRequest createOption(
607+
@NotNull String pollId) {
608+
return new CreatePollOptionRequestData.CreatePollOptionRequest(pollId);
609+
}
546610
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.getstream.chat.java.services;
22

3+
import io.getstream.chat.java.models.Poll.CreatePollOptionRequestData;
4+
import io.getstream.chat.java.models.Poll.CreatePollOptionResponse;
35
import io.getstream.chat.java.models.Poll.CreatePollRequestData;
46
import io.getstream.chat.java.models.Poll.CreatePollResponse;
57
import io.getstream.chat.java.models.Poll.GetPollResponse;
@@ -72,4 +74,15 @@ Call<UpdatePollResponse> partialUpdate(
7274
@DELETE("polls/{poll_id}")
7375
Call<StreamResponseObject> delete(
7476
@NotNull @Path("poll_id") String pollId, @Nullable @Query("user_id") String userId);
77+
78+
/**
79+
* Creates a poll option.
80+
*
81+
* @param pollId The poll ID
82+
* @param request The poll option creation request data
83+
* @return A response with the created poll option
84+
*/
85+
@POST("polls/{poll_id}/options")
86+
Call<CreatePollOptionResponse> createOption(
87+
@NotNull @Path("poll_id") String pollId, @NotNull @Body CreatePollOptionRequestData request);
7588
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.getstream.chat.java;
22

33
import io.getstream.chat.java.models.Poll;
4+
import io.getstream.chat.java.models.Poll.CreatePollOptionResponse;
45
import io.getstream.chat.java.models.Poll.CreatePollResponse;
56
import io.getstream.chat.java.models.Poll.GetPollResponse;
67
import io.getstream.chat.java.models.Poll.PollOptionRequestObject;
@@ -285,4 +286,35 @@ void whenDeletingPoll_thenNoException() {
285286

286287
Assertions.assertNotNull(deleteResponse);
287288
}
289+
290+
@DisplayName("Can create a poll option")
291+
@Test
292+
void whenCreatingPollOption_thenNoException() {
293+
// Create a poll first (with allow_user_suggested_options enabled)
294+
CreatePollResponse createResponse =
295+
Assertions.assertDoesNotThrow(
296+
() ->
297+
Poll.create()
298+
.name("Poll for options " + UUID.randomUUID())
299+
.allowUserSuggestedOptions(true)
300+
.userId(testUserRequestObject.getId())
301+
.option(PollOptionRequestObject.builder().text("Initial Option").build())
302+
.request());
303+
304+
String pollId = createResponse.getPoll().getId();
305+
306+
// Create a new option
307+
CreatePollOptionResponse optionResponse =
308+
Assertions.assertDoesNotThrow(
309+
() ->
310+
Poll.createOption(pollId)
311+
.text("New Option " + UUID.randomUUID())
312+
.userId(testUserRequestObject.getId())
313+
.request());
314+
315+
Assertions.assertNotNull(optionResponse);
316+
Assertions.assertNotNull(optionResponse.getPollOption());
317+
Assertions.assertNotNull(optionResponse.getPollOption().getId());
318+
Assertions.assertNotNull(optionResponse.getPollOption().getText());
319+
}
288320
}

0 commit comments

Comments
 (0)