Files
TelegaBot/src/main/java/ru/cathub/telegabot/service/impl/BookManageServiceImpl.java
T
2025-02-16 17:53:09 +03:00

145 lines
6.4 KiB
Java

package ru.cathub.telegabot.service.impl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.cathub.telegabot.bot.KeyboardHelper;
import ru.cathub.telegabot.model.BookEditStateCache;
import ru.cathub.telegabot.model.BookCreationCache;
import ru.cathub.telegabot.model.*;
import ru.cathub.telegabot.repository.BookRepository;
import ru.cathub.telegabot.repository.UserRepository;
import ru.cathub.telegabot.service.*;
import ru.cathub.telegabot.utils.Constants;
import java.util.Optional;
import static ru.cathub.telegabot.bot.KeyboardHelper.*;
import static ru.cathub.telegabot.utils.Constants.*;
@Service
@RequiredArgsConstructor
@Slf4j
public class BookManageServiceImpl implements BookManageService {
private final BookCreationService bookCreationService;
private final BookEditingService bookEditingService;
private final BookListingService bookListingService;
private final UserRepository userRepository;
private final BookCacheService bookCacheService;
private final BookRepository bookRepository;
private final TelegramClientService telegramClientService;
@Override
public void processBookInput(BotUser user, String text) {
Long userId = user.getId();
BookCreationCache creationCache = bookCacheService.getCache(userId);
BookEditStateCache editCache = bookCacheService.getBookEditCache(userId);
switch (text) {
case LIST_BOOKS:
bookCacheService.clearBookListCache(userId);
bookCacheService.clearBookEditCache(userId);
bookListingService.showPaginatedBookList(user, 0, null, false);
break;
case EDIT_BOOKS:
bookCacheService.clearBookListCache(userId);
bookCacheService.clearBookEditCache(userId);
bookListingService.showPaginatedBookList(user, 0, null, true);
//startBookEditing(user);
break;
case ADD_BOOK:
bookCacheService.clearBookListCache(userId);
bookCacheService.clearBookEditCache(userId);
bookCreationService.handleNewAddRequest(user, text);
break;
default:
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 ->
bookEditingService.updateBookAuthor(user, text);
case EDITING_RATING ->
bookEditingService.updateBookRating(user, text);
case EDITING_DATE ->
bookEditingService.updateBookDate(user, text);
case CHOOSING_FIELD -> {} // No action needed
}
}
}
}
@Override
public void handleStartMessage(BotUser botUser) {
botUser.setWorkingMode(BotUser.WorkingMode.BOOK);
userRepository.save(botUser);
telegramClientService.sendMessageWithMarkdown(botUser, getWelcomeMessage(), KeyboardHelper.getBookMenuKeyboard());
}
@Override
public void processBookQuery(BotUser user, String text) {
if (text.startsWith("BOOK_PAGE_")) {
int page = Integer.parseInt(text.split("_")[2]);
var bookListCache = bookCacheService.getBookListState(user.getId());
if (bookListCache.getCurrentPage() != page) {
bookListCache.setCurrentPage(page);
bookCacheService.updateBookListState(user.getId(), bookListCache);
}
var bookEditCache = bookCacheService.getBookEditCache(user.getId());
bookListingService.showPaginatedBookList(user, page, bookListCache.getListMessageId(),bookEditCache != null);
} else if (text.startsWith("BOOK_EDIT_ID_")) {
Long bookId = Long.parseLong(text.split("_")[3]);
bookEditingService.processBookSelection(user, bookId);
} else {
switch (text) {
case "BOOK_EDIT_TITLE":
bookEditingService.handleEditTitle(user);
break;
case "BOOK_EDIT_AUTHOR":
bookEditingService.handleEditAuthor(user);
break;
case "BOOK_EDIT_RATING":
bookEditingService.handleEditRating(user);
break;
case "BOOK_EDIT_DATE":
bookEditingService.handleEditDate(user);
break;
case "BOOK_EDIT_CANCEL":
// cancelEditing(user);
break;
}
}
}
private String getWelcomeMessage() {
return "✨ *Добро пожаловать в Бок Книжник\\!* 📚\n" +
"_Ваш персональный помощник для управления библиотекой_\n\n" +
"Чтобы начать, отправьте:\n" +
"👉 `/start` — активировать бота и открыть меню\n\n" +
"**С нами вы сможете:**\n" +
"🔸 Добавлять книги с рейтингом\n" +
"🔸 Просматривать свою коллекцию\n" +
"🔸 Редактировать записи\n" +
"🔸 Сортировать по рейтингу\n\n" +
"📌 *Не забывайте: всегда можно вернуться в главное меню командой* `/start`\n\n" +
"_📖 Давайте начнём ваше литературное путешествие\\!_";
}
}