diff --git a/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java b/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java index 9befc95..dd84f4f 100644 --- a/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java +++ b/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java @@ -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); + } } diff --git a/src/main/java/ru/cathub/telegabot/service/impl/TelegramBotServiceImpl.java b/src/main/java/ru/cathub/telegabot/service/impl/TelegramBotServiceImpl.java index a0b1d3a..00c5f47 100644 --- a/src/main/java/ru/cathub/telegabot/service/impl/TelegramBotServiceImpl.java +++ b/src/main/java/ru/cathub/telegabot/service/impl/TelegramBotServiceImpl.java @@ -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()) {