Skip to content

Commit a398aec

Browse files
Добавлен обработчик выбора пола пользователя и обновлены состояния регистрации
1 parent b8d2848 commit a398aec

File tree

5 files changed

+122
-3
lines changed

5 files changed

+122
-3
lines changed

src/main/java/ru/untitled_devs/bot/features/registration/RegistrationScene.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public RegistrationScene(BotClient bot, RegistrationService regService,
3535
private void registerHandlers() {
3636
addHandler(RegistrationStates.NAME, new RegNameHandler(bot));
3737
addHandler(RegistrationStates.AGE, new RegAgeHandler(bot));
38+
addHandler(RegistrationStates.GENDER, new RegGenderHandler(bot));
3839
addHandler(RegistrationStates.LOCATION, new RegLocationHandler(bot, geocoder));
3940
addHandler(RegistrationStates.DESCRIPTION, new RegDescriptionHandler(bot));
4041
addHandler(RegistrationStates.PHOTO, new RegPhotoHandler(bot, imageService));

src/main/java/ru/untitled_devs/bot/features/registration/RegistrationStates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public final class RegistrationStates extends StatesGroup {
88
public static State NAME = state(RegistrationStates.class, "Name");
99
public static State AGE = state(RegistrationStates.class, "Age");
1010
public static State LOCATION = state(RegistrationStates.class, "Location");
11-
public static State SEX = state(RegistrationStates.class, "Sex");
11+
public static State GENDER = state(RegistrationStates.class, "Sex");
1212
public static State DESCRIPTION = state(RegistrationStates.class, "Description");
1313
public static State PHOTO = state(RegistrationStates.class, "Photo");
1414
public static State PREVIEW = state(RegistrationStates.class, "Preview");

src/main/java/ru/untitled_devs/bot/features/registration/handlers/RegAgeHandler.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package ru.untitled_devs.bot.features.registration.handlers;
22

33
import org.telegram.telegrambots.meta.api.objects.Message;
4+
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
5+
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
6+
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
47
import ru.untitled_devs.bot.features.registration.RegistrationStates;
8+
import ru.untitled_devs.bot.shared.localisation.BtnLocService;
9+
import ru.untitled_devs.bot.shared.localisation.ButtonKey;
510
import ru.untitled_devs.bot.shared.localisation.MessageKey;
611
import ru.untitled_devs.bot.shared.localisation.MsgLocService;
712
import ru.untitled_devs.bot.shared.models.Profile;
813
import ru.untitled_devs.core.client.BotClient;
914
import ru.untitled_devs.core.fsm.context.FSMContext;
1015
import ru.untitled_devs.core.routers.filters.Filter;
1116

17+
import java.util.ArrayList;
18+
import java.util.List;
1219
import java.util.Locale;
1320

1421
public class RegAgeHandler extends RegStepHandler {
@@ -50,9 +57,29 @@ void action(Message message, FSMContext ctx) {
5057
profileData.setAge(age);
5158

5259
ctx.setData(profileKey, profileData);
53-
ctx.setState(RegistrationStates.LOCATION);
60+
ctx.setState(RegistrationStates.GENDER);
61+
62+
ReplyKeyboardMarkup markup = new ReplyKeyboardMarkup();
63+
markup.setResizeKeyboard(true);
64+
markup.setOneTimeKeyboard(true);
65+
markup.setSelective(false);
66+
67+
KeyboardRow row = new KeyboardRow();
68+
row.add(new KeyboardButton(
69+
BtnLocService.getLocal(ButtonKey.GENDER_MALE, loc))
70+
);
71+
row.add(new KeyboardButton(
72+
BtnLocService.getLocal(ButtonKey.GENDER_FEMALE, loc))
73+
);
74+
row.add(new KeyboardButton(
75+
BtnLocService.getLocal(ButtonKey.GENDER_NONE, loc))
76+
);
77+
78+
List<KeyboardRow> rows = new ArrayList<>();
79+
rows.add(row);
80+
markup.setKeyboard(rows);
5481

5582
bot.sendMessage(message.getChatId(),
56-
MsgLocService.getLocal(MessageKey.ASK_LOCATION, loc));
83+
MsgLocService.getLocal(MessageKey.ASK_SEX, loc), markup);
5784
}
5885
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.untitled_devs.bot.features.registration.handlers;
2+
3+
import org.telegram.telegrambots.meta.api.objects.Message;
4+
import ru.untitled_devs.bot.features.registration.RegistrationStates;
5+
import ru.untitled_devs.bot.shared.localisation.BtnLocService;
6+
import ru.untitled_devs.bot.shared.localisation.ButtonKey;
7+
import ru.untitled_devs.bot.shared.localisation.MessageKey;
8+
import ru.untitled_devs.bot.shared.localisation.MsgLocService;
9+
import ru.untitled_devs.bot.shared.models.Gender;
10+
import ru.untitled_devs.bot.shared.models.Profile;
11+
import ru.untitled_devs.core.client.BotClient;
12+
import ru.untitled_devs.core.fsm.context.FSMContext;
13+
import ru.untitled_devs.core.routers.filters.Filter;
14+
15+
import java.util.Locale;
16+
import java.util.Map;
17+
18+
public class RegGenderHandler extends RegStepHandler {
19+
public RegGenderHandler(BotClient bot, Filter... filters) {
20+
super(bot, filters);
21+
}
22+
23+
@Override
24+
void action(Message message, FSMContext ctx) {
25+
Locale loc = ctx.getData(langKey);
26+
Profile profileData = ctx.getData(profileKey);
27+
28+
String text = message.getText();
29+
30+
String maleButton = BtnLocService.getLocal(ButtonKey.GENDER_MALE, loc);
31+
String femaleButton = BtnLocService.getLocal(ButtonKey.GENDER_FEMALE, loc);
32+
String noneGenderButton = BtnLocService.getLocal(ButtonKey.GENDER_NONE, loc);
33+
34+
Map<String, Gender> gendersMap = Map.of(
35+
maleButton, Gender.MALE,
36+
femaleButton, Gender.FEMALE,
37+
noneGenderButton, Gender.NONE
38+
);
39+
40+
Gender selectedGender = gendersMap.get(text);
41+
42+
if (selectedGender != null) {
43+
profileData.setGender(selectedGender);
44+
ctx.setState(RegistrationStates.LOCATION);
45+
bot.sendMessage(message.getChatId(), MsgLocService.getLocal(MessageKey.ASK_LOCATION, loc));
46+
} else {
47+
bot.sendMessage(message.getChatId(), MsgLocService.getLocal(MessageKey.INCORRECT_INPUT, loc));
48+
throw new IllegalArgumentException();
49+
}
50+
51+
}
52+
}

src/test/java/ru/untitled_devs/bot/features/registration/RegistrationSceneTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ru.untitled_devs.bot.shared.image.ImageService;
1111
import ru.untitled_devs.bot.shared.localisation.BtnLocService;
1212
import ru.untitled_devs.bot.shared.localisation.ButtonKey;
13+
import ru.untitled_devs.bot.shared.models.Gender;
1314
import ru.untitled_devs.bot.shared.models.Image;
1415
import ru.untitled_devs.bot.shared.models.Profile;
1516
import ru.untitled_devs.core.client.PollingClient;
@@ -180,6 +181,44 @@ void getAgeGoTooSmallAge() {
180181
"Get age must do not throw exception when got invalid age value");
181182
}
182183

184+
@Test
185+
void getGenderGotValidGender() {
186+
Message message = mock(Message.class);
187+
String genderButton = BtnLocService.getLocal(ButtonKey.GENDER_FEMALE, Locale.forLanguageTag("en-US"));
188+
when(message.getText()).thenReturn(genderButton);
189+
190+
when(updateContext.hasMessage()).thenReturn(true);
191+
when(update.getMessage()).thenReturn(message);
192+
193+
when(ctx.getState()).thenReturn(RegistrationStates.GENDER);
194+
195+
Profile profile = new Profile();
196+
DataKey<Profile> key = DataKey.of("RegistrationData", Profile.class);
197+
when(ctx.getData(key)).thenReturn(profile);
198+
199+
assertDoesNotThrow(() -> router.routeUpdate(updateContext, ctx));
200+
assertEquals(Gender.FEMALE, profile.getGender());
201+
}
202+
203+
@Test
204+
void getGenderGotInvalidGender() {
205+
Message message = mock(Message.class);
206+
String genderButton = "awbfgaowgbpaioug";
207+
when(message.getText()).thenReturn(genderButton);
208+
209+
when(updateContext.hasMessage()).thenReturn(true);
210+
when(update.getMessage()).thenReturn(message);
211+
212+
when(ctx.getState()).thenReturn(RegistrationStates.GENDER);
213+
214+
Profile profile = new Profile();
215+
DataKey<Profile> key = DataKey.of("RegistrationData", Profile.class);
216+
when(ctx.getData(key)).thenReturn(profile);
217+
218+
assertThrows(IllegalArgumentException.class,
219+
() -> router.routeUpdate(updateContext, ctx));
220+
}
221+
183222
@Test
184223
void getLocationGotValidLocation() {
185224
DataKey<Profile> profileKey = DataKey.of("RegistrationData", Profile.class);

0 commit comments

Comments
 (0)