79 lines
2.7 KiB
Java
79 lines
2.7 KiB
Java
package ru.cathub.telegabot.service.impl;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
import ru.cathub.telegabot.bot.KeyboardHelper;
|
|
import ru.cathub.telegabot.exception.ServiceException;
|
|
import ru.cathub.telegabot.model.BotUser;
|
|
import ru.cathub.telegabot.repository.UserRepository;
|
|
import ru.cathub.telegabot.service.ProfileService;
|
|
import ru.cathub.telegabot.service.TelegramClientService;
|
|
import ru.cathub.telegabot.utils.Constants;
|
|
|
|
import java.util.Optional;
|
|
|
|
import static ru.cathub.telegabot.utils.Constants.SET_BOOKS_GOAL;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class ProfileServiceImpl implements ProfileService {
|
|
|
|
private final UserRepository userRepository;
|
|
private final TelegramClientService telegramClientService;
|
|
|
|
@Override
|
|
public void enterProfileEditingMode(BotUser user) {
|
|
user.setWorkingMode(BotUser.WorkingMode.EDIT_PROFILE);
|
|
userRepository.save(user);
|
|
telegramClientService.sendMessage(
|
|
user,
|
|
"⌨️ Выберите параметр для редактирования:",
|
|
KeyboardHelper.getProfileMenuKeyboard()
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public void handleProfileUpdate(BotUser user, String input) throws ServiceException {
|
|
try {
|
|
if (input.equals(SET_BOOKS_GOAL)) {
|
|
handleBooksGoalUpdate(user);
|
|
} else {
|
|
processFieldUpdate(user, input);
|
|
}
|
|
} finally {
|
|
resetEditingState(user);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void handleBooksGoalUpdate(BotUser user) {
|
|
user.setWorkingMode(BotUser.WorkingMode.EDIT_PROFILE);
|
|
userRepository.save(user);
|
|
telegramClientService.sendMessage(
|
|
user,
|
|
"Текущая цель: " + (user.getBooksToRead() != null ? String.valueOf(user.getBooksToRead()) : "Не задано") + "\nВведите новое количество книг:"
|
|
);
|
|
}
|
|
|
|
private void processFieldUpdate(BotUser user, String input) throws ServiceException {
|
|
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);
|
|
}
|
|
}
|