fix: ensure proper state initialization in book creation flow

This commit is contained in:
2025-02-09 18:46:30 +03:00
parent 5f868a0351
commit 5378ab9707
2 changed files with 21 additions and 10 deletions
@@ -27,8 +27,19 @@ public class BookCreationServiceImpl implements BookCreationService {
public void handleNewAddRequest(BotUser user, String text) {
if (ADD_BOOK.equals(text)) {
BookCreationCache newCache = new BookCreationCache();
newCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
bookCacheService.updateCache(user.getId(), newCache);
telegramClientService.sendMessageWithMarkdown(user, "*Введите название книги: \uD83D\uDCDA*", null);
// Verify cache was set correctly
BookCreationCache verifyCache = bookCacheService.getCache(user.getId());
if (verifyCache != null && verifyCache.getCreationState() == BookCreationCache.CreationState.ADDING_TITLE) {
log.info("Successfully initialized ADDING_TITLE state for user {}", user.getId());
telegramClientService.sendMessageWithMarkdown(user, "*Введите название книги: \uD83D\uDCDA*", null);
} else {
log.error("Failed to initialize ADDING_TITLE state for user {}", user.getId());
telegramClientService.sendMessage(user, "❌ Ошибка: не удалось начать процесс добавления книги");
bookCacheService.clearCache(user.getId());
}
}
}
@@ -60,15 +60,15 @@ public class BookManageServiceImpl implements BookManageService {
bookCreationService.handleNewAddRequest(user, text);
break;
default:
if (editCache == null || editCache.getBookEditState() == BookEditStateCache.BookEditState.NONE) {
telegramClientService.sendMessage(user, Constants.ERROR_NO_ACTION);
return;
}
switch (editCache.getBookEditState()) {
case ADDING_TITLE, ADDING_AUTHOR, ADDING_RATING ->
bookCreationService.handleExistingAddCache(user, text);
case SELECTING_BOOK ->
bookCreationService.handleNewAddRequest(user, text);
BookCreationCache creationCache = bookCacheService.getCache(user.getId());
if (creationCache != null && creationCache.getCreationState() != null) {
// Handle book creation states
bookCreationService.handleExistingAddCache(user, text);
} else if (editCache != null && editCache.getBookEditState() != BookEditStateCache.BookEditState.NONE) {
// Handle book editing states
switch (editCache.getBookEditState()) {
case SELECTING_BOOK ->
bookCreationService.handleNewAddRequest(user, text);
case EDITING_TITLE ->
bookEditingService.updateBookTitle(user, text);
case EDITING_AUTHOR ->