refactor: Improve profile editing structure for future extensibility

This commit is contained in:
2025-02-16 21:39:54 +03:00
parent 2b89c9d581
commit b5e7f6e2c8
2 changed files with 36 additions and 23 deletions
@@ -23,13 +23,9 @@ public class ProfileServiceImpl implements ProfileService {
public void enterProfileEditingMode(BotUser user) {
user.setWorkingMode(BotUser.WorkingMode.EDIT_PROFILE);
userRepository.save(user);
telegramClientService.sendMessage(
user,
"⌨️ Режим редактирования профиля:\nТекущая цель по книгам "
+ Optional.of(user.getBooksToRead().toString()).orElse("Не задано"));
telegramClientService.sendMessage(
user,
"Введите цель по количеству книг или вернитесь назад",
"⌨️ Выберите параметр для редактирования:",
KeyboardHelper.getProfileMenuKeyboard()
);
}
@@ -37,25 +33,42 @@ public class ProfileServiceImpl implements ProfileService {
@Override
public void handleProfileUpdate(BotUser user, String input) throws ServiceException {
try {
if (input.matches("\\d+")) {
int goal = Integer.parseInt(input);
if (goal < 0) {
throw new NumberFormatException();
}
user.setBooksToRead(goal);
user.setWorkingMode(BotUser.WorkingMode.NO_MODE);
userRepository.save(user);
telegramClientService.sendMessage(
user,
Constants.PROFILE_UPDATED,
KeyboardHelper.getMainMenuKeyboard()
);
if (input.equals(SET_BOOKS_GOAL)) {
handleBooksGoalUpdate(user);
} else {
throw new NumberFormatException();
processFieldUpdate(user, input);
}
} catch (NumberFormatException e) {
telegramClientService.sendMessage(user, "❌ Ошибка! Введите целое число",
KeyboardHelper.getProfileMenuKeyboard());
} finally {
resetEditingState(user);
}
}
private void handleBooksGoalUpdate(BotUser user) {
telegramClientService.sendMessage(
user,
"Текущая цель: " + user.getBooksToRead() + "\nВведите новое количество книг:",
KeyboardHelper.getBackKeyboard()
);
}
private void processFieldUpdate(BotUser user, String input) {
try {
int goal = Integer.parseInt(input);
if (goal < 0) throw new NumberFormatException();
user.setBooksToRead(goal);
telegramClientService.sendMessage(
user,
Constants.PROFILE_UPDATED,
KeyboardHelper.getMainMenuKeyboard()
);
} catch (NumberFormatException e) {
throw new ServiceException("❌ Ошибка! Введите целое положительное число");
}
}
private void resetEditingState(BotUser user) {
user.setWorkingMode(BotUser.WorkingMode.NO_MODE);
userRepository.save(user);
}
}
@@ -51,7 +51,7 @@ public class TelegramBotServiceImpl implements TelegramBotService {
bookManageService.processBookInput(botUser,text);
break;
case SET_BOOKS_GOAL:
profileService.enterProfileEditingMode(botUser);
profileService.handleProfileUpdate(botUser, text);
break;
default:
switch (botUser.getWorkingMode()) {